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

目录

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上
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值