Skip to content

Commit

Permalink
发布1.0.2版本
Browse files Browse the repository at this point in the history
  • Loading branch information
yidao620c committed Jan 21, 2016
1 parent ba0021f commit deeafdf
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@

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

-------------------------------------------------------------

如果您认为本书读后收获很大,不妨小额赞助我一下,让我有动力继续翻译高质量的教程。^_^

支付宝账号:yidao620@126.com

--------------------------------------------------------------

++++++++++++++++
Expand Down
36 changes: 35 additions & 1 deletion basic/mycore/iffor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
# print(word_to_name(['abc', 'aERTadd', 'EEEEFFF']))
for name, member in Month.__members__.items():
print(name, '=>', member, ',', member.value)
File renamed without changes.
36 changes: 36 additions & 0 deletions basic/mycore/prime.py
Original file line number Diff line number Diff line change
@@ -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


12 changes: 12 additions & 0 deletions basic/myoop/classinstance.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Topic: class和instance的练习
Desc :
"""
from types import MethodType


class Dog:
Expand All @@ -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')
Expand All @@ -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)




76 changes: 76 additions & 0 deletions basic/mythread/mthread.py
Original file line number Diff line number Diff line change
@@ -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()

8 changes: 8 additions & 0 deletions source/aboutme.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*关于译者*

* 姓名: 熊能
* 微信: yidao620
* Email: yidao620@gmail.com
* 博客: http://yidao620c.github.io/
* GitHub: https://github.com/yidao620c
Expand All @@ -17,3 +18,10 @@
https://github.com/yidao620c/python3-cookbook

|
*友情赞助*

如果您认为本书读后收获很大,不妨小额赞助我一下,让我有动力继续翻译高质量的教程。^_^

支付宝账号:yidao620@126.com

4 changes: 2 additions & 2 deletions source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand Down

0 comments on commit deeafdf

Please sign in to comment.