473,574 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Transfer undefined class methods to attribute's method.

Hello,

Maybe I was a little too detailed in my previous post [same title]. I can
boil down my problem to this: say I have a class A that I encapsulate with
a class Proxy. Now I just want to override and add some functionality (see
my other post why). All functionality not defined in the Proxy class should
be delegated (I can't use inheritance, see other post). It should be
possible to achieve this using Python's great introspection possibilities,
but I can't find out how. Any help would be really appreciated!

TIA, Maarten

Example (class A and Proxy):

class A:
def __init__(self):
pass

def methodA(self):
pass

def methodB(self):
pass

def methodC(self):
pass

class Proxy:
def __init__(self):
self.a = A()

# maybe scan the methods in A and not in this class?????
# setup a hook for undefined methods?

def methodA(self):
# what I DON'T want:
return self.a.methodA( )
# and this for every method...
# maybe something like this?
def process_unknown method(self, method, args):
return self.a.method(a rgs)

P = Proxy()
P.methodA()
P.methodC()

output:
Traceback (most recent call last):
File "group.py", line 36, in ?
P.methodC()

--
=============== =============== =============== =============== =======
Maarten van Reeuwijk Thermal and Fluids Sciences
Phd student dept. of Multiscale Physics
www.ws.tn.tudelft.nl Delft University of Technology
Jul 18 '05 #1
4 1860
Maarten van Reeuwijk wrote:
Hello,

Maybe I was a little too detailed in my previous post [same title]. I can
boil down my problem to this: say I have a class A that I encapsulate with
a class Proxy. Now I just want to override and add some functionality (see
my other post why). All functionality not defined in the Proxy class should
be delegated (I can't use inheritance, see other post). It should be
possible to achieve this using Python's great introspection possibilities,
but I can't find out how. Any help would be really appreciated!

TIA, Maarten

Example (class A and Proxy):

class A:
def __init__(self):
pass

def methodA(self):
pass

def methodB(self):
pass

def methodC(self):
pass

class Proxy:
def __init__(self):
self.a = A()

# maybe scan the methods in A and not in this class?????
# setup a hook for undefined methods?

def methodA(self):
# what I DON'T want:
return self.a.methodA( )
# and this for every method...
# maybe something like this?
def process_unknown method(self, method, args):
return self.a.method(a rgs)

P = Proxy()
P.methodA()
P.methodC()

output:
Traceback (most recent call last):
File "group.py", line 36, in ?
P.methodC()


This might help (almost untested):

class Proxy(object):
def __init__(self, obj):
self.obj_ = obj

def __getattr__(sel f, name):
return getattr(self.ob j_, name)

class Foo(object):
def methodA(self):
return 'Foo.methodA'

def methodB(self):
return 'Foo.methodB'

foo = Foo()
p = Proxy(foo)

print p.methodA()
print p.methodB()

regards,
anton.
Jul 18 '05 #2
> Maybe I was a little too detailed in my previous post [same title]. I can
boil down my problem to this: say I have a class A that I encapsulate with
a class Proxy. Now I just want to override and add some functionality (see
my other post why). All functionality not defined in the Proxy class
should be delegated (I can't use inheritance, see other post). It should
be possible to achieve this using Python's great introspection
possibilities, but I can't find out how. Any help would be really
appreciated!


Why don't you just scan the underlying object for all methods it has (using
the objects __dict__, filtering with type method) and add these methods on
your proxy when there exists no method of the same name (using the proyies
dict)? That would eliminate the need of a general-purpose method call
interception and do what you want.

I'm a little bit too lazy right now create a working example, but I think
you should be able to come up with your own in no time - if not, ask again
(and someone more enlightened might answer, or I get down on my a** and
write something... :))

--
Regards,

Diez B. Roggisch
Jul 18 '05 #3
Maarten van Reeuwijk wrote:
Hello,

Maybe I was a little too detailed in my previous post [same title]. I can
boil down my problem to this: say I have a class A that I encapsulate with
a class Proxy. Now I just want to override and add some functionality (see
my other post why). All functionality not defined in the Proxy class
should be delegated (I can't use inheritance, see other post). It should
be possible to achieve this using Python's great introspection
possibilities, but I can't find out how. Any help would be really
appreciated!

TIA, Maarten

Example (class A and Proxy):

class A:
def __init__(self):
pass

def methodA(self):
pass

def methodB(self):
pass

def methodC(self):
pass

class Proxy:
def __init__(self):
self.a = A()

# maybe scan the methods in A and not in this class?????
# setup a hook for undefined methods?

def methodA(self):
# what I DON'T want:
return self.a.methodA( )
# and this for every method...
# maybe something like this?
def process_unknown method(self, method, args):
return self.a.method(a rgs)

P = Proxy()
P.methodA()
P.methodC()

output:
Traceback (most recent call last):
File "group.py", line 36, in ?
P.methodC()


The __getattr__() method could be helpful for your problem. It's calld for
every missing attribute - not just methods. If you want to set attributes,
there's a corresponding __setattr__() method which is called for *every*
attribute.

Adopting your example:

class A:
def methodA(self):
print "original A"

def methodB(self):
print "original B"
class Proxy:
def __init__(self, wrapped):
self._wrapped = wrapped

def methodA(self):
print "replacemen t A, internally calling",
return self._wrapped.m ethodA()

def __getattr__(sel f, name):
return getattr(self._w rapped, name)

a = A()
p = Proxy(a)
p.methodA()
p.methodB()
Peter
Jul 18 '05 #4
That does the trick!

Thanks guys,

Maarten
--
=============== =============== =============== =============== =======
Maarten van Reeuwijk Thermal and Fluids Sciences
Phd student dept. of Multiscale Physics
www.ws.tn.tudelft.nl Delft University of Technology
Jul 18 '05 #5

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

Similar topics

2
9586
by: Fernando Rodriguez | last post by:
Hi, I need to traverse the methods defined in a class and its superclasses. This is the code I'm using: # An instance of class B should be able to check all the methods defined in B #and A, while an instance of class C should be able to check all methods #defined in C, B and A. #------------------------------------------------
0
1626
by: Maarten van Reeuwijk | last post by:
I am constructing a "placeholder array" to mimick Numeric arrays. I have a 3D regular structured domain, so I can describe the axis with 3 1D arrays x, y and z. All the variables defined on this field (temperature, velocity) are 3D-fields, and to treat everything the same, I want to have x,y and z also as a 3d-array. However, as one field is...
3
3041
by: Robert | last post by:
Python doesn't know the class of a method when container not direct class attribute: >>> class X: .... def f():pass .... g=f .... l= .... >>> X.g <unbound method X.f>
4
1852
by: Terry Olsen | last post by:
Since both methods seem to produce the same results, in which cases would you prefer one over the other? The only thing I would think is using Server.Transfer because of some browsers blocking redirects. *** Sent via Developersdex http://www.developersdex.com ***
2
5472
by: Vivek Ragunathan | last post by:
Hi Are the members in a static class in C# class synchronized for multiple thread access. If yes, are all static members in a C# class auto synchronized ? Regards Vivek Ragunathan
22
7939
by: Saul | last post by:
I have a set of radio buttons that are created dynamically, after rendered I try loop thru this set by getting the length of the set, but I keep getting an error stating the element is undefined. I am using getElelementsByName since these are radio buttons, but it seems that the dynamic element is not seen!!! This is my code... please let...
4
3297
by: Pedro Werneck | last post by:
Hi all I noticed something strange here while explaining decorators to someone. Not any real use code, but I think it's worth mentioning. When I access a class attribute, on a class with a custom metaclass with a __getattribute__ method, the method is used when acessing some attribute directly with the class object, but not when you do...
0
2816
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass magic, I wrote the following module. It is mainly useful as a light weight tool to help programmers catch mistakes at definition time (e.g.,...
4
1902
by: Travis | last post by:
Is it considered good practice to call a mutator when inside the same class or modify the attribute directly? So if there's a public method SetName() would it be better from say ::Init() to call SetName("none") or just set name="none"?
0
7736
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8249
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7826
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...
0
6461
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...
1
5631
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3755
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2251
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
1
1350
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1066
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...

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.