快速全面的python语言解析器。作为codimension项目的一部分编写的这个解析器旨在从python源中提取最多的数据,同时超过现有解析器的速度。
cdmpyparser的Python项目详细描述
CDM pythonparser
cdm-pythonparser project是 Python3(Python2支持有限)扩展模块。模块 提供的函数可以接受带有python代码或字符的文件 缓冲区,解析它并返回代码中的内容:函数, 类、全局变量等
在Codimension Python IDE中使用该模块以结构化的方式显示 任意的python代码和其他一些特性 在其他需要python代码回顾的项目中使用。
python 3安装和构建
github上的master branch 包含Python3的代码(包括3.5/3.6/3.7语法)。
可以使用pip安装模块:
pip install cdmpyparser
您还可以检索完整的源代码 公用事业。为此,您可以执行以下步骤:
git clone https://github.com/SergeySatskiy/cdm-pythonparser.git
cd cdm-pythonparser
make
make check
make localinstall
python 3用法
假设有如下文件~/my-file.py 内容:
#!/bin/env pythonimportsys# global variablea=154classC(BaseClass):"""Class docstring"""@staticmethoddefgetValue(arg):"""Method docstring"""returnarg+154
然后可能会发生以下python会话:
$ python Python 3.6.2 (default, Aug 92017, 11:11:12)[GCC 4.8.5 20150623(Red Hat 4.8.5-11)] on linux Type "help", "copyright", "credits" or "license"for more information. >>> import cdmpyparser >>> c= cdmpyparser.getBriefModuleInfoFromFile('my-file.py') >>> print(c.niceStringify()) Import[2:8:25]: 'sys' Global[5:1:48]: 'a' Class[7:1:7:7:63:7:19]: 'C' Base class: 'BaseClass' Docstring[8]: 'Class docstring' Function[10:5:10:9:129:10:22]: 'getValue' Argument: 'arg' Decorator[9:6:108]: 'staticmethod Docstring[11]: 'Method docstring' >>> len(c.imports) 1 >>> c.imports[0].name 'sys' >>> c.imports[0].line 2 >>> c.imports[0].pos 8
注意:Python3和Python2模块使用不同的名称。Python3 使用cdmpyparser。python 2使用cdmbriefparser。
有关随附的成员,请参见“cdmpyparser.py”文件 与所有已识别的项目。
python 2安装和构建
注意:不再支持python 2版本。会有的 不再发布Python2。
最新的Python2版本是2.0.1。预建模块和源代码 在github的releases区域中可以找到代码:latest Python 2 release 2.0.1。
要从源代码构建Python2模块,请执行以下步骤:
cd wget https://github.com/SergeySatskiy/cdm-pythonparser/archive/v2.0.1.tar.gz gunzip v2.0.1.tar.gz tar -xf v2.0.1.tar cd cdm-pythonparser-2.0.1/ make make localinstall make check
python 2用法
假设有如下文件~/my-file.py 内容:
#!/bin/env pythonimportsys# global variablea=154classC(BaseClass):"""Class docstring"""@staticmethoddefgetValue(arg):"""Method docstring"""returnarg+154
然后可能会发生以下python会话:
$ python Python 2.7.12 (default, Sep 132016, 16:46:03)[GCC 4.8.5 20150623(Red Hat 4.8.5-4)] on linux2 Type "help", "copyright", "credits" or "license"for more information. >>> import cdmbriefparser >>> c= cdmbriefparser.getBriefModuleInfoFromFile('my-file.py') >>> print c.niceStringify() Import[2:8:25]: 'sys' Global[5:1:48]: 'a' Class[7:1:7:7:63:7:19]: 'C' Base class: 'BaseClass' Docstring[8]: 'Class docstring' Function[10:5:10:9:129:10:22]: 'getValue' Argument: 'arg' Decorator[9:6:108]: 'staticmethod Docstring[11]: 'Method docstring' >>> len(c.imports) 1 >>> c.imports[0].name 'sys' >>> c.imports[0].line 2 >>> c.imports[0].pos 8
注意:Python3和Python2模块使用不同的名称。Python3 使用cdmpyparser。python 2使用cdmbriefparser。
有关提供的成员,请参阅“cdmbriefparser.py”文件 以及所有认可的项目。
与标准pyclbr模块的比较
注意:比较结果是为python 2模块提供的。 python 3模块在 同时提取更多信息的性能,因为语法是 扩展到Python3。
下表显示了标准^{tt5}之间的比较$ 模和共维的cdmbriefparser模。
feature | pyclbr | cdmbriefparser |
---|---|---|
Extracting coding string | N | Y |
Extracting module docstring | N | Y |
Extracting global variables | N | Y |
Extracting imports | N | Y |
Extracting top level functions | Y | Y |
Extracting nested functions | N | Y |
Extracting functions arguments | N | Y |
Extracting functions docstrings | N | Y |
Extracting functions decorators | N | Y |
Extracting classes | Y | Y |
Extracting base classes | Y | Y |
Extracting class attributes | N | Y |
Extracting class instance attributes | N | Y |
Extracting class methods | Y | Y |
Extracting class methods arguments | N | Y |
Extracting nested classes | N | Y |
Extracting classes docstrings | N | Y |
Extracting class methods docstrings | N | Y |
Extracting classes decorators | N | Y |
Extracting decorators arguments | N | Y |
Keeping the hierarchy of the classes/func tions of the arbitrary depth | N | Y |
Ability to work with partially syntacticall y correct files | Y (silent) | Y (error messages are provided) |
Ability to parse python code from a file | Y | Y |
Ability to parse python code from memory | N | Y |
Extracting classes and functions with the same names | N | Y |
Supported python version | ANY | Up to 2.7 (series 3 has not been tested) |
Time to process 11365 python files (python 2.7 distribution and some third party packages). | 2 min 37 sec | 24 sec |