473,396 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Metaclasses / Decorators for magick with functions

Hi all,

I wanted to do some magick to classes so that any method.__name__.startswith('list') would be expected to return a list and would magically wrap the elements of that list with an ItemWrapper() instance.

For instance, this might be useful for automatically wrapping the rows of a cursor result set from a database with the excellent dtuple. Besides, it's evil, and sometimes evil is good. ;)

This is what I came up with using Python 2.3 (no @ available). Is there a better way?

class ItemWrapper:
"""A class that wraps an item in a list"""
def __init__(self,item):
self.item = item

def val(self):
"""Example wrapped method"""
return self.item

class ListWrapper:
"""
Wraps the elements of a functions return list with
ItemWrapper() instances
"""
def __init__(self,f):
self.f = f

def __call__(self,*args,**kwds):
vals = self.f(self.f,*args,**kwds)
return [ItemWrapper(x) for x in vals]

class MyMeta(type):
"""
Metaclass that will wrap methods who's names
start with 'list' with ListWrappers
"""
def __new__(cls, name, bases, dct):
for key,item in dct.items():
if key.startswith('list'):
import types
if type(item) == types.FunctionType:
dct[key] = ListWrapper(dct[key])
return type.__new__(cls, name, bases, dct)

class ParentTest:
"""
Parent class to inherit from, so folks know that they're
declaring a class with magick
"""
__metaclass__ = MyMeta

class Test(ParentTest):
"""
A class who's listXXX methods return lists of ItemWrappers()
instead of what is expected. BWWWWAHAHAHAHAH!
"""
def list1(self):
return [1,2,3,4]

if __name__ == '__main__':

t = Test()
vals = t.list1()
for x in vals:
print x,x.val()
Jul 18 '05 #1
0 898

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michael Sparks | last post by:
Anyway... At Europython Guido discussed with everyone the outstanding issue with decorators and there was a clear majority in favour of having them, which was good. From where I was sitting it...
2
by: Paul Morrow | last post by:
....that they warrant an entirely new syntax? It seems that they are very similar in a very significant way --- they alter the default behavior of something. IMO, it's not a stretch to say that...
8
by: Jan-Ole Esleben | last post by:
Hi! I am new to this list, and maybe this is a stupid question, but I can't seem to find _any_ kind of answer anywhere. What I want to do is the following: I want to insert a class variable...
10
by: sysfault | last post by:
Does anyone know of any good documentation on these topics, doesn't look like the official python tutorial covers them. Thanks in advance. -- A wise man knows he knows nothing.
0
by: Michael Ekstrand | last post by:
I've been googling around for a bit trying to find some mechanism for doing in Python something like Java's synchronized methods. In the decorators PEP, I see examples using a hypothetical...
10
by: Jerry | last post by:
I have just started to do some semi-serious programming (not one-off specialized scripts) and am loving Python. I just seem to get most of the concepts, the structure, and the classes (something I...
9
by: rahatekarabhijeet | last post by:
I downloaded the sorce from CPAN(PerlMagick-6.32.tar.gz). I followed the normal procedure to install perl module on unix platform, i.e. 1) perl Makefile.PL during the execution of this command i...
2
by: Andrew West | last post by:
Probably a bit of weird question. I realise decorators shouldn't be executed until the function they are defined with are called, but is there anyway for me to find all the decorates declared in a...
0
by: Gabriel Genellina | last post by:
En Tue, 29 Jul 2008 08:45:02 -0300, Themis Bourdenas <bourdenas@gmail.com> escribi�: In a very strict sense, I'd say that all those references to "method decorators" are wrong - because...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.