注:STL是cpp语言中Stand Template Library的缩写,为避免歧义,此处使用STDL作为Standard Library的缩写
- link
- link
- 设置断点
breakpoint()import pdb; pdb.set_trace()
- REPL模式下执行pdb
pdb.run('hf0') - 命令行下执行pdb
python -m pdb draft00.py - 在pdb模式下的快捷键
h,help: 打印所有快捷键help xxx:查询某一快捷键c: continuen: next lineq: quitwherewhatis xxx
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()')- link
python setup.py developto do the unittest- 偏见
- 使用
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
- 使用
- high level interface:
TemporaryFile() NamedTemporaryFile() TemporaryDirectory() SpooledTemporaryFile()- context manager
- low level interface:
mkstemp()mkdtemp()- cleanup manually
gettempdir() gettempprefix()- recommend to use keyword arguments
gzip,bz2,lzma
- link
- 模块
cPickle已废弃 - 禁止对不信任的数据使用
pickle.load() - 特性
- 将对象以文件的形式存放在磁盘
- pickle序列化后的数据,可读性差,人一般无法识别
- 常用示例
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), 字节流恢复对象
pickle对于大型的数据结构比如使用array或numpy模块创建的二进制数组编码效率不高。建议先在一个文件中将其保存为数组数据块或使用更高级的标准编码方式如HDF5 (需要第三方库的支持)- 由于
pickle是Python特有的并且附着在源码上,所有如果需要长期存储数据的时候不应该选用它。 例如,如果源码变动了,你所有的存储数据可能会被破坏并且变得不可读取。 坦白来讲,对于在数据库和存档文件中存储数据时,你最好使用更加标准的数据编码格式如XML,CSV或JSON。 这些编码格式更标准,可以被不同的语言支持,并且也能很好的适应源码变更 pickle.pickler()pickler.clear_memo()
with open(file,'rb') as fid:
xxx = pickle.load(fid, encoding='bytes')- link
osprovides a portable way of using operating system dependent functionalitysysprovides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter
- link
- 多线程涉及竞争-互斥的问题,在理解之前不推荐使用
- 常用函数
threading.active_count() threading.enumerate() threading.current_thread() - threading不一定有效率:GIL (global interpreter lock)
- global interpreter lock (GIL)
- python的确存在GIL,且对于某些运算
- GIL对文件读取的性能影响很大
- GIL对numpy性能影响可忽略不计,对
np.fromfile影响很大 - numpy因为多线程/多进程同时进行导致的性能降低并没有因为是多线程而降低更多
- numpy多进程依旧会降低性能,与OpenMP相比如何呢?
- link
if __name_=='__main__'是必须的- 进程锁
multiprocessing.Lock() - 三种启动方法:
spawn fork forserver - 队列Queue是进程线程安全的
- 进程间共享状态:
multipoocessing.Value multiprocessing.Array,服务进程multiprocessing.Manager() - 同步方式:
multiprocessing.Queues,multiprocessing.Pipes,multiprocessing.Lock - TODO:管理器,代理,自定义管理器
- link
- level
debug info warning error exception critical, defaultwarning
- link
- signal不能用于线程间通信
- link
- link
- 概念
socket.AF_INET:IPv4协议socket.AF_INET6:IPv6协议socket.SOCK_STREAM:面向流的TCP协议socket.SOCK_DGRAM:面向无连接的UDP协议- Inter Process Communication (IPC)
- 常见服务端口
- http服务80
- https服务443
- SMTP邮件服务25
- FTP服务21
- link
- 要求
python37
- link
- 对于未知内容的变量,禁止用string的方式嵌入到SQL命令中,见SQL injection attack
connection与cursor的区别,见stackoverflow
- link
- 当且仅当所有进程抛错中断时会导致主进程中断进而打印出错信息(或者进程结束),否则表现为挂起
- link
- documentation
- common weakness enumeration (CWE) link
- common vulnerabilities and exposures (CVE) link
- concept
- secure hash algorithm (message digest algorithm)
- SHAKE variable length digests
- file hashing
- keyed hashing
- randomized hashing
secrets
- @2015,
32bytes/256bitsof randomness is sufficient for the typical use (against brute force attack) - timing attacks:
secrets.compare_digest,hmac.compare_digest - password: salted, hashed