Using descriptors to wrap methods

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Edward C. Jones

    Using descriptors to wrap methods

    Here is a stripped-down version of a Python Cookbook recipe. Is there a
    simpler, more Pythonical, natural way of doing this?

    ------
    #! /usr/bin/env python

    # Modified from Python Cookbook entry 91192, "eiffelmeth od" by Andres
    # Tuells. The url is
    # http://aspn.activestate.com/ASPN/Coo...n/Recipe/91192

    class MethodWraper(ob ject):
    def __init__(self, method):
    self.method = method
    def __get__(self, inst, type=None):
    result = wrapper(inst, self.method)
    setattr(inst, self.method.__n ame__, result)
    return result

    class wrapper:
    def __init__(self, inst, method):
    self.instance = inst
    self.method = method

    def __call__(self, *args, **kargs):
    print 'pre'
    result = apply(self.meth od, (self.instance, ) + args, kargs)
    print 'post'
    return result

    def test():
    class C:
    def f(self, arg):
    print 'in f'
    return arg+1
    f = MethodWraper(f)

    c = C()
    print c.f(1)

    if __name__=='__ma in__':
    test()
    ------
  • Duncan Booth

    #2
    Re: Using descriptors to wrap methods

    "Edward C. Jones" <edcjones@erols .com> wrote in
    news:408cfa5a$0 $28920$61fed72c @news.rcn.com:
    [color=blue]
    > Here is a stripped-down version of a Python Cookbook recipe. Is there a
    > simpler, more Pythonical, natural way of doing this?[/color]

    Here's a simpler way of doing the same thing:
    [color=blue][color=green][color=darkred]
    >>> def MethodWrapper(f ):[/color][/color][/color]
    def wrapper(self, *args, **kw):
    print 'pre'
    result = f(self, *args, **kw)
    print 'post'
    return result
    return wrapper
    [color=blue][color=green][color=darkred]
    >>> class C(object):[/color][/color][/color]
    def f(self, arg):
    print 'in f'
    return arg+1
    f = MethodWrapper(f )

    [color=blue][color=green][color=darkred]
    >>> c = C()
    >>> print c.f(1)[/color][/color][/color]
    pre
    in f
    post
    2[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    Or if you want pre and post code customisable you might try:
    [color=blue][color=green][color=darkred]
    >>> def MethodWrapper(f , pre=None, post=None):[/color][/color][/color]
    def wrapper(self, *args, **kw):
    if pre: pre(self, *args, **kw)
    result = f(self, *args, **kw)
    if post: post(self, result, *args, **kw)
    return result
    return wrapper
    [color=blue][color=green][color=darkred]
    >>> class C(object):[/color][/color][/color]
    def pre_f(self, arg):
    print 'pre',arg
    def post_f(self, res, arg):
    print 'post',res,arg
    def f(self, arg):
    print 'in f'
    return arg+1
    f = MethodWrapper(f , pre_f, post_f)


    [color=blue][color=green][color=darkred]
    >>> c = C()
    >>> c.f(1)[/color][/color][/color]
    pre 1
    in f
    post 2 1
    2[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    I don't know if you could call it Pythonic though.

    Comment

    • Sean Ross

      #3
      Re: Using descriptors to wrap methods


      "Edward C. Jones" <edcjones@erols .com> wrote in message
      news:408cfa5a$0 $28920$61fed72c @news.rcn.com.. .[color=blue]
      > Here is a stripped-down version of a Python Cookbook recipe. Is there a
      > simpler, more Pythonical, natural way of doing this?
      >[/color]

      def wrap(f):
      def wrapped(*args, **kwds):
      print 'pre'
      result = f(*args, **kwds)
      print 'post'
      return result
      return wrapped


      Comment

      Working...