Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Using descriptors to wrap methods

Reply
Thread Tools

Using descriptors to wrap methods

 
 
Edward C. Jones
Guest
Posts: n/a
 
      04-26-2004
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, "eiffelmethod" by Andres
# Tuells. The url is
# http://aspn.activestate.com/ASPN/Coo...n/Recipe/91192

class MethodWraper(object):
def __init__(self, method):
self.method = method
def __get__(self, inst, type=None):
result = wrapper(inst, self.method)
setattr(inst, self.method.__name__, 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.method, (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__=='__main__':
test()
------
 
Reply With Quote
 
 
 
 
Duncan Booth
Guest
Posts: n/a
 
      04-26-2004
"Edward C. Jones" <> wrote in
news:408cfa5a$0$28920$:

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


Here's a simpler way of doing the same thing:

>>> def MethodWrapper(f):

def wrapper(self, *args, **kw):
print 'pre'
result = f(self, *args, **kw)
print 'post'
return result
return wrapper

>>> class C(object):

def f(self, arg):
print 'in f'
return arg+1
f = MethodWrapper(f)


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

pre
in f
post
2
>>>


Or if you want pre and post code customisable you might try:

>>> def MethodWrapper(f, pre=None, post=None):

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

>>> class C(object):

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)



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

pre 1
in f
post 2 1
2
>>>


I don't know if you could call it Pythonic though.
 
Reply With Quote
 
 
 
 
Sean Ross
Guest
Posts: n/a
 
      04-26-2004

"Edward C. Jones" <> wrote in message
news:408cfa5a$0$28920$...
> Here is a stripped-down version of a Python Cookbook recipe. Is there a
> simpler, more Pythonical, natural way of doing this?
>


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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to find the class methods of a class, just like'methods' finds the instance methods? Kenneth McDonald Ruby 5 09-26-2008 03:09 PM
To wrap or not to wrap? Aaron Fude Java 12 05-10-2008 06:33 PM
Wrap computer components in bubble wrap? Ickshka Computer Support 7 05-05-2006 05:54 PM
Text::Wrap::wrap difference Art Werschulz Perl Misc 1 09-25-2003 06:15 PM
Text::Wrap::wrap difference Art Werschulz Perl Misc 0 09-22-2003 02:36 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57