Mastering Python:Master the art of writing beautiful and powerful Python by using all of the features that Python 3.5 offers
目录
Get Started
- venv?
Python风格指南
- PEP20
- PEP8
容器与集合
- 基本的:list dict set tuple
- ChainMap、counter、deque、defaultdict、namedtuple、enum、heapq、bisect
FP:可读性 vs Brevity
- {list/dict/set}comp
- functools:partial、reduce、wraps
- itertools
- accumulate、chain、combinations/permutations、compress、drop/takeWhile、count、groupby、islice
Decorators*
Generators和Coroutines
- def generaor(): .... yield ...
- g = generaor()
- y = next(g) #对应yeild y的情况
- g.send(x) #对应x = (yield)的情况?x = yield "value"(双向通信)?
- a, b = itertools.tee(spam_and_eggs())
- 从生成器生成:(pipe,级联)
- 3.3 yield from(就是3.5的await?)
- @contextlib.contextmanager
- with contextlib.ExitStack() as stack:
- fh = stack.enter_context(open('stdout.txt', 'w'))
- stack.enter_context(contextlib.redirect_stdout(fh))
- ...
- close_handlers = stack.pop_all().close
- with contextlib.ExitStack() as stack:
- Priming
- g.send实际value之前需要先next(g)或send(None)
- 双向管道(这个地方的代码示例有点费解,关键是对yield这个关键词内部作用原理的理解)
- 使用`state` *
- total += yield total / count ?
Async IO
- 老式库:asyncore、gevent、eventlet
- 3.5语法:async { def, for, with }, await/wait
- 3.4->3.3:asyncio库
- @asyncio.coroutine #这里的装饰器可替换为3.5 async def
-
def sleeper(n):
- yield from asyncio.sleep(n) #可使用3.5 await
- loop = asyncio.get_event_loop()
- loop.run_until_complete( asyncio.wait( (...) ))
- Future
- Task
- loop.call_soon( loop.create_task, sleeper(1))
- 调试:(异步回调栈?)
- for t in asyncio.Task.all_tasks(): t.print_stack()
- Process
- p = await asyncio.create_subprocess_exec('sleep', '1')
- await p.wait()
- EventLoop实现:Selector/Proactor(win IOCP)
- 3.4 selectors库*(Select, Kqueue, Epoll, Devpoll),例如:
- selector = selectors.SelectSelector()
- loop = asyncio.SelectorEventLoop(selector)
- call_{soon, soon_treadsafe, later, at}系列返回一个Handle,可在上面cancel之:
- loop.call_soon_threadsafe( handle.cancel) #?这里handle.cancel到底是什么?似乎内部bind了this为handle?
- 2个heaps:_scheduled,_ready
- try:
-
- loop.run_forever() #<-- loop上可触发多个run_xxx
- except KeyboardInterrupt: ...
-
- 3.4 selectors库*(Select, Kqueue, Epoll, Devpoll),例如:
- 编写服务:
- async def handle_connection(reader, writer):
- await writer.drain() #等待写操作flush完成?
- r, w = await asyncio.open_connection(host=..., port=...)
- async def handle_connection(reader, writer):
元类(略)
- 从type继承?动态创建Class?
文档:Sphinx与reST
测试与Logging
- doctest、py.test、unitest.mock
- logging/Logger
调试
性能
- py3:str += "hi"
- @numba.jit?配合numpy/pandas使用
- tracemalloc
- memory_profiler
__slots__
threading与multiprocessing
略
C/C++扩展
- ctypes、CFFI、PyObject*
Packaging
- setuptools、distutils
- Wheels:the new eggs
- 包含src和bin,可无需编译器安装到Windows/OSX上