class DBLogWrapper2(BaseWrapper):

    def __init__(self, instance, methods):
        super(DBLogWrapper, self).__init__(instance)
self._methods = methods

def __getattr__(self, attr):
    if attr not in methods:
        return getattr(self._instance, attr)

def f(*args, **kwargs):
    return self.timed_method(getattr(self._item, attr), *args, **kwargs)
return f

def timed_method(method, *args, **kwargs):
    begin = datetime.datetime.now()
return_val = method(*args, **kwargs)
end = datetime.datetime.now()
trace = traceback.format_stack()
db_log(name, begin, end, info={'args': args, 'kwargs': kwargs, 'trace': trace})
return return_val
How to wrap python instance methods at runtime