-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathyield_ope.py
More file actions
70 lines (53 loc) · 1.07 KB
/
Copy pathyield_ope.py
File metadata and controls
70 lines (53 loc) · 1.07 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
# coding:utf-8
def f_135():
yield 1
yield 3
yield 5
def demo_1():
for val in f_135():
print val
# 1 3 5
generator = f_135()
print next(generator)
# 1
print next(generator)
# 3
print next(generator)
# 5
def fibonacci(n):
cur = 1
pre = 0
count = 0
while count < n:
yield cur
cur, pre = cur + pre, cur
count += 1
def demo_fib():
ge_fib = fibonacci(10)
for i in ge_fib:
print i, ", "
# 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55
ge_fib = fibonacci(5)
print next(ge_fib)
# 1
print next(ge_fib)
# 1
def read_file(f_path='__init__.py'):
# read 60 bytes once
bt_once = 60
with open(f_path, 'rb') as fmp3:
data = fmp3.read(bt_once)
while data:
yield data
data = fmp3.read(bt_once)
def demo_read_file():
for txt in read_file():
print txt
# # coding:utf-8
# if __name__ == '__main__':
# pass
if __name__ == '__main__':
# demo_1()
# demo_fib()
demo_read_file()
pass