My problem is that I use a metaclass to transfer certain class methods to a timer for logging.
For instance:
class MyMeta(type): @staticmethod def time_method(method): def __wrapper(self, *args, **kwargs): start = time.time() result = method(self, *args, **kwargs) finish = time.time() sys.stdout.write('instancemethod %s took %0.3f s.\n' %( method.__name__, (finish - start))) return result return __wrapper def __new__(cls, name, bases, attrs): for attr in ['__init__', 'run']: if not attr in attrs: continue attrs[attr] = cls.time_method(attrs[attr]) return super(MetaBuilderModule, cls).__new__(cls, name, bases, attrs)
The problem that I am facing is that my shell works for each "__init__", although I really want it only for the current module that I am creating. The same goes for any method that time wants. I do not want time to be executed on any inherited methods unless they are overridden.
class MyClass0(object): __metaclass__ = MyMeta def __init__(self): pass def run(self): sys.stdout.write('running') return True class MyClass1(MyClass0): def __init__(self):
I have tried several things, but so far I have not been successful.
source share