python - 有什么方法可以从树的转储文件中恢复 AST 树吗?

标签 python abstract-syntax-tree dump

c 是 python 代码的解析树:

c=ast.parse(''' 对于 y 中的 x: 打印(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。

有什么建议吗?

谢谢

最佳答案

您可以使用eval从字符串中获取ast对象:

ast_obj = eval(d)

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

关于python - 有什么方法可以从树的转储文件中恢复 AST 树吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51366393/

相关文章:

python - 转换 Jython 的 source/ast

clang - 创建 LLVM IR 的方法

load - CouchDB 转储到文件并从文件加载

svn转储->通过Windows Powershell重命名路径->与格式错误的转储文件头作斗争

python - 我怎样才能在我的模板中获取 JSONfield 的值

clang - 在 clang 中,如何获取 FieldDecl 的基础类型

python - 在线python代码运行时错误,离线工作

windbg - 为什么windbg中的!dumpvc可以dump出数组元素?

python - 将 _constraints 更改为 _sql_constraints - Odoo v8 到 Odoo v10 社区

python - 当窗口调整大小时调整 Tkinter 小部件和 Canvas 的大小?