从多个Python文件生成函数调用树 [英] Generate function calls tree from multiple Python files

查看:224
本文介绍了从多个Python文件生成函数调用树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经继承了一堆Python文件,并且正在寻找一种方法,通过收集有关那些文件中已定义和调用的所有函数的信息来了解它们中发生的事情.

I have inherited a bunch of Python files and looking for a way to get an understanding of what is going on within them by collecting information about all the functions that have been defined and called within those files.

文件示例(MainModule.py):

Example of a file (MainModule.py):

Import module1
Import module2
Import module3
 
...
Def func1():
...
Def func2():
...
 
result1 = Module1.funcX(args)
result2 = Module2.funcY(args)
...

通过查看此文件,我希望IDE报告该文件中定义了哪些功能(这是我使用Wing IDE可以执行的操作),还报告了哪些功能(在同一文件内的.py文件中定义了哪些功能).文件夹或项目).显然,我不想报告Python系统功能的使用(例如print()等),而只是报告那些在我项目中的文件中定义的功能(可以是文件夹,并不重要).

By looking into this file, I want IDE to report what functions have been defined within this file (this is something I can do with Wing IDE), but also what functions (that were defined within .py files located within the same folder or a project) were called. Obviosly, I don't want to report use of Python system functions (such as print(), etc.), but only those functions that were defined within files within my project (could be a folder, doesn't really matter).

因此,在此示例中,我想得到如下内容:在MainFunction.py文件中,分别从Module1.py文件和Module2.py文件调用了funcX和funcY函数.我将使用此信息在Visio(或其他软件)中构建参考架构,该代码将在重构代码时使用.

So in this example, I'd like to get something like this: in file MainFunction.py functions funcX and funcY were called from Module1.py file and Module2.py file respectively. I will use this information to build a reference schema in Visio (or some other piece of software) which I will use while refactoring the code.

我知道在WingIDE中使用查找使用点"和转到"定义,但是读取20个文件以查找功能会很麻烦.

I am aware of using Find points of use and Goto definition within WingIDE, but reading through 20 files looking for functions would be too cumbersome.

我已经使用了PyScripter和WingIDE Pro,但是似乎这些IDE缺少此功能.哪种IDE支持这种操作?如果没有,还有其他简单的方法可以实现这一目标吗?

I have used PyScripter and WingIDE Pro, but it seems as those IDEs lack this functionality. What IDE has support for this kind of operation? If none, is there any other straightforward way to achieve this?

推荐答案

通过静态分析生成动态语言的调用图实际上是不可能的(即使对于简单示例而言,可能是近似的).因此,通常使用诸如pycallgraph( https://pypi之类的东西在运行时动态地完成此操作. python.org/pypi/pycallgraph ).

Generating call graphs of dynamic languages through static analysis is practically impossible (even if an approximation is possible for simple examples). It is therefore typically done dynamically at run-time, through use of something like pycallgraph (https://pypi.python.org/pypi/pycallgraph).

因此,良好的Python代码单元测试覆盖至关重要.如果缺少单元测试,那么在进行重构之前,提高单元测试的覆盖率可能是非常有价值的一步.

Good unit test coverage of the Python code is therefore of paramount importance. If unit tests are lacking, improving unit test coverage is likely a very valuable step prior to tackling refactoring.

这是先前的StackOverflow问题,也涉及到此问题:

Here is a previous StackOverflow question that also touches on this: Build a Call graph in python including modules and functions?

这篇关于从多个Python文件生成函数调用树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