从 Python 中解析 Python 代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1978515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Parsing Python Code From Within Python?
提问by UberJumper
We have an older C++ tool that generates some python code automatically. I tried to slog through the C++ source tool, today and pretty much wanted to shoot my self. The thing is what i want to do, is clean up the source created by the tool and link the classes to our internal documentation system via adding sphinx tags.
我们有一个旧的 C++ 工具,可以自动生成一些 python 代码。今天,我试图通过 C++ 源代码工具艰难地浏览,并且非常想拍摄我自己。事情就是我想要做的,就是清理工具创建的源代码,并通过添加 sphinx 标签将类链接到我们的内部文档系统。
Now what i am wondering is there some sort of wonderful tool for parsing python code within python?
现在我想知道是否有某种很棒的工具可以在 python 中解析 python 代码?
There is alot of stuff like this:
有很多这样的东西:
foo._methods_.append()
Snip 500 lines
foo._methods_.append()
ANy suggestions?
有什么建议?
Basically i have a functional but insanely messy code structure, i basically want to extract certain chunks, move them to their own files. And cleanup all the miscellanous things that get created.
基本上我有一个功能强大但非常混乱的代码结构,我基本上想提取某些块,将它们移动到自己的文件中。并清理所有创建的杂项。
I looked quickyl at both parser and AST but i cannot find any real examples of it being used.
我查看了解析器和 AST 的 quickyl,但我找不到任何使用它的真实示例。
采纳答案by Anurag Uniyal
You may tokenize python code to parse individual tokens using tokenizemodule. e.g. Script to remove Python comments/docstrings
您可以使用tokenize模块对 python 代码进行标记以解析单个标记。例如删除 Python 注释/文档字符串的脚本
or you can use the parsermodule
或者你可以使用解析器模块
or use astmodule
或使用ast模块
回答by Steven
Depending on your needs, you may also want to check out the 2to3library. It was written to automatically facilitate the conversion of Python 2.x apps to Python 3.0, so its main use case is taking one Python source file, performing some transformations on it, and then spitting out the result source file.
根据您的需要,您可能还想查看2to3库。它的编写是为了自动促进 Python 2.x 应用程序到 Python 3.0 的转换,因此它的主要用例是获取一个 Python 源文件,对其执行一些转换,然后吐出结果源文件。
One benefit that lib2to3 has over the ast module is that ast does not preserve whitespace and comments, whereas lib2to3 does. If you're already dealing with autogenerated code this might not be a problem for you.
lib2to3 对 ast 模块的一个好处是 ast 不保留空格和注释,而 lib2to3 保留。如果您已经在处理自动生成的代码,这对您来说可能不是问题。
回答by PaulMcG
Try one of the Python doc-generating utilities to see if this might help you with your overall problem. I've used epydoc with great success.
尝试使用 Python 文档生成实用程序之一,看看这是否可以帮助您解决整体问题。我已经成功地使用了 epydoc。
Or if you can get your boss to part with $200, buy a copy of Enterprise Architect from SparxSystems. It will reverse-engineer your Python code, generate a class diagram, and allow you to click on the class diagram and see the underlying code. Works for many languages besides Python, and an excellent design and documentation utility. (There is a $99 version, but this does not include the code import capability.)
或者,如果您可以让您的老板支付 200 美元,请从 SparxSystems 购买一份 Enterprise Architect。它将对您的 Python 代码进行逆向工程,生成类图,并允许您单击类图并查看底层代码。适用于除 Python 之外的多种语言,以及出色的设计和文档实用程序。(有一个 99 美元的版本,但这不包括代码导入功能。)