473,811 Members | 3,467 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

is decorator the right thing to use?

Hi,

after hearing a lot about decorators and never actually using one I have
decided to give it a try. My particular usecase is that I have class that
acts as a proxy to other classes (i.e. passes messages along to those
classes) however hand-coding this type of class is rather tedious, so I
decided to use decorator for that. Can somebody tell me if what I'm doing
is a potential shot-in-the-foot or am I on the right track? (Note, It's
rather rudimentary proof-of-concept implementation and not the final
solution I'm about to employ so there are no optimizations or
signature-preserving code there yet, just the idea).

Here's the code:

class A:
b=None
def __init__(self,b ):
self.val='aval'
self.b=b
b.val='aval'

def mymethod(self,a ):
print "A::mymetho d, ",a

def mymethod2(self, a):
print "A::another method, ",a
def Aproxy(fn):
def delegate(*args, **kw):
print "%s::%s" % (args[0].__class__.__na me__,fn.__name_ _)
args=list(args)
b=getattr(args[0],'b')
fnew=getattr(b, fn.__name__)
# get rid of original object reference
del args[0]
fnew(*args,**kw )
setattr(A,fn.__ name__,delegate )
return fn

class B:
def __init__(self):
self.val='bval'

@Aproxy
def bmethod(self,a) :
print "B::bmethod "
print a, self.val

@Aproxy
def bmethod2(self,a ):
print "B::bmethod 2"
print a, self.val

b=B()
b.bmethod('foo' )
a=A(b)
b=B()
b.val='newval'
a.bmethod('bar' )
a.bmethod2('zam ')
Sep 24 '08
25 1459
On Sep 27, 1:44 am, "Dmitry S. Makovey" <dmi...@makovey .netwrote:
George Sakkis wrote:
Although this works, the second argument to ProxyMethod shouldn't be
necessary, it's semantically redundant; ideally you would like to
write it as "bmethod = ProxyMethod('b' )".

since I'm already on exploratory trail (what about that rug being pulled
from under....?) With my code I can do really dirty tricks like this (not
necessary that I'm going to):

class B_base:
def bmethod(self):
print 'B_base'

class B(B_base):
def bmethod(self):
print 'B'

class A:
bmethod=ProxyMe thod('b',B_base .bmethod)
Yes, that's indeed dirty; don't do it :)
As before, I don't think
that's doable without metaclasses (or worse, stack frame hacking).
Below is the update of my original recipe; interestingly, it's
(slightly) simpler than before:

Out of curiosity (and trying to understand): why do you insist on
dictionaries with strings contents ( {'bmethod' : 'b1' } ) rather than
something more explicit ? Again, I can see that your code is working and I
can even understand what it's doing, just trying to explore alternatives :)

I guess my bias is towards more explicit declarations thus

bmethod=ProxyMe thod('b',B.bmet hod)

looks more attractive to me, but I stand to be corrected/educated why is
that not the right thing to do?
I see where you're coming from and I also prefer explicit reflection
mechanisms instead of strings (e.g. avoid eval/exec as much as
possible). As I mentioned, the second argument to ProxyMethod is (for
all sane purposes) redundant, so if you could implement it in a way
that "bmethod = ProxyMethod('b' )" worked, I would be all for it, but
AFAIK it's not possible without a metaclass. A dict with string keys
and values to be consumed by a metaclass is perhaps the simplest thing
that could possibly work. It contains all the relevant information for
hooking the proxy to the delegate methods and nothing more; zero
boilerplate code overhead. Also note that it's not that big of a
difference; you have to provide the attribute name as a string anyway.
Another thing that turns me away from string dictionaries is that those are
the ones causing me more trouble hunting down typos. Maybe it's just "my
thing" so I'm not going to insist on it. I'm open to arguments against that
theory.
From my experience, I am equally prone to typos for both strings and
regular attributes; I agree though that the traceback information is
often more helpful when you mistype an attribute.
One argument I can bring in defence of more explicit declarations is IDE
parsing when autocompletion for B.bme... pops up (suggesting bmethod and
bmethod2) and with 'b':'bmethod' it never happens.
I don't rely on autocompleting IDEs, at least in dynamic languages, so
it's not much of an issue for me but yes, it's another small argument
against strings.

