-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_study_20191229.py
More file actions
346 lines (254 loc) · 5.36 KB
/
python_study_20191229.py
File metadata and controls
346 lines (254 loc) · 5.36 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 29 17:21:00 2019
@author: ecupl
"""
#生成器
g = (x*x for x in range(10))
#next(g)
for n in g:
print(n)
def myYield(n):
while n>0:
print("开始生成。。。")
yield n
print("完成一次。。。")
n -= 1
if __name__ == "__main__":
a = myYield(3)
a.__next__()
a.__next__()
def fib(max):
n, a, b = 0, 0, 1
while n < max:
yield b
a, b = b, a+b
n += 1
return "well done"
f = fib(5)
print(f)
print(list(f))
f = fib(5)
print(f.__next__())
print(f.__next__())
print("something else")
print(f.__next__())
print(f.__next__())
print(f.__next__())
def iter_st(x):
for xi in range(x):
yield xi
re = iter_st(10)
for t in re:
print(t)
#杨辉三角
def triangles():
result = [1]
while True:
yield result
result = [1] + [result[i] + result[i+1] for i in range(len(result)-1)] + [1]
n = 0
results = []
for t in triangles():
results.append(t)
n = n + 1
if n == 10:
break
results
def odd():
print('step 1')
yield 1
print('step 2')
yield(3)
print('step 3')
yield(5)
o = odd()
next(o)
next(o)
next(o)
next(o)
#修饰器
def makebold(fn):
def wrapped():
return "<b>" + fn() + "</b>"
return wrapped
def makeitalic(fn):
def wrapped():
return "<i>" + fn() + "</i>"
return wrapped
@makebold
@makeitalic
def hello():
return "hello world"
hello()
#案例1
import time
def foo():
print("in foo()")
def timeit(func):
start = time.clock()
func()
end = time.clock()
print("Time Elapsed:",end-start)
timeit(foo)
#案例1:另一种写法
import time
def foo():
print('in foo()')
def timeit(func):
def wrapper1():
start = time.clock()
func()
end =time.clock()
print('Time Elapsed:', end - start)
return wrapper1
foo = timeit(foo) #可以直接写成@timeit + foo定义,python的"语法糖"
foo()
#或者写成@形式
@timeit
def foo1():
print('in foo()')
foo1()
#有趣的汉堡了解顺序
def bread(func):
def wrapper():
print("</''' '''\>")
func()
print("<\_______/>")
return wrapper
def ingredients(func):
def wrapper():
print("#tomatoes#")
func()
print("~salad~")
return wrapper
def sandwich(food="--ham--"):
print(food)
#1
sandwich()
#2
sandwich2 = bread(ingredients(sandwich))
sandwich2()
#3
@bread
@ingredients
def sandwich(food="--ham--"):
print(food)
sandwich()
#对有参函数进行修饰
##一个参数:如果原函数有参数,那闭包函数必须保持参数个数一致,并且将参数传递给原方法
def w1(func):
def wrapper(name):
print("this is wrapper head")
func(name)
print("this is wrapper tail")
return wrapper
@w1
def hello(name):
print("hello, "+name)
hello("AoDaCat")
##多个参数:
def w2(func):
def wrapper(*args, **kwargs):
print("this is wrapper head")
func(*args, **kwargs)
print("this is wrapper tail")
return wrapper
@w2
def hello(name1, name2, name3):
print("hello, "+name1+" and "+name2+" and "+name2)
hello("AoDaCat", "aodadou", "xiaotudou")
#有返回值的函数
def w3(func):
def wrapper(name):
print("this is wrapper head")
hello = func(name)
print("this is wrapper tail")
return hello #要把值传回去
return wrapper
@w3
def hello(name):
print("hello, "+name)
return "I am fine!"
hello("AoDaCat")
#有参数的修饰器
def func_args(pre="xiaoqiang"):
def w4(func):
def inner(name):
print("日志记录人:", pre)
hello = func(name)
return hello
return inner
return w4
@func_args()
def hello(name):
print("hello, "+name)
return "I am fine!"
hello("AoDaCat")
@func_args('wangcai')
def hello(name):
print("hello, "+name)
return "I am fine!"
hello("AoDaCat")
#通用修饰器
def w_test(func):
def wrapper(*args, **kwargs):
ret1 = func(*args, **kwargs)
return ret1
return wrapper
#1
@w_test
def test():
print('test1')
test()
#2
@w_test
def test():
print("test2")
return "Python"
test()
#3
@w_test
def test(a):
print("test+"+a)
return ("hello, "+a)
test("AoDaCat")
#类修饰器
class Test(object):
def __call__(self, *args, **kwargs):
print('call called')
t = Test()
print(t())
#直接对类进行修饰
class Test(object):
def __init__(self, func):
print('test init')
print('func name is %s ' % func.__name__)
self.abc = func
def __call__(self, *args, **kwargs):
print('this is wrapper')
print(*args[0])
self.abc()
@Test
def test():
print('this is test func')
test('a')
######测试程序运行时长
import time, functools
def log(text):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
start = time.clock()
print("%s call %s()"%(text, func.__name__))
ret = func(*args, **kwargs)
end = time.clock()
print("函数运行时长:%fs"%(end-start))
return ret
return wrapper
return decorator
@log("execute")
def now():
print("2019-12-31")
now()
now.__name__