-
- def time_this(original_function):
- print("decorating")
- def new_function(*args, **kwargs):
- import datetime
- before = datetime.datetime.now
- x = original_function(*args, **kwargs)
- after = datetime.datetime.now
- print("Elapsed time {0}".format(after-before))
- return x
- return new_function
- def time_all_class_methods(Cls):
- class NewCls(object):
- def __init__(self,*args,**kwargs):
- self.oInstance = Cls(*args,**kwargs)
- def __getattribute__(self,s):
- try:
- x = super(NewCls,self).__getattribute__(s)
- except AttributeError:
- pass
- else:
- return x
- x = self.oInstance.__getattribute__(s)
- if type(x) == type(self.__init__): # it is an instance method
- return time_this(x) # this is equivalent of just decorating the method with time_this
- else:
- return x
- return NewCls
- #@time_all_class_methods
- class Foo(object):
- def a(self):
- print "entering a"
- import time
- time.sleep(3)
- print "exiting a"
- Foo = time_all_class_methods(Foo)
- oF = Foo
- oF.a()
-
- ================
- Error Message::
- File "/home/psychognite/wingProjects/limboCls.py", line 37, in <module>
- oF.a()
-
- AttributeError: 'NoneType' object has no attribute 'a'
-