Help in using introspection to simplify repetitive code

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jsceballos@gmail.com

    #1

    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 thisIsTheMethod Name(self):
    self._handlerCl ass.thisIsTheMe thodName()

    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

  • Simon Percivall

    #2
    Re: Help in using introspection to simplify repetitive code


    jsceballos@gmai l.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 thisIsTheMethod Name(self):
    self._handlerCl ass.thisIsTheMe thodName()
    >
    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


    Comment

    • Terry Reedy

      #3
      Re: Help in using introspection to simplify repetitive code


      <jsceballos@gma il.comwrote in message
      news:1156090845 .875872.41220@7 5g2000cwc.googl egroups.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 thisIsTheMethod Name(self):
      self._handlerCl ass.thisIsTheMe thodName()
      Are these parameterless static methods
      or should this be self._handlerCl ass.thisIsTheMe thodName(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__(sel f, name):
      if name in _forwarded: return getattr(self._h andlerClass, name)

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

      Terry Jan Reedy



      Comment

      • jsceballos@gmail.com

        #4
        Re: Help in using introspection to simplify repetitive code

        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

        Comment

        • Fredrik Lundh

          #5
          Re: Help in using introspection to simplify repetitive code

          jsceballos@gmai l.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,"fo o"); f())


          </F>

          Comment

          • Tim N. van der Leeuw

            #6
            Re: Help in using introspection to simplify repetitive code


            Fredrik Lundh wrote:
            jsceballos@gmai l.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,"fo o"); 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 RequestCalculat ions(object):
            def __init__(self, request):
            self.serviceTyp e, self.facade =
            makeMessageFaca deInstance(requ est)
            return

            def __getattr__(sel f, name):
            return getattr(self.fa cade, name)

            (rest of the code omitted)

            Cheers,

            --Tim

            Comment

            • Bruno Desthuilliers

              #7
              Re: Help in using introspection to simplify repetitive code

              jsceballos@gmai l.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.

              Comment

              Working...