Mastering Python-Packt Publishing 2016(读书笔记)第1版(讲解3.5的coroutine、asyncio、metaclass等)

Mastering Python:Master the art of writing beautiful and powerful Python by using all of the features that Python 3.5 offers

目录

  • 1 Get Started
  • 2 Python风格指南
  • 3 容器与集合
  • 4 FP:可读性 vs Brevity
  • 5 Decorators*
  • 6 Generators和Coroutines
  • 7 Async IO
  • 8 元类(略)
  • 9 文档:Sphinx与reST
  • 10 测试与Logging
  • 11 调试
  • 12 性能
  • 13 threading与multiprocessing
  • 14 C/C++扩展
  • 15 Packaging

Get Started

  1. venv?

Python风格指南

  1. PEP20
  2. PEP8

容器与集合

  1. 基本的:list dict set tuple
  2. ChainMap、counter、deque、defaultdict、namedtuple、enum、heapq、bisect

FP:可读性 vs Brevity

  1. {list/dict/set}comp
  2. functools:partial、reduce、wraps
  3. itertools
    • accumulate、chain、combinations/permutations、compress、drop/takeWhile、count、groupby、islice

Decorators*

Generators和Coroutines

  1. def generaor(): .... yield ...
    1. g = generaor()
    2. y = next(g) #对应yeild y的情况
    3. g.send(x) #对应x = (yield)的情况?x = yield "value"(双向通信)?
  2. a, b = itertools.tee(spam_and_eggs())
  3. 从生成器生成:(pipe,级联)
    1. 3.3 yield from(就是3.5的await?)
  4. @contextlib.contextmanager
    1. 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
  5. Priming
    1. g.send实际value之前需要先next(g)或send(None)
  6. 双向管道(这个地方的代码示例有点费解,关键是对yield这个关键词内部作用原理的理解)
  7. 使用`state` *
    1. total += yield total / count ?

Async IO

  1. 老式库:asyncore、gevent、eventlet
  2. 3.5语法:async { def, for, with }, await/wait
  3. 3.4->3.3:asyncio库
    @asyncio.coroutine #这里的装饰器可替换为3.5 async def
    def sleeper(n):
    yield from asyncio.sleep(n) #可使用3.5 await
  4. loop = asyncio.get_event_loop()
    loop.run_until_complete( asyncio.wait( (...) ))
  5. Future
  6. Task
    1. loop.call_soon( loop.create_task, sleeper(1))
    2. 调试:(异步回调栈?)
      for t in asyncio.Task.all_tasks(): t.print_stack()
  7. Process
    1. p = await asyncio.create_subprocess_exec('sleep', '1')
    2. await p.wait()
  8. EventLoop实现:Selector/Proactor(win IOCP)
    1. 3.4 selectors库*(Select, Kqueue, Epoll, Devpoll),例如:
      selector = selectors.SelectSelector()
      loop = asyncio.SelectorEventLoop(selector)
    2. call_{soon, soon_treadsafe, later, at}系列返回一个Handle,可在上面cancel之:
      loop.call_soon_threadsafe( handle.cancel) #?这里handle.cancel到底是什么?似乎内部bind了this为handle?
    3. 2个heaps:_scheduled,_ready
    4. try:
      loop.run_forever() #<-- loop上可触发多个run_xxx
      except KeyboardInterrupt: ...
  9. 编写服务:
    1. async def handle_connection(reader, writer):
      await writer.drain() #等待写操作flush完成?
    2. r, w = await asyncio.open_connection(host=..., port=...)

元类(略)

  1. 从type继承?动态创建Class?

文档:Sphinx与reST

测试与Logging

  1. doctest、py.test、unitest.mock
  2. logging/Logger

调试

性能

  1. py3:str += "hi"
  2. @numba.jit?配合numpy/pandas使用
  3. tracemalloc
  4. memory_profiler
  5. __slots__

threading与multiprocessing

C/C++扩展

  1. ctypes、CFFI、PyObject*

Packaging

  1. setuptools、distutils
  2. Wheels:the new eggs
    1. 包含src和bin,可无需编译器安装到Windows/OSX上

你可能感兴趣的:(Mastering Python-Packt Publishing 2016(读书笔记)第1版(讲解3.5的coroutine、asyncio、metaclass等))