Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Help in using introspection to simplify repetitive code

Reply
Thread Tools

Help in using introspection to simplify repetitive code

 
 
jsceballos@gmail.com
Guest
Posts: n/a
 
      08-20-2006
Hello.
I'm writing a proxy class, i.e: a class whose methods mostly delegate
their functionality to other class object. Most of the methods (which
are quite a lot) defined in the class would end up being:

def thisIsTheMethodName(self):
self._handlerClass.thisIsTheMethodName()

The handler object is the same in all methods.

I was wondering if there is a way to simplify this proxy class, maybe
using some pythonic technique like metaclasses, introspection... any
suggestion is appreciated.

Thanks,

Javier Sanz

 
Reply With Quote
 
 
 
 
Simon Percivall
Guest
Posts: n/a
 
      08-20-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hello.
> I'm writing a proxy class, i.e: a class whose methods mostly delegate
> their functionality to other class object. Most of the methods (which
> are quite a lot) defined in the class would end up being:
>
> def thisIsTheMethodName(self):
> self._handlerClass.thisIsTheMethodName()
>
> The handler object is the same in all methods.
>
> I was wondering if there is a way to simplify this proxy class, maybe
> using some pythonic technique like metaclasses, introspection... any
> suggestion is appreciated.
>
> Thanks,
>
> Javier Sanz


http://docs.python.org/ref/attribute...s.html#l2h-206

 
Reply With Quote
 
 
 
 
Terry Reedy
Guest
Posts: n/a
 
      08-20-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ps.com...
> Hello.
> I'm writing a proxy class, i.e: a class whose methods mostly delegate
> their functionality to other class object. Most of the methods (which
> are quite a lot) defined in the class would end up being:
>
> def thisIsTheMethodName(self):
> self._handlerClass.thisIsTheMethodName()


Are these parameterless static methods
or should this be self._handlerClass.thisIsTheMethodName(self)
or does self get auto-bound even though not a _handlerClass instance?
(I have never needed or done such delegation.)

> The handler object is the same in all methods.
>
> I was wondering if there is a way to simplify this proxy class, maybe
> using some pythonic technique like metaclasses, introspection... any
> suggestion is appreciated.


My immediate thought would be to start with

_forwarded = set(......) # of forwarded method names
def __getattr__(self, name):
if name in _forwarded: return getattr(self._handlerClass, name)

but I am not sure if this gives the right wrapping and binding.

Terry Jan Reedy



 
Reply With Quote
 
jsceballos@gmail.com
Guest
Posts: n/a
 
      08-21-2006
As you mention, wether the methods take arguments or not is something
to have into account.
And they do take arguments, and a variable number of them, so AFAIK
hooking with __getattr__ or __getattribute__ will not work, as you can
only get the method name with that. I was thinking of some __call__
overriding, but I've never done it before and I don't know if this
could be the way to go.
--


Un saludo,

Javier

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      08-21-2006
(E-Mail Removed) wrote:

> And they do take arguments, and a variable number of them, so AFAIK
> hooking with __getattr__ or __getattribute__ will not work, as you can
> only get the method name with that.


why not just return the bound method *object* (a callable), and let the
caller call that as usual (see Terry's last example).

(hint: x.foo() can be written f=getattr(x,"foo"); f())


</F>

 
Reply With Quote
 
Tim N. van der Leeuw
Guest
Posts: n/a
 
      08-21-2006

Fredrik Lundh wrote:
> (E-Mail Removed) wrote:
>
> > And they do take arguments, and a variable number of them, so AFAIK
> > hooking with __getattr__ or __getattribute__ will not work, as you can
> > only get the method name with that.

>
> why not just return the bound method *object* (a callable), and let the
> caller call that as usual (see Terry's last example).
>
> (hint: x.foo() can be written f=getattr(x,"foo"); f())
>
>
> </F>


I can tell you from my experience that this works; I've used this
before to make something very much like this proxy-class:

class RequestCalculations(object):
def __init__(self, request):
self.serviceType, self.facade =
makeMessageFacadeInstance(request)
return

def __getattr__(self, name):
return getattr(self.facade, name)

(rest of the code omitted)

Cheers,

--Tim

 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      08-27-2006
(E-Mail Removed) a écrit :
> As you mention, wether the methods take arguments or not is something
> to have into account.
> And they do take arguments, and a variable number of them, so AFAIK
> hooking with __getattr__ or __getattribute__ will not work, as you can
> only get the method name with that.


Nope. Defining __getattr__ is the canonical pythonic way to do
delegation. Remember that in Python, functions and methods are objects
too and can be passed around/returned etc just like any other object.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I simplify this code? James Yong Java 6 12-15-2006 09:59 PM
HELP!! (kind of repetitive, yes.) MC felon C++ 4 12-10-2006 04:09 AM
how to simplify/shorten this loop of bit shifting code? ben C Programming 8 02-17-2004 06:53 PM
Re: Repetitive code (Newbie) Jerry VHDL 2 08-07-2003 03:28 PM
Re: Repetitive code (Newbie) smu VHDL 0 08-07-2003 03:48 AM



Advertisments