George
Sep 27 '08 #21
On Sep 27, 9:23 am, George Sakkis <george.sak...@ gmail.comwrote:
On Sep 27, 1:44 am, "Dmitry S. Makovey" <dmi...@makovey .netwrote:
I guess my bias is towards more explicit declarations thus
bmethod=ProxyMe thod('b',B.bmet hod)
looks more attractive to me, but I stand to be corrected/educated why is
that not the right thing to do?

I see where you're coming from and I also prefer explicit reflection
mechanisms instead of strings (e.g. avoid eval/exec as much as
possible). As I mentioned, the second argument to ProxyMethod is (for
all sane purposes) redundant, so if you could implement it in a way
that "bmethod = ProxyMethod('b' )" worked, I would be all for it, but
AFAIK it's not possible without a metaclass.
Just for completeness, here's a metaclass version that uses
ProxyMethod declarations instead of a dict; you'll probably like this
better:

#======= usage =============== ==========
from proxies import Proxy, ProxyMethod

class B(object):
def __init__(self, val): self.val = val
def bmethod(self,n) : print "B::bmethod ", self.val, n
def bmethod2(self,n ,m): print "B::bmethod 2", self.val, n, m

class C(object):
def __init__(self, val): self.val = val
def cmethod(self,x) : print "C::cmethod ", self.val, x
def cmethod2(self,x ,y): print "C::cmethod2",s elf.val, x, y
cattr = 4

class A(Proxy):
def __init__(self, b1, b2, c):
print "init A()"
# must call Proxy.__init__
super(A,self)._ _init__(b1=b1, b2=b2, c=c)

def amethod(self,a) :
print "A::mymetho d",a

bmethod = ProxyMethod('b1 ')
bmethod2 = ProxyMethod('b2 ')
cmethod = ProxyMethod('c' )
a = A(B(10), B(20), C(30))
a.amethod('foo' )

print "bound proxy calls"
a.bmethod('foo' )
a.bmethod2('bar ','baz')
a.cmethod('foo' )
try: a.cmethod2('bar ','baz')
except Exception, ex: print ex

print "unbound proxy calls"
A.bmethod(a,'fo o')
A.bmethod2(a,'b ar','baz')
A.cmethod(a, 'foo')
try: A.cmethod2(a,'b ar','baz')
except Exception, ex: print ex

#======= output =============== =============== ==========

init A()
A::mymethod foo
bound proxy calls
B::bmethod 10 foo
B::bmethod2 20 bar baz
C::cmethod 30 foo
'A' object has no attribute 'cmethod2'
unbound proxy calls
B::bmethod 10 foo
B::bmethod2 20 bar baz
C::cmethod 30 foo
type object 'A' has no attribute 'cmethod2'

#====== proxies.py =============== =============== ========

class _ProxyMeta(type ):
def __new__(meta, name, bases, namespace):
for attrname,value in namespace.iteri tems():
if isinstance(valu e, ProxyMethod) and value.name is None:
value.name = attrname
return super(_ProxyMet a,meta).__new__ (meta, name, bases,
namespace)
class ProxyMethod(obj ect):
def __init__(self, proxy_attr, name=None):
self._proxy_att r = proxy_attr
self.name = name

def __get__(self, proxy, proxytype):
if proxy is not None:
return self.__get_targ et_attr(proxy)
else:
return self.__unbound_ method

def __unbound_metho d(self, proxy, *args, **kwds):
method = self.__get_targ et_attr(proxy)
return method(*args, **kwds)

def __get_target_at tr(self, proxy):
try:
delegate = getattr(proxy, self._proxy_att r)
return getattr(delegat e, self.name)
except AttributeError:
raise AttributeError( '%r object has no attribute %r' %
(proxy.__class_ _.__name__,
self.name))
class Proxy(object):
__metaclass__ = _ProxyMeta

