-
Notifications
You must be signed in to change notification settings - Fork 0
/
Meta_pro_4.py
61 lines (56 loc) · 2.04 KB
/
Meta_pro_4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Here, first we will unwrap the static and class method, then decorate the original function then re-wrap them with those decorators back.
from functools import wraps
from pprint import pprint
def func_logger(fn):
@wraps(fn)
def inner(*args, **kwargs):
result = fn(*args, **kwargs)
print(f'Log: {fn.__qualname__}({args}, {kwargs}) = {result}')
return result
return inner
def class_logger(cls):
for name, obj in vars(cls).items():
if callable(obj):
print('Decorating callable', cls, name)
original_func = obj
decorated_func = func_logger(original_func)
setattr(cls, name, decorated_func)
elif isinstance(obj, staticmethod):
print('Decorating static method', cls, name)
original_func = obj.__func__
decorated_func = func_logger(original_func)
method = staticmethod(decorated_func)
setattr(cls, name, method)
elif isinstance(obj, classmethod):
print('Decorating class method', cls, name)
original_func = obj.__func__
decorated_func = func_logger(original_func)
method = classmethod(decorated_func)
setattr(cls, name, method)
elif isinstance(obj, property):
print('Decorating property', cls, name)
if obj.fget:
obj = obj.getter(func_logger(obj.fget))
if obj.fset:
obj = obj.setter(func_logger(obj.fset))
if obj.fdel:
obj = obj.deleter(func_logger(obj.fdel))
setattr(cls, name, obj)
return cls
@class_logger
class Person:
def __init__(self, name):
self._name = name
@staticmethod
def staticmethod(a,b):
print('static_method called....', a,b)
@classmethod
def class_method(cls, a, b):
print('class_method called....',a,b)
def instance_method(self, a, b):
print('instance_method called...',a,b)
@property
def name(self):
return self._name
p = Person('David')
print(p)