34

I have inherited a huge codebase that I need to make some small changes into. I was wondering if there are utilities that would parse python code and give dependencies between functions, as in if I make changes to a function I want to be sure that I dont break other functions, so if I could see in a graph like diagram it would make my life easier.

Jean-Paul Calderone
  • 47,755
  • 6
  • 94
  • 122
anijhaw
  • 8,954
  • 7
  • 35
  • 36
  • See also http://stackoverflow.com/questions/798389/python-tool-that-builds-a-dependency-diagram-for-methods-of-a-class – Vanuan Feb 22 '13 at 18:22

1 Answers1

35
  • Usually "dependency" is defined for module / package import.
  • What you are looking for is a visualizing call flow.

  • You can still not guarantee that you will not break functionality :)

  • My experience and solution:

    Many a times, I found the call flow data overwhelming and the diagram too complex. So what i usually do is trace call flow partially for the function, I am interested in.

    This is done by utilizing the sys.settrace(...) function. After generating the call flows as textual data, I generate a call graph using graphviz.

[Edit: based on comments]

Then my piecemeal solution works better. Just insert the code and use the decorator on a function that you want to trace. You will see gaps where deferred comes into picture but that can be worked out. You will not get the complete picture directly.

I have been trying to do that and made a few post that works on that understanding.

pyfunc
  • 65,343
  • 15
  • 148
  • 136
  • 5
    +1 for last point about guarantees. I would be very impressed if it could follow much of the type of code that uses a lot of HOF and other dynamic features. I imagine that it would be useful mainly for evaluating the work of programmers that are either not smart enough to use such features or smart enough not to. – aaronasterling Nov 12 '10 at 01:16
  • I am not looking for a silver bullet. What I am looking for is more to give me a general idea. – anijhaw Nov 12 '10 at 01:42
  • @anijhaw : Then you got it. :) pycallgraph should do your job. I have also provided the solution which I use more frequently to learn what going on. This can be made difficult though. Try tracing calls in a twisted application. – pyfunc Nov 12 '10 at 01:46
  • it is a twisted application :( – anijhaw Nov 12 '10 at 02:04
  • your posts are great, I am trying your solution will keep you posted on how it turns out :) – anijhaw Nov 12 '10 at 13:59
  • The `pycallgraph` module has now been moved I think. Follow [this](https://pycallgraph.readthedocs.io/en/master/) link for documentation and it had become an unmaintained one. – Sivaram May 25 '23 at 17:38