def __init__(self, **attr2delegate ):
self.__dict__.u pdate(attr2dele gate)

#============== =============== =============== ==========

If you want to eliminate completely specifying attributes with
strings, it's easy to modify the above so that you write instead:

class A(Proxy):
...
bmethod = ProxyMethod(lam bda self: self.b1)
bmethod2 = ProxyMethod(lam bda self: self.b2)

This is more verbose for the common case, but it's more flexible in
cases where the callable may be more complex than a plain getattr().
Actually you can support both, it doesn't have to be either/or; just
check whether the argument to ProxyMethod is a callable and if not,
make it:

from operator import attrgetter

class ProxyMethod(obj ect):
def __init__(self, proxy_attr, name=None):
if not callable(proxy_ attr):
proxy_attr = attrgetter(prox y_attr)
...

Remaining Implementation is left as an exercise to the reader ;)

George
Sep 27 '08 #22
On Sep 27, 11:27 am, George Sakkis <george.sak...@ gmail.comwrote:
If you want to eliminate completely specifying attributes with
strings, it's easy to modify the above so that you write instead:

class A(Proxy):
...
bmethod = ProxyMethod(lam bda self: self.b1)
bmethod2 = ProxyMethod(lam bda self: self.b2)
It's funny how often you come with a better solution a few moments
after htting send! The snippet above can (ab)use the decorator syntax
so that it becomes:

class A(Proxy):

@ProxyMethod
def bmethod(self):
return self.b1

@ProxyMethod
def bmethod2(self):
return self.b2

With the observation that ProxyMethod has access both to the callable
that returns the delegate and the name of the delegated method, we can
remove the need for the metaclass and the Proxy base class altogether:

class proxymethod(obj ect):
def __init__(self, get_delegate):
self._get_deleg ate = get_delegate

def __get__(self, proxy, proxytype):
if proxy is not None:
return self.__get_targ et_attr(proxy)
else:
return self.__unbound_ method

def __unbound_metho d(self, proxy, *args, **kwds):
method = self.__get_targ et_attr(proxy)
return method(*args, **kwds)

def __get_target_at tr(self, proxy):
get_delegate = self._get_deleg ate
try: return getattr(get_del egate(proxy),
get_delegate.__ name__)
except AttributeError:
raise AttributeError( '%r object has no attribute %r' %
(proxy.__class_ _.__name__, get_delegate.__ name__))
class A(object):

def __init__(self, b1, b2):
self.b1 = b1
self.b2 = b2

def amethod(self,a) :
print "A::mymetho d",a

@proxymethod
def bmethod(self):
return self.b1

@proxymethod
def bmethod2(self):
return self.b2
a = A(B(10), B(20))
a.amethod('foo' )

print "bound proxy calls"
a.bmethod('foo' )
a.bmethod2('bar ','baz')

print "unbound proxy calls"
A.bmethod(a,'fo o')
A.bmethod2(a,'b ar','baz')

So back to the OP's original question and after a long circle.. a
decorator might well be the right thing to use after all :)

George
Sep 27 '08 #23
George Sakkis wrote:
It's funny how often you come with a better solution a few moments
after htting send! The snippet above can (ab)use the decorator syntax
so that it becomes:

class A(Proxy):

@ProxyMethod
def bmethod(self):
return self.b1

@ProxyMethod
def bmethod2(self):
return self.b2
That is outstanding! This code looks very clean to me (just a touch cryptic
around declarations in A, but that was unavoidable anyway). Seems like the
right way to read it would be bottom up (or is it only my mind so
perverted?). By the looks of it - it does exactly what I needed with great
number of possibilities behind it and is very lightweight and transparent.
Now I regret I haven't come up with it myself :-D

George, at this point I'm out of rugs - so no more rug pulling from under
your feet for me.

Now I'm going to apply all this knowledge to my code, see how that goes and
come back with more questions later.

