Skip to content

Commit deeafdf

Browse files
committed
发布1.0.2版本
1 parent ba0021f commit deeafdf

File tree

8 files changed

+175
-3
lines changed

8 files changed

+175
-3
lines changed

Diff for: README.rst

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212

1313
* 中文繁体版PDF下载地址: http://pan.baidu.com/s/1eRjFDaM
1414

15+
-------------------------------------------------------------
16+
17+
如果您认为本书读后收获很大,不妨小额赞助我一下,让我有动力继续翻译高质量的教程。^_^
18+
19+
支付宝账号:[email protected]
20+
1521
--------------------------------------------------------------
1622

1723
++++++++++++++++

Diff for: basic/mycore/iffor.py

+35-1
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,39 @@ def for_demo():
3636
print("ddd", "dafdf", "ccc")
3737

3838

39+
def odd():
40+
print('step 1')
41+
yield 1
42+
print('step 2')
43+
yield(3)
44+
print('step 3')
45+
yield(5)
46+
47+
48+
def triangles():
49+
"""杨辉三角"""
50+
num, lstpre = 1, [1]
51+
yield lstpre
52+
while True:
53+
num += 1
54+
lst = [1] + [lstpre[i] + lstpre[i + 1] for i in range(0, num - 2)] + [1]
55+
yield lst
56+
lstpre = lst
57+
58+
59+
def normalize(name):
60+
return "".join([s.upper() if i == 0 else s.lower() for i,s in enumerate(name)])
61+
62+
63+
def word_to_name(lst):
64+
return list(map(normalize, lst))
65+
66+
from enum import Enum
67+
68+
Month = Enum('Month', ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
69+
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'))
70+
3971
if __name__ == '__main__':
40-
print(fibonacci.__doc__)
72+
# print(word_to_name(['abc', 'aERTadd', 'EEEEFFF']))
73+
for name, member in Month.__members__.items():
74+
print(name, '=>', member, ',', member.value)
File renamed without changes.

Diff for: basic/mycore/prime.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
"""
4+
Topic: 素数生成
5+
Desc : 埃氏筛法算法
6+
"""
7+
8+
9+
def _odd_iter():
10+
'''构造以3开始的奇数序列'''
11+
n = 1
12+
while True:
13+
n += 2
14+
yield n
15+
16+
17+
def _not_divisible(n):
18+
return lambda x: x % n > 0
19+
20+
21+
def primes():
22+
yield 2
23+
24+
it = _odd_iter()
25+
while True:
26+
n = next(it)
27+
yield n
28+
it = filter(_not_divisible(n), it)
29+
30+
for n in primes():
31+
if n < 1000:
32+
print(n, end=' ')
33+
else:
34+
break
35+
36+

Diff for: basic/myoop/classinstance.py

+12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Topic: class和instance的练习
55
Desc :
66
"""
7+
from types import MethodType
78

89

910
class Dog:
@@ -18,6 +19,10 @@ def change_dog():
1819
Dog.kind = 'another'
1920

2021

22+
def set_age(self, age):
23+
print('set age...')
24+
self.age = age
25+
2126
if __name__ == '__main__':
2227
a = Dog('adog')
2328
b = Dog('bdog')
@@ -27,3 +32,10 @@ def change_dog():
2732
print(Dog.kind, a.kind, a.name)
2833
print(Dog.kind, b.kind, b.name)
2934

35+
Dog.set_age = MethodType(set_age, Dog)
36+
# b = Dog('bdog')
37+
b.set_age(111)
38+
39+
40+
41+

Diff for: basic/mythread/mthread.py

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
"""
4+
Topic: 多线程示例程序
5+
Desc :
6+
"""
7+
8+
from multiprocessing import Process
9+
import os
10+
11+
# 子进程要执行的代码
12+
def run_proc(name):
13+
print('Run child process %s (%s)...' % (name, os.getpid()))
14+
15+
if __name__=='__main__':
16+
print('Parent process %s.' % os.getpid())
17+
p = Process(target=run_proc, args=('test',))
18+
print('Child process will start.')
19+
p.start()
20+
p.join()
21+
print('Child process end.')
22+
23+
24+
from multiprocessing import Pool
25+
import os, time, random
26+
27+
def long_time_task(name):
28+
print('Run task %s (%s)...' % (name, os.getpid()))
29+
start = time.time()
30+
time.sleep(random.random() * 3)
31+
end = time.time()
32+
print('Task %s runs %0.2f seconds.' % (name, (end - start)))
33+
34+
if __name__=='__main__':
35+
print('Parent process %s.' % os.getpid())
36+
p = Pool(4)
37+
for i in range(5):
38+
p.apply_async(long_time_task, args=(i,))
39+
print('Waiting for all subprocesses done...')
40+
p.close()
41+
p.join()
42+
print('All subprocesses done.')
43+
44+
45+
from multiprocessing import Process, Queue
46+
import os, time, random
47+
48+
# 写数据进程执行的代码:
49+
def write(q):
50+
print('Process to write: %s' % os.getpid())
51+
for value in ['A', 'B', 'C']:
52+
print('Put %s to queue...' % value)
53+
q.put(value)
54+
time.sleep(random.random())
55+
56+
# 读数据进程执行的代码:
57+
def read(q):
58+
print('Process to read: %s' % os.getpid())
59+
while True:
60+
value = q.get(True)
61+
print('Get %s from queue.' % value)
62+
63+
if __name__=='__main__':
64+
# 父进程创建Queue,并传给各个子进程:
65+
q = Queue()
66+
pw = Process(target=write, args=(q,))
67+
pr = Process(target=read, args=(q,))
68+
# 启动子进程pw,写入:
69+
pw.start()
70+
# 启动子进程pr,读取:
71+
pr.start()
72+
# 等待pw结束:
73+
pw.join()
74+
# pr进程里是死循环,无法等待其结束,只能强行终止:
75+
pr.terminate()
76+

Diff for: source/aboutme.rst

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*关于译者*
66

77
* 姓名: 熊能
8+
* 微信: yidao620
89
910
* 博客: http://yidao620c.github.io/
1011
* GitHub: https://github.com/yidao620c
@@ -17,3 +18,10 @@
1718
https://github.com/yidao620c/python3-cookbook
1819

1920
|
21+
22+
*友情赞助*
23+
24+
如果您认为本书读后收获很大,不妨小额赞助我一下,让我有动力继续翻译高质量的教程。^_^
25+
26+
支付宝账号:[email protected]
27+

Diff for: source/conf.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,9 @@
5151
# built documents.
5252
#
5353
# The short X.Y version.
54-
version = '1.0.0'
54+
version = '1.0.2'
5555
# The full version, including alpha/beta/rc tags.
56-
release = '1.0.0'
56+
release = '1.0.2'
5757

5858
exclude_patterns = []
5959

0 commit comments

Comments
 (0)