#!/usr/bin/env python
from datetime import *
def trace_it(method):
# retrieve the enclosing class of the method
class_ = method.im_class
# retrieve the method name
name_ = method.__name__
def alias_method(*args, **kwargs):
print "[%s] %s %s srated" % (datetime.today(), class_.__name__, name_)
retval = method(*args, **kwargs)
print "[%s] %s %s finished" % (datetime.today(), class_.__name__, name_)
return retval
# replace the original method with the alias
setattr(class_, name_, alias_method)
class Test1(object):
def foo1(self):
print 'foo1()'
def foo2(self):
print 'foo2()'
trace_it(Test1.foo1)
tc = Test1()
tc.foo1()
tc.foo2()
Comments