-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathcombine_decorators.py
67 lines (43 loc) · 1018 Bytes
/
combine_decorators.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
62
63
64
65
66
67
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "ipetrash"
import functools
def makebold(func):
@functools.wraps(func)
def wrapped(*args, **kwargs):
return "<b>" + func(*args, **kwargs) + "</b>"
return wrapped
def makeitalic(func):
@functools.wraps(func)
def wrapped(*args, **kwargs):
return "<i>" + func(*args, **kwargs) + "</i>"
return wrapped
def upper(func):
@functools.wraps(func)
def wrapped(*args, **kwargs):
return func(*args, **kwargs).upper()
return wrapped
def composed(*decs):
def deco(f):
for dec in reversed(decs):
f = dec(f)
return f
return deco
def multi(func):
return composed(
makebold,
makeitalic,
upper,
)(func)
@makebold
@makeitalic
@upper
def hello(text):
return text
@multi
def hello_2(text):
return text
print(hello("Hello World!"))
# <b><i>HELLO WORLD!</i></b>
print(hello_2("Hello World!"))
# <b><i>HELLO WORLD!</i></b>