Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Standard Library

:STL是cpp语言中Stand Template Library的缩写,为避免歧义,此处使用STDL作为Standard Library的缩写

  1. link

pdb

  1. link
  2. 设置断点
    • breakpoint()
    • import pdb; pdb.set_trace()
  3. REPL模式下执行pdb pdb.run('hf0')
  4. 命令行下执行pdb python -m pdb draft00.py
  5. 在pdb模式下的快捷键
    • h, help: 打印所有快捷键
    • help xxx:查询某一快捷键
    • c: continue
    • n: next line
    • q: quit
    • where
    • whatis xxx
  6. pdb.pm(): post-mortem debugging验尸。。。。

REPL模式下执行pdb

import pdb
def hf0():
    a = 'hello'
    print(a)
    breakpoint() #seems cannot change the value
    print(a)
pdb.run('hf0()')

unittest

  1. link
  2. python setup.py develop to do the unittest
  3. 偏见
    • 使用pytest替代unittest
    • do NOT distribute unittest files within the module itself. It often increases complexity for the users and many test suites often require additional dependencies and runtime contexts

tempfile

  1. high level interface: TemporaryFile() NamedTemporaryFile() TemporaryDirectory() SpooledTemporaryFile()
    • context manager
  2. low level interface: mkstemp() mkdtemp()
    • cleanup manually
  3. gettempdir() gettempprefix()
  4. recommend to use keyword arguments

tarfile

  1. gzip, bz2, lzma

pickle

  1. link
  2. 模块cPickle已废弃
  3. 禁止对不信任的数据使用pickle.load()
  4. 特性
    • 将对象以文件的形式存放在磁盘
    • pickle序列化后的数据,可读性差,人一般无法识别
  5. 常用示例
    • with open(filename, 'wb') as fid: pickle.dump(xxx, fid), 对象序列化到文件,调用__getstate__()获取序列化对象
    • with open(filename, 'rb') as fid: xxx = pickle.load(fid), 文件中恢复对象,调用__setstate__()反序列化,反序列对象前要让python能够找到类的定义
    • s = pickle.dumps(xxx), 对象序列化到字符串
    • xxx = pickle.loads(s), 字节流恢复对象
  6. pickle对于大型的数据结构比如使用arraynumpy模块创建的二进制数组编码效率不高。建议先在一个文件中将其保存为数组数据块或使用更高级的标准编码方式如HDF5 (需要第三方库的支持)
  7. 由于pickle是Python特有的并且附着在源码上,所有如果需要长期存储数据的时候不应该选用它。 例如,如果源码变动了,你所有的存储数据可能会被破坏并且变得不可读取。 坦白来讲,对于在数据库和存档文件中存储数据时,你最好使用更加标准的数据编码格式如XML,CSV或JSON。 这些编码格式更标准,可以被不同的语言支持,并且也能很好的适应源码变更
  8. pickle.pickler()
  9. pickler.clear_memo()
with open(file,'rb') as fid:
    xxx = pickle.load(fid, encoding='bytes')

os and sys

  1. link
  2. os provides a portable way of using operating system dependent functionality
  3. sys provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter

threading

  1. link
  2. 多线程涉及竞争-互斥的问题,在理解之前不推荐使用
  3. 常用函数threading.active_count() threading.enumerate() threading.current_thread()
  4. threading不一定有效率:GIL (global interpreter lock)
  5. global interpreter lock (GIL)
    • python的确存在GIL,且对于某些运算
    • GIL对文件读取的性能影响很大
    • GIL对numpy性能影响可忽略不计,对np.fromfile影响很大
    • numpy因为多线程/多进程同时进行导致的性能降低并没有因为是多线程而降低更多
    • numpy多进程依旧会降低性能,与OpenMP相比如何呢?

multiprocessing

  1. link
  2. if __name_=='__main__'是必须的
  3. 进程锁 multiprocessing.Lock()
  4. 三种启动方法:spawn fork forserver
  5. 队列Queue是进程线程安全的
  6. 进程间共享状态:multipoocessing.Value multiprocessing.Array,服务进程multiprocessing.Manager()
  7. 同步方式:multiprocessing.Queues, multiprocessing.Pipes, multiprocessing.Lock
  8. TODO:管理器,代理,自定义管理器

argparse

  1. link

logging

  1. link
  2. level debug info warning error exception critical, default warning

signal

  1. link
  2. signal不能用于线程间通信

ipaddress

  1. link

struct

  1. link

socket

  1. link
  2. 概念
    • socket.AF_INET:IPv4协议
    • socket.AF_INET6:IPv6协议
    • socket.SOCK_STREAM:面向流的TCP协议
    • socket.SOCK_DGRAM:面向无连接的UDP协议
    • Inter Process Communication (IPC)
  3. 常见服务端口
    • http服务80
    • https服务443
    • SMTP邮件服务25
    • FTP服务21

selectors

  1. link

asyncio

  1. link
  2. 要求python37

sqlite3

  1. link
  2. 对于未知内容的变量,禁止用string的方式嵌入到SQL命令中,见SQL injection attack
  3. connectioncursor的区别,见stackoverflow

concurrent.futures

  1. link
  2. 当且仅当所有进程抛错中断时会导致主进程中断进而打印出错信息(或者进程结束),否则表现为挂起

hashlib,hmac,secrets

  1. link
  2. concept
    • secure hash algorithm (message digest algorithm)
    • SHAKE variable length digests
  3. file hashing
  4. keyed hashing
  5. randomized hashing

secrets

  1. @2015, 32bytes/256bits of randomness is sufficient for the typical use (against brute force attack)
  2. timing attacks: secrets.compare_digest, hmac.compare_digest
  3. password: salted, hashed