Last active
August 6, 2020 15:21
-
-
Save r3dm1ke/95f692b28acc6911d0eed51c2ae1d6c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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