Skip to content

Instantly share code, notes, and snippets.

@r3dm1ke
Last active August 6, 2020 15:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save r3dm1ke/95f692b28acc6911d0eed51c2ae1d6c3 to your computer and use it in GitHub Desktop.
Save r3dm1ke/95f692b28acc6911d0eed51c2ae1d6c3 to your computer and use it in GitHub Desktop.
def logging_decorator(func):
def logging_wrapper(*args, **kwargs):
print(f'Before {func.__name__}')
result = func(*args, **kwargs)
print(f'After {func.__name__}')
return result
return logging_wrapper
def log_all_class_methods(cls):
class NewCls(object):
def __init__(self, *args, **kwargs):
self.original = cls(*args, **kwargs)
def __getattribute__(self, s):
try:
x = super(NewCls,self).__getattribute__(s)
except AttributeError:
pass
else:
return x
x = self.original.__getattribute__(s)
if type(x) == type(self.__init__):
return logging_decorator(x)
else:
return x
return NewCls
@log_all_class_methods
class SomeMethods:
def func1(self):
print('func1')
def func2(self):
print('func2')
methods = SomeMethods()
methods.func1()
> Before func1
> func1
> After func1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment