-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecorator_partial_fun.py
148 lines (125 loc) · 3.1 KB
/
decorator_partial_fun.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#decorator and partial function
import functools
from collections import Iterable
def log(content = None):
if content == None:
def Decorator(func):
@functools.wraps(func)
def wrapper(*args,**kw):
print('begin call')
print('function name: %s' % func.__name__)
func(*args,**kw)
print('end call')
return wrapper
return Decorator
else:
def Decorator(func):
@functools.wraps(func)
def wrapper(*args,**kw):
print('begin call')
print('function name: %s log %s' % (func.__name__, content))
func(*args,**kw)
print('end call')
return wrapper
return Decorator
def log1(content = None):
def Decorator(func):
@functools.wraps(func)
def wrapper(*args,**kw):
print('begin call')
if content==None:
print('function name: %s' % func.__name__)
else:
print('function name: %s log %s' % (func.__name__, content))
fn=func(*args,**kw)
print('end call')
return fn
return wrapper
return Decorator
def log3(func):
if not isinstance(text,Iterable):
def Decorator(func):
@functools.wraps(func)
def wrapper(*args,**kw):
print('begin call')
print('function name: %s' % func.__name__)
func(*args,**kw)
print('end call')
return wrapper
return Decorator
else:
def Decorator(func):
@functools.wraps(func)
def wrapper(*args,**kw):
print('begin..')
print('function name: %s log: %s' % (func.__name__,text))
func(*args,**kw)
print('end...')
return wrapper
return Decorator
def log4(inargs):
#无参数类型
# now = log(now)
if isinstance(inargs,Iterable):
@functools.wraps(inargs)
def Decorator_1(*args,**kw):
print('begin call')
print('function name: %s' % inargs.__name__)
inargs(*args,**kw)
print('end call')
return Decorator_1
#有参数类型
else:
# now = log('execute')(now)
def Decorator_2(func):
def wrapper(*args,**kw):
print('begin..')
print('function name: %s log: %s' % (func.__name__,inargs))
func(*args,**kw)
print('end...')
return wrapper
return Decorator_2
import functools
from collections import Iterable
def log5(inArgs):
#带参数的函数
def Decorator_withPara(fn):
@functools.wraps(fn)
def wrapper(*args,**kw):
print('decorator_function with parameter')
print('args:',inArgs)
fn(*args,**kw)
return wrapper
#无参数的函数
@functools.wraps(inArgs)
def Decorator_withoutPara(*args,**kw):
print('decorator_function without parameter')
print('args:',args)
inArgs(*args,**kw)
if isinstance(inArgs,Iterable):
return Decorator_withPara
else:
return Decorator_withoutPara
@log5('22222')
def now1():
print('2015-09-19-now1')
@log5
def now2():
print('2015-09-19-now2')
now1()
print('function name:',now1.__name__)
now2()
print('function name:',now2.__name__)
#偏函数12
def int2(x,base=2):
v=int(x,base)
print(v)
# 23 22222222222222222222222222222222222222222222222222222222
int2('100000')
int2('10101010')
import functools
int22= functools.partial(int,base=2)
v=int22('100000')
print(v)
v=int22('1010101')
print(v)