473,666 Members | 2,181 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 1865
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
9592
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
1627
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 typically 100 Mb, I do not want to create these arrays, but instead define a class that mimicks...
3
3045
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
1854
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
5478
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
7951
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 me know if there is anything that I am doing wrong! - thanks ---- ....
4
3308
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 it from the instance.
0
2822
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., forgetting to implement a method required by the given interface). This is handy when unit tests or...
4
1907
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
8362
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8785
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8560
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
8644
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
7389
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...
1
6200
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2776
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
2
2012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1778
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.