有什么方法可以从树的转储文件中还原AST树?

时间:2018-07-16 16:34:08

标签: python abstract-syntax-tree dump

c是python代码的解析树:

c=ast.parse(''' for x in y: print(x) ''')

d是tree-c的转储文件

d=ast.dump(c)

d的内容为以下字符串:

Module(
    body=[For(target=Name(id='x', ctx=Store()), iter=Name(id='y', ctx=Load()),
    body=[Expr(value=Call(func=Name(id='print', ctx=Load()), 
    args=[Name(id='x', ctx=Load())], keywords=[]))], orelse=[])])

我环顾四周,看看是否可以找到一种方法来使用d的内容来还原为树c。

有什么建议吗?

谢谢

1 个答案:

答案 0 :(得分:0)

您可以使用date -> datetime从字符串中获取eval对象:

ast

然后,存在各种可以转换回源代码(Given an AST, is there a working library for getting the source?Python ast (Abstract Syntax Trees): get back source string of subnode)的第三方库