Transfer undefined class methods to attribute's method.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Maarten van Reeuwijk

    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
  • anton muhin

    #2
    Re: Transfer undefined class methods to attribute's method.

    Maarten van Reeuwijk wrote:[color=blue]
    > 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()
    >
    >
    >[/color]

    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.

    Comment

    • Diez B. Roggisch

      #3
      Re: Transfer undefined class methods to attribute's method.

      > Maybe I was a little too detailed in my previous post [same title]. I can[color=blue]
      > 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![/color]

      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

      Comment

      • Peter Otten

        #4
        Re: Transfer undefined class methods to attribute's method.

        Maarten van Reeuwijk wrote:
        [color=blue]
        > 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()
        >
        >
        >[/color]

        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

        Comment

        • Maarten van Reeuwijk

          #5
          Re: Transfer undefined class methods to attribute's method.

          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

          Comment

          Working...