美化打印python stdlib `ast.parse`的输出。
项目描述
astpretty
美化打印python stdlib ast.parse
的输出。
astpretty旨在替代ast.dump
。
安装
pip install astpretty
用法
astpretty
提供两个API函数
astpretty.pprint(node, indent=FOUR_SPACE_INDENT, show_offsets=True)
打印ast节点的表示。
>>> astpretty.pprint(ast.parse('if x == y: y += 4').body[0])
If(
lineno=1,
col_offset=0,
test=Compare(
lineno=1,
col_offset=3,
left=Name(lineno=1, col_offset=3, id='x', ctx=Load()),
ops=[Eq()],
comparators=[Name(lineno=1, col_offset=8, id='y', ctx=Load())],
),
body=[
AugAssign(
lineno=1,
col_offset=11,
target=Name(lineno=1, col_offset=11, id='y', ctx=Store()),
op=Add(),
value=Num(lineno=1, col_offset=16, n=4),
),
],
orelse=[],
)
indent
允许控制缩进字符串
>>> astpretty.pprint(ast.parse('if x == y: y += 4').body[0], indent=' ')
If(
lineno=1,
col_offset=0,
test=Compare(
lineno=1,
col_offset=3,
left=Name(lineno=1, col_offset=3, id='x', ctx=Load()),
ops=[Eq()],
comparators=[Name(lineno=1, col_offset=8, id='y', ctx=Load())],
),
body=[
AugAssign(
lineno=1,
col_offset=11,
target=Name(lineno=1, col_offset=11, id='y', ctx=Store()),
op=Add(),
value=Num(lineno=1, col_offset=16, n=4),
),
],
orelse=[],
)
show_offsets
控制输出是否包括行/列信息
>>> astpretty.pprint(ast.parse('x += 5').body[0], show_offsets=False)
AugAssign(
target=Name(id='x', ctx=Store()),
op=Add(),
value=Num(n=5),
)
astpretty.pformat(node, indent=FOUR_SPACE_INDENT, show_offsets=True)
返回ast节点的字符串表示。
参数与astpretty.pprint
相同。
>>> astpretty.pformat(ast.parse('if x == y: y += 4').body[0])
"If(\n lineno=1,\n col_offset=0,\n test=Compare(\n lineno=1,\n col_offset=3,\n left=Name(lineno=1, col_offset=3, id='x', ctx=Load()),\n ops=[Eq()],\n comparators=[Name(lineno=1, col_offset=8, id='y', ctx=Load())],\n ),\n body=[\n AugAssign(\n lineno=1,\n col_offset=11,\n target=Name(lineno=1, col_offset=11, id='y', ctx=Store()),\n op=Add(),\n value=Num(lineno=1, col_offset=16, n=4),\n ),\n ],\n orelse=[],\n)"
与stdlib ast.dump
的比较
>>> print(ast.dump(ast.parse('if x == y: y += 4').body[0]))
If(test=Compare(left=Name(id='x', ctx=Load()), ops=[Eq()], comparators=[Name(id='y', ctx=Load())]), body=[AugAssign(target=Name(id='y', ctx=Store()), op=Add(), value=Num(n=4))], orelse=[])
typed-ast
支持
astpretty
与typed-ast兼容!
为了与typed-ast
一起使用,请确保已安装typed-ast
,一种方便的方法是通过astpretty
的typed
额外功能来实现
pip install astpretty[typed]
上述API与由typed_ast
提供的ast
模块的返回值同样兼容
>>> import astpretty
>>> from typed_ast import ast3
>>> astpretty.pprint(ast3.parse('x = 4 # type: int'))
Module(
body=[
Assign(
lineno=1,
col_offset=0,
targets=[Name(lineno=1, col_offset=0, id='x', ctx=Store())],
value=Num(lineno=1, col_offset=4, n=4),
type_comment='int',
),
],
type_ignores=[],
)
安装了typed-ast
后,命令行界面增加了用于使用替代ast解析器的--typed-27
和--typed-3
选项
$ astpretty --typed-3 t.py
Module(
body=[
Assign(
lineno=1,
col_offset=0,
targets=[Name(lineno=1, col_offset=0, id='x', ctx=Store())],
value=Num(lineno=1, col_offset=4, n=4),
type_comment='int',
),
],
type_ignores=[],
)
项目详情
关闭
astpretty-3.0.0.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | b08c95f32e5994454ea99882ff3c4a0afc8254c38998a0ed4b479dba448dc581 |
|
MD5 | 6af044f722f2f23de86d927e5ddf09ba |
|
BLAKE2b-256 | 2db6ffec7edce2a8315ef1acd4ae2e334ba428db2b2e8d7577a9aaf1e434034d |
关闭
astpretty-3.0.0-py2.py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 15bfd47593667169485a1fa7938b8de9445b11057d6f2b6e214b2f70667f94b6 |
|
MD5 | 66e31d7ab88a11c1f78360d8c6c091e8 |
|
BLAKE2b-256 | 9f0a79fff71a08bc0cc427f0dfbd4cca62b60f7f277aae81b89b79e9b04d526d |