用于管理Python位置表(co_linetable)的库
项目描述
关于linetable
linetable是一个库,用于解析和生成Python代码对象中的co_linetable属性。
基于https://github.com/python/cpython/blob/main/Objects/locations.md
安装
可以从PyPI安装linetable
pip install linetable
对大多数用户来说应该能正常工作
使用
可以使用linetable.parse_linetable解析现有的linetable
>>> def testfunc(): ... x = 3 ... y = x + 1 ... return y >>> list(linetable.parse_linetable(testfunc.__code__.co_linetable)) [ (1, 1, 1, 0, 0), (1, 2, 2, 6, 7), (1, 2, 2, 2, 3), (1, 3, 3, 6, 7), (1, 3, 3, 10, 11), (2, 3, 3, 6, 11), (1, 3, 3, 2, 3), (1, 4, 4, 9, 10), (1, 4, 4, 2, 10), ]
如果您希望以dis.Positions对象的格式输出,您可以从生成值创建它们
>>> [dis.Positions(*e[1:]) for e in linetable.parse_linetable(testfunc.__code__.co_linetable)] [Positions(lineno=1, end_lineno=1, col_offset=0, end_col_offset=0), Positions(lineno=2, end_lineno=2, col_offset=8, end_col_offset=9), Positions(lineno=2, end_lineno=2, col_offset=4, end_col_offset=5), Positions(lineno=3, end_lineno=3, col_offset=8, end_col_offset=9), Positions(lineno=3, end_lineno=3, col_offset=12, end_col_offset=13), Positions(lineno=3, end_lineno=3, col_offset=8, end_col_offset=13), Positions(lineno=3, end_lineno=3, col_offset=4, end_col_offset=5), Positions(lineno=4, end_lineno=4, col_offset=11, end_col_offset=12), Positions(lineno=4, end_lineno=4, col_offset=4, end_col_offset=12)]
如果您有linetable,可以使用linetable.generate_linetable生成二进制编码版本
>>> lt = [ ... (1, 1, 1, 0, 0), ... (1, 2, 2, 6, 7), ... (1, 2, 2, 2, 3), ... (1, 3, 3, 6, 7), ... (1, 3, 3, 10, 11), ... (2, 3, 3, 6, 11), ... (1, 3, 3, 2, 3), ... (1, 4, 4, 9, 10), ... (1, 4, 4, 2, 10), ... ] >>> linetable.generate_linetable(lt) b"\x80\x00\xd8\x06\x07\x80!\xd8\x06\x07\x88!\x81e\x80!\xd8\t\n\x80("
项目详情
关闭
linetable-0.0.3.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 571305913fe024924b414f3fe4c84c7848446c6546f3dab2a508c6788d79f1ed |
|
MD5 | 1560e04e0435c7f5d3a75a13e9371ccb |
|
BLAKE2b-256 | 439d4eb3859e9fbea37e4647e109f2813895353e8160adae3a8e7db051035568 |