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

编程入门 行业动态 更新时间:2024-10-25 03:29:24
本文介绍了有什么办法可以从树的转储文件中还原AST树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

c是经过解析的python代码树:

c is a parsed tree of a python code:

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

d是tree-c的转储文件

d is the dump file of tree-c

d=ast.dump(c)

d的内容为以下字符串:

the content of d is the following string:

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。

I've looked around the net to see if I could find a method to use the content of d, to revert back to the tree c.

有什么建议吗?

谢谢

推荐答案

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

You can use eval to get the ast object from the string:

ast_obj = eval(d)

然后,存在第三种可以转换回源代码的第三方库(使用AST,是否有一个可用的库来获取源代码?, Python ast(抽象语法树):获取子节点的源字符串)

Then, there exist various third party libraries that can convert back to source (Given an AST, is there a working library for getting the source?, Python ast (Abstract Syntax Trees): get back source string of subnode)

更多推荐

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

本文发布于:2023-10-27 22:49:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1534774.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么办法可以   储文   AST

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!