diff --git a/README.rst b/README.rst index 748c9038..f324c63e 100644 --- a/README.rst +++ b/README.rst @@ -12,6 +12,12 @@ * 中文繁体版PDF下载地址: http://pan.baidu.com/s/1eRjFDaM +------------------------------------------------------------- + +如果您认为本书读后收获很大,不妨小额赞助我一下,让我有动力继续翻译高质量的教程。^_^ + +支付宝账号:yidao620@126.com + -------------------------------------------------------------- ++++++++++++++++ diff --git a/basic/mycore/iffor.py b/basic/mycore/iffor.py index 232c96e7..9d17d3a5 100644 --- a/basic/mycore/iffor.py +++ b/basic/mycore/iffor.py @@ -36,5 +36,39 @@ def for_demo(): print("ddd", "dafdf", "ccc") +def odd(): + print('step 1') + yield 1 + print('step 2') + yield(3) + print('step 3') + yield(5) + + +def triangles(): + """杨辉三角""" + num, lstpre = 1, [1] + yield lstpre + while True: + num += 1 + lst = [1] + [lstpre[i] + lstpre[i + 1] for i in range(0, num - 2)] + [1] + yield lst + lstpre = lst + + +def normalize(name): + return "".join([s.upper() if i == 0 else s.lower() for i,s in enumerate(name)]) + + +def word_to_name(lst): + return list(map(normalize, lst)) + +from enum import Enum + +Month = Enum('Month', ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', + 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')) + if __name__ == '__main__': - print(fibonacci.__doc__) \ No newline at end of file + # print(word_to_name(['abc', 'aERTadd', 'EEEEFFF'])) + for name, member in Month.__members__.items(): + print(name, '=>', member, ',', member.value) diff --git a/basic/mycore/collections.py b/basic/mycore/mcollections.py similarity index 100% rename from basic/mycore/collections.py rename to basic/mycore/mcollections.py diff --git a/basic/mycore/prime.py b/basic/mycore/prime.py new file mode 100644 index 00000000..425853b6 --- /dev/null +++ b/basic/mycore/prime.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- +""" +Topic: 素数生成 +Desc : 埃氏筛法算法 +""" + + +def _odd_iter(): + '''构造以3开始的奇数序列''' + n = 1 + while True: + n += 2 + yield n + + +def _not_divisible(n): + return lambda x: x % n > 0 + + +def primes(): + yield 2 + + it = _odd_iter() + while True: + n = next(it) + yield n + it = filter(_not_divisible(n), it) + +for n in primes(): + if n < 1000: + print(n, end=' ') + else: + break + + diff --git a/basic/myoop/classinstance.py b/basic/myoop/classinstance.py index 099322b0..9f56f346 100644 --- a/basic/myoop/classinstance.py +++ b/basic/myoop/classinstance.py @@ -4,6 +4,7 @@ Topic: class和instance的练习 Desc : """ +from types import MethodType class Dog: @@ -18,6 +19,10 @@ def change_dog(): Dog.kind = 'another' +def set_age(self, age): + print('set age...') + self.age = age + if __name__ == '__main__': a = Dog('adog') b = Dog('bdog') @@ -27,3 +32,10 @@ def change_dog(): print(Dog.kind, a.kind, a.name) print(Dog.kind, b.kind, b.name) + Dog.set_age = MethodType(set_age, Dog) + # b = Dog('bdog') + b.set_age(111) + + + + diff --git a/basic/mythread/mthread.py b/basic/mythread/mthread.py new file mode 100644 index 00000000..eb90336f --- /dev/null +++ b/basic/mythread/mthread.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- +""" +Topic: 多线程示例程序 +Desc : +""" + +from multiprocessing import Process +import os + +# 子进程要执行的代码 +def run_proc(name): + print('Run child process %s (%s)...' % (name, os.getpid())) + +if __name__=='__main__': + print('Parent process %s.' % os.getpid()) + p = Process(target=run_proc, args=('test',)) + print('Child process will start.') + p.start() + p.join() + print('Child process end.') + + +from multiprocessing import Pool +import os, time, random + +def long_time_task(name): + print('Run task %s (%s)...' % (name, os.getpid())) + start = time.time() + time.sleep(random.random() * 3) + end = time.time() + print('Task %s runs %0.2f seconds.' % (name, (end - start))) + +if __name__=='__main__': + print('Parent process %s.' % os.getpid()) + p = Pool(4) + for i in range(5): + p.apply_async(long_time_task, args=(i,)) + print('Waiting for all subprocesses done...') + p.close() + p.join() + print('All subprocesses done.') + + +from multiprocessing import Process, Queue +import os, time, random + +# 写数据进程执行的代码: +def write(q): + print('Process to write: %s' % os.getpid()) + for value in ['A', 'B', 'C']: + print('Put %s to queue...' % value) + q.put(value) + time.sleep(random.random()) + +# 读数据进程执行的代码: +def read(q): + print('Process to read: %s' % os.getpid()) + while True: + value = q.get(True) + print('Get %s from queue.' % value) + +if __name__=='__main__': + # 父进程创建Queue,并传给各个子进程: + q = Queue() + pw = Process(target=write, args=(q,)) + pr = Process(target=read, args=(q,)) + # 启动子进程pw,写入: + pw.start() + # 启动子进程pr,读取: + pr.start() + # 等待pw结束: + pw.join() + # pr进程里是死循环,无法等待其结束,只能强行终止: + pr.terminate() + diff --git a/source/aboutme.rst b/source/aboutme.rst index ced6c687..e860b789 100644 --- a/source/aboutme.rst +++ b/source/aboutme.rst @@ -5,6 +5,7 @@ *关于译者* * 姓名: 熊能 +* 微信: yidao620 * Email: yidao620@gmail.com * 博客: http://yidao620c.github.io/ * GitHub: https://github.com/yidao620c @@ -17,3 +18,10 @@ https://github.com/yidao620c/python3-cookbook | + +*友情赞助* + +如果您认为本书读后收获很大,不妨小额赞助我一下,让我有动力继续翻译高质量的教程。^_^ + +支付宝账号:yidao620@126.com + diff --git a/source/conf.py b/source/conf.py index b4e722bf..f77ba0c1 100644 --- a/source/conf.py +++ b/source/conf.py @@ -51,9 +51,9 @@ # built documents. # # The short X.Y version. -version = '1.0.0' +version = '1.0.2' # The full version, including alpha/beta/rc tags. -release = '1.0.0' +release = '1.0.2' exclude_patterns = []