-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecorator.py
More file actions
78 lines (51 loc) · 1.55 KB
/
decorator.py
File metadata and controls
78 lines (51 loc) · 1.55 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""Um decorator pega uma função adiciona algumas funcionalidades e em seguida a retorna
"""
# function decorator
def decorator(func):
def inner(data):
print(f'decorator: Olá {data}')
return func('Luiz Filipy')
return inner
@decorator
def test(data):
print(f'test: {data}')
test('Mundo')
def decorator2(**kwargs):
def inner(func):
print(f'decorator2: {kwargs}')
return func(12345, **kwargs)
return inner
@decorator2(id='luiz')
def test2(num, **kwargs):
print(f'test2: {kwargs}\t{num}\n')
# class decorator
class MeuDecorator:
def __init__(self, function):
self.function = function
def __call__(self, *args, **kwargs):
print(args)
self.function(*args, **kwargs)
@MeuDecorator
def test3(n):
print(n)
test3('Filipy')
class MeuDecorator2:
def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs
print(f'__init__ args: {args}\tkwargs: {kwargs}')
def __call__(self, *args, **kwargs):
print(f'__call__ args: {args}\tkwargs: {kwargs}')
if self.kwargs["login_requerido"]:
print('logado')
kwargs['user'] = {'id': 123, 'name': 'luiz'}
else:
print('não logado')
kwargs['user'] = None
self.function = args[0](kwargs, {'response': None})
@MeuDecorator2(path='/', login_requerido=True)
def test3(req, res):
print(f'req: {req}\tres: {res}\n')
@MeuDecorator2(path='/', login_requerido=False)
def test4(req, res):
print(f'req: {req}\tres: {res}')