python树函数,从多个Python文件生成函数调用树

本文探讨如何在Python项目中理解和分析代码,特别是寻找文件内定义及调用的函数。作者提到,虽然某些IDE如WingIDE可以显示函数定义,但无法直接展示跨文件的调用关系。建议使用动态分析工具如pycallgraph来生成调用图,这对于在大量文件中重构代码非常有用。在进行重构之前,确保有足够的单元测试覆盖也是关键。
摘要由CSDN通过智能技术生成

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?

解决方案

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?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值