473,386 Members | 1,758 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,386 software developers and data experts.

Help in using introspection to simplify repetitive code

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

Aug 20 '06 #1
6 1658

js********@gmail.com 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

Aug 20 '06 #2

<js********@gmail.comwrote in message
news:11*********************@75g2000cwc.googlegrou 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

Aug 20 '06 #3
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

Aug 21 '06 #4
js********@gmail.com 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>

Aug 21 '06 #5

Fredrik Lundh wrote:
js********@gmail.com 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

Aug 21 '06 #6
js********@gmail.com 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.

Aug 27 '06 #7

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

Similar topics

0
by: Carlos Ribeiro | last post by:
Hello all, While studying some of the templating systems available for Python, I've come up with an idea to implement templates using decorators. The following code snippet runs in Python 2.3; a...
13
by: Steven Scaife | last post by:
I have decided to re-write the intranet site i created over a year ago. The coding is pretty awful and hard to read cos I made the mistake of not putting comments in or putting crappy comments in...
4
by: Benjamin Rutt | last post by:
I'm trying to learn about introspection in Python. my ultimate goal is to be able to build a module "text database" of all modules that are in the sys.path, by discovering all candidate modules...
4
by: Steven T. Hatton | last post by:
Has there been any substantial progress toward supporting introspection/reflection in C++? I don't intend to mean it should be part of the Standard. It would, nonetheless, be nice to have a...
98
by: tjb | last post by:
I often see code like this: /// <summary> /// Removes a node. /// </summary> /// <param name="node">The node to remove.</param> public void RemoveNode(Node node) { <...> }
4
by: MC felon | last post by:
hello. i tried this for a simple push_back action..... it says "ostream - no such file or directory" #include <iostream> #include <conio2.h> #include <string> #include <ostream> int main() {
3
by: James Stroud | last post by:
Hello, I wanted to automagically generate an instance of a class from a dictionary--which might be generated from yaml or json. I came up with this: # automagical constructor def...
14
by: Dave Rahardja | last post by:
Is there a way to generate a series of statements based on the data members of a structure at compile time? I have a function that reverses the endianness of any data structure: /// Reverse...
4
by: mirandacascade | last post by:
Not sure if 'introspection' is the correct term, so apologies if subject line is off-base. 1) Is there any functionality that answers the question: what module am I in? 2) Is there any...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.