Generate function calls tree from multiple Python files

青春壹個敷衍的年華 提交于 2019-12-06 16:57:18

问题


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.

Example of a file (MainModule.py):

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

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).

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.

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.

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?


回答1:


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).

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.

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



来源:https://stackoverflow.com/questions/27269604/generate-function-calls-tree-from-multiple-python-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!