Thank you (all) very much for a great discussion. This thread educated me
quite a bit on descriptors and why one would need them, and decorators -
just as subject line suggested, were not forgotten.
Sep 28 '08 #24
On Sep 27, 11:38*pm, "Dmitry S. Makovey" <dmi...@makovey .netwrote:
George Sakkis wrote:
It's funny how often you come with a better solution a few moments
after htting send! The snippet above can (ab)use the decorator syntax
so that it becomes:
class A(Proxy):
* * @ProxyMethod
* * def bmethod(self):
* * * * return self.b1
* * @ProxyMethod
* * def bmethod2(self):
* * * * return self.b2

That is outstanding!
FYI, in case you missed it the final version doesn't need a Proxy base
class, just inherit from object. Also lowercased ProxyMethod to look
similar to staticmethod/classmethod:

class A(object):

def __init__(self, b1, b2):
self.b1 = b1
self.b2 = b2

@proxymethod
def bmethod(self):
return self.b1

@proxymethod
def bmethod2(self):
return self.b2

George
Sep 28 '08 #25
George Sakkis wrote:
FYI, in case you missed it the final version doesn't need a Proxy base
class, just inherit from object. Also lowercased ProxyMethod to look
similar to staticmethod/classmethod:
I cought that, just quoted the wrong one :)
class A(object):

def __init__(self, b1, b2):
self.b1 = b1
self.b2 = b2

@proxymethod
def bmethod(self):
return self.b1

@proxymethod
def bmethod2(self):
return self.b2

George
Sep 28 '08 #26

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

Similar topics

14
2320
by: Sandy Norton | last post by:
If we are going to be stuck with @decorators for 2.4, then how about using blocks and indentation to elminate repetition and increase readability: Example 1 --------- class Klass: def __init__(self, name):
24
2074
by: Steven Bethard | last post by:
I think one of the biggest reasons we're having such problems coming to any agreement on decorator syntax is that each proposal makes a number of syntax decisions, not just one. For decorators, I see the following decisions that must be made: 1) Indicator Proposals differ on how some sort of indicator of "decoratorhood" is use. These include: * none (e.g. just the list, as in the "list-after-def" suggestion) * the '@' character
11
1639
by: Ville Vainio | last post by:
It might just be that @decorator might not be all that bad. When you look at code that uses it it's not that ugly after all. A lot of the furor about this is probably because it happened so quicly. The situation might have been different if we had seen a pronouncement a week before, in the vein of "I have chosen this syntax - it will go in to the next alpha". My chief worry was throwing away one of the few unused ascii symbols, but if...
37
2616
by: Bengt Richter | last post by:
ISTM that @limited_expression_producing_function @another def func(): pass is syntactic sugar for creating a hidden list of functions. (Using '|' in place of '@' doesn't change the picture much (except for people whose tools depend on '@' ;-)). I.e., (not having the source or time to delve) the apparent semantics of the above is something roughly like
30
2514
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 closer look at function def's and call's, which is something I tend to take for granted. I'm not sure this is 100%, or if there are other ways to view it, but it seems to make sense when viewed this way. Is there a way to do this same thing...
22
2243
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 in it that needs to be addressed. (1) The test for the 'function' object needs to not test for a string but an object type instead.
6
1404
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 accepts(*types): > def check_accepts(f): > assert len(types) == f.func_code.co_argcount
11
1834
by: glen.coates.bigworld | last post by:
I'm developing a library at the moment that involves many classes, some of which have "exposed" capabilities. I'm trying to design a nice interface for both exposing those capabilities, and inspecting instances to find out what capabilities they have. At the moment, I'm leaning towards a superclass (Exposed) that defines a static method which is a decorator (expose) such that any derived class can mark a method with @Exposed.expose and...
4
2474
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 has all of its decorator implementation inside a decorator() method. Every other attribute access is being proxied to decorator().getParent(). Here's my attempt: -------------------------------------------------------
0
9727
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10398
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10133
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9204
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6889
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5554
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.