473,326 Members | 2,182 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,326 software developers and data experts.

deferred decorator

i was intrigued with a recently posted cookbook recipe which implements deferred
results with decorators:

http://aspn.activestate.com/ASPN/Coo.../Recipe/355651

the problem i see with this recipe is that the author only tested his code by
printing the values which causes __getattr__ to be called with '__str__' and
work correctly. but, if you were to remove the print statements and just do a v
+ v, __getattr__ won't get called as he expected and the join will never happen.
i think by making __coerce__ as smart as __getattr__ this recipe would work.
can someone validate that i'm correct in what i'm saying?

i'm also curious if it's possible to write this recipe using the new class style
for the Deffered class. it appears you can nolonger delegate all attributes
including special methods to the contained object by using the __getattr__ or
the new __getattribute__ methods. does anyone know how to port this recipe to
the new class style?

thanks,

bryan
Jul 18 '05 #1
3 1817
Bryan wrote:
i'm also curious if it's possible to write this recipe using the new
class style for the Deffered class. it appears you can nolonger
delegate all attributes including special methods to the contained
object by using the __getattr__ or the new __getattribute__ methods.
does anyone know how to port this recipe to the new class style?


Override __getattribute__. I don't know why you think it doesn't let you
override all attribute accesses, as that's exactly what it is for.

Cheers,
Nick.

--
Nick Coghlan | nc******@email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #2
Nick Coghlan wrote:
Bryan wrote:
i'm also curious if it's possible to write this recipe using the new
class style for the Deffered class. it appears you can nolonger
delegate all attributes including special methods to the contained
object by using the __getattr__ or the new __getattribute__ methods.
does anyone know how to port this recipe to the new class style?

Override __getattribute__. I don't know why you think it doesn't let you
override all attribute accesses, as that's exactly what it is for.

Cheers,
Nick.


here's an example. __getattribute__ gets called for x but not for the special
method __add__. and the __str__ method was found in the base class object which
printed the memory location, but didn't call __getattribute__. so
__getattribute__ cannot be used to capture special methods and delegate them to
an encapsulated object. this is why i'm asking what the technique using new
classes would be for this recipe.

class Foo(object): .... def __getattribute__(self, name):
.... raise AttributeError('__getattribute__ is called')
.... f = Foo()
f.x() Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "<interactive input>", line 3, in __getattribute__
AttributeError: __getattribute__ is called f + f Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: unsupported operand type(s) for +: 'Foo' and 'Foo' str(f)

'<__main__.Foo object at 0x0118AD10>'
Jul 18 '05 #3
Bryan wrote:
Nick Coghlan wrote:
Bryan wrote:
i'm also curious if it's possible to write this recipe using the new
class style for the Deffered class. it appears you can nolonger
delegate all attributes including special methods to the contained
object by using the __getattr__ or the new __getattribute__ methods.
does anyone know how to port this recipe to the new class style?


Override __getattribute__. I don't know why you think it doesn't let
you override all attribute accesses, as that's exactly what it is for.

Cheers,
Nick.


here's an example. __getattribute__ gets called for x but not for the
special method __add__. and the __str__ method was found in the base
class object which printed the memory location, but didn't call
__getattribute__. so __getattribute__ cannot be used to capture special
methods and delegate them to an encapsulated object. this is why i'm
asking what the technique using new classes would be for this recipe.


Hmm, good point. I now have a vague idea why it happens that way, too
(essentially, it appears the setting of the magic methods causes the interpreter
to be notified that the object has a Python-defined method to call. With only
__getattribute__ defined, that notification doesn't happen for all of the other
magic methods).

This actually makes sense - otherwise how would the method which implements
__getattribute__ be retrieved?

Similarly, in the existing recipe, none of __init__, __call__, __coerce__ or
__getattr__ are delegated - the behaviour is actually consistent for all magic
methods (I'm curious how the existing recipe actually works - it seems the use
of "self.runfunc" in __call__ should get delegated. It obviously isn't, though).

Something like the following might work (I haven't tested it though):

class Deferred(object):
...

def lookup_magic(f_name):
def new_f(self, *args, **kwargs):
return getattr(self, f_name)(*args, **kwargs)
return new_f

func_list = ['__add__', '__radd__', ...]
for f_name in func_list:
setattr(Deferred, f_name) = lookup_magic(f_name)

A metaclass may actually provide a cleaner solution, but I'm not quite sure
where to start with that.

Cheers,
Nick.

--
Nick Coghlan | nc******@email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #4

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

Similar topics

41
by: John Marshall | last post by:
How about the following, which I am almost positive has not been suggested: ----- class Klass: def __init__(self, name): self.name = name deco meth0: staticmethod def meth0(x):
30
by: Ron_Adam | last post by:
I was having some difficulty figuring out just what was going on with decorators. So after a considerable amount of experimenting I was able to take one apart in a way. It required me to take a...
22
by: Ron_Adam | last post by:
Hi, Thanks again for all the helping me understand the details of decorators. I put together a class to create decorators that could make them a lot easier to use. It still has a few glitches...
3
by: Ron_Adam | last post by:
Ok... it's works! :) So what do you think? Look at the last stacked example, it process the preprocess's first in forward order, then does the postprocess's in reverse order. Which might be...
6
by: Michele Simionato | last post by:
could ildg wrote: > I think decorator is a function which return a function, is this right? > e.g. The decorator below if from http://www.python.org/peps/pep-0318.html#id1. > > def...
8
by: Frank van Vugt | last post by:
Hi, If during a transaction a number of deferred triggers are fired, what will be their execution order upon the commit? Will they be executed in order of firing or alfabetically or...
5
by: Doug | last post by:
I am looking at using the decorator pattern to create a rudimentary stored proc generator but am unsure about something. For each class that identifies a part of the stored proc, what if I want to...
4
by: thomas.karolski | last post by:
Hi, I would like to create a Decorator metaclass, which automatically turns a class which inherits from the "Decorator" type into a decorator. A decorator in this case, is simply a class which...
8
by: Chris Forone | last post by:
hello group, is there a possibility to implement the decorator-pattern without new/delete (nor smartpt)? if not, how to ensure correct deletion of the objects? thanks & hand, chris
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.