Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build a Call graph in python including modules and functions? [closed]

I have a bunch of scripts to perform a task. And I really need to know the call graph of the project because it is very confusing. I am not able to execute the code because it needs extra HW and SW to do so. However, I need to understand the logic behind it. So, I need to know if there is a tool (which do not require any python file execution) that can build a call graph using the modules instead of the trace or python parser. I have such tools for C but not for python.
Thank you.

like image 897
JohnnyDH Avatar asked Dec 20 '12 00:12

JohnnyDH


People also ask

What is a call graph in Python?

A call graph is a directed graph that displays which function calls which. It is created from the data of a Python profiler such as cProfile. Since such a graph proved helpful in a project I'm working on, I created a package called project_graph, which builds such a call graph for any provided python script.

How do you make a directed graph in Python?

Add the nodes from any container (a list, dict, set or even the lines from a file or the nodes from another graph). In addition to strings and integers any hashable Python object (except None) can represent a node, e.g. a customized node object, or even another Graph. Edges: G can also be grown by adding edges.

How do you represent a graph in Python?

In Python, we can represent graphs like this using a two-dimensional array. And a two-dimensional array can be achieved in Python by creating a list of lists. The indices of the list will represent the nodes. So if we want to create a graph having 5 nodes, we will represent the nodes from 0 to 4.


2 Answers

The best tool I've found is called pyan, and was originally written by Edmund Horner, improved by him, and then given colorization and other features by Juha Jeronen. That version has useful commandline options:

Usage: pyan.py FILENAME... [--dot|--tgf]  Analyse one or more Python source files and generate an approximate call graph of the modules, classes and functions within them.  Options:   -h, --help           show this help message and exit   --dot                output in GraphViz dot format   --tgf                output in Trivial Graph Format   -v, --verbose        verbose output   -d, --defines        add edges for 'defines' relationships [default]   -n, --no-defines     do not add edges for 'defines' relationships   -u, --uses           add edges for 'uses' relationships [default]   -N, --no-uses        do not add edges for 'uses' relationships   -c, --colored        color nodes according to namespace [dot only]   -g, --grouped        group nodes (create subgraphs) according to namespace                        [dot only]   -e, --nested-groups  create nested groups (subgraphs) for nested namespaces                        (implies -g) [dot only] 

Here's the result of running pyan.py --dot -c -e pyan.py | fdp -Tpng:

pyan's output on itself

Edmund Horner's original code is now best found in his github repository, and somebody has also made a repository with both versions, from where you can download Juha Jeronen's version. I've made a clean version combining their contributions into my own repository just for pyan, since both repositories have lots of other software.

like image 147
David Fraser Avatar answered Oct 07 '22 23:10

David Fraser


You might want to check out pycallgraph:

pycallgraph

Also in this link a more manual approach is described:

generating-call-graphs-for-understanding-and-refactoring-python-code

like image 28
vkontori Avatar answered Oct 08 '22 00:10

vkontori