S
shailesh
Hello,
I'm trying to write a class decorator which takes a function as an
argument and wraps instancemethods of the class with the function.
After reading a few examples on ActiveState and this blog post <http://
wordaligned.org/articles/echo>, my first attempt goes something like
this:
def tracing_wrapper(fn):
import functools
@functools.wraps(fn)
def wrapped(*args, **kwargs):
print "invoking %s(%s, %s)" % (fn.__name__, args, kwargs)
return fn(*args, **kwargs)
return wrapped
def factory(wrapper_fn):
def class_wrapper(klass):
import inspect
for _, method in inspect.getmembers(klass, inspect.ismethod):
setattr(klass, method.__name__, wrapper_fn(method))
for _, fn in inspect.getmembers(klass, inspect.isfunction):
setattr(klass, fn.__name__, staticmethod(wrapper_fn(fn)))
return klass
return class_wrapper
@factory(tracing_wrapper)
class MyObject(object):
def a(self):
print "a"
@factory(tracing_wrapper)
class MyDict(dict):
pass
invoking a((<__main__.MyObject object at 0xb7bb5bcc>,), {})
a
Naturally, when wrapping the built-in dict type, I don't get the
expected results.
The reason as far as I understand is that the methods on the built-in
dict are not of MethodType or FunctionType so they are not included in
the result of the inspect.getmembers call and are not wrapped.
Here I'm stuck, unsure how to fix this. Could anyone suggest a way to
wrap the methods of the built-in types? This recipe <http://
code.activestate.com/recipes/252151/> for generalized proxies seems
promising, but I'm not sure how to adapt it for use here.
Thanks in advance.
I'm trying to write a class decorator which takes a function as an
argument and wraps instancemethods of the class with the function.
After reading a few examples on ActiveState and this blog post <http://
wordaligned.org/articles/echo>, my first attempt goes something like
this:
def tracing_wrapper(fn):
import functools
@functools.wraps(fn)
def wrapped(*args, **kwargs):
print "invoking %s(%s, %s)" % (fn.__name__, args, kwargs)
return fn(*args, **kwargs)
return wrapped
def factory(wrapper_fn):
def class_wrapper(klass):
import inspect
for _, method in inspect.getmembers(klass, inspect.ismethod):
setattr(klass, method.__name__, wrapper_fn(method))
for _, fn in inspect.getmembers(klass, inspect.isfunction):
setattr(klass, fn.__name__, staticmethod(wrapper_fn(fn)))
return klass
return class_wrapper
@factory(tracing_wrapper)
class MyObject(object):
def a(self):
print "a"
@factory(tracing_wrapper)
class MyDict(dict):
pass
invoking a((<__main__.MyObject object at 0xb7bb5bcc>,), {})
a
Naturally, when wrapping the built-in dict type, I don't get the
expected results.
1md = MyDict()
md['hello'] = 1
md.get('hello')
The reason as far as I understand is that the methods on the built-in
dict are not of MethodType or FunctionType so they are not included in
the result of the inspect.getmembers call and are not wrapped.
Here I'm stuck, unsure how to fix this. Could anyone suggest a way to
wrap the methods of the built-in types? This recipe <http://
code.activestate.com/recipes/252151/> for generalized proxies seems
promising, but I'm not sure how to adapt it for use here.
Thanks in advance.