Is a free application that automatically generates a directed acyclic graph (DAG) for all the functions in a code? I am particularly interested in one that builds the DAG for the Python code edited in Pycharm.
-
What do the edges of such a graph represent? – Code-Apprentice Apr 22 '17 at 22:13
-
@Code-Apprentice: The directed edge from node a to b represents function a calling function b. – Hans Apr 22 '17 at 22:59
-
2I do not think such a graph will always be a DAG in the general case. It isn't difficult to imagine a cyclical graph for function calls. – Code-Apprentice Apr 22 '17 at 23:02
-
@Code-Apprentice: I see what you mean. You are referring to a recursive function call involving, say, two functions, right? – Hans Apr 22 '17 at 23:05
-
Yes, that is an example that comes to mind: `a()` calls `b()` and `b()` calls `a()`. I was also trying to think if there were any non-mutually recursive examples, but the more I think about it, it seems that a cycle in the graph implies recursion. – Code-Apprentice Apr 22 '17 at 23:08
-
@Code-Apprentice: It has to be recursive, almost by definition. – Hans Apr 22 '17 at 23:14
-
Are you looking for a call graph generator such as [pycallgraph](https://github.com/gak/pycallgraph)? – MB-F Apr 24 '17 at 13:45
1 Answers
For static code inspection: in PyCharm, select the root folder of your project and press Ctrl+Alt+Shift+u for a class diagram.
Edit:
Using Alt+F7 you can see the usages of a selected function in other functions and methods. But this is also not a graph for all the functions in a project.
What you are searching for is (in general) not feasible with static code analysis, since the overall call graph is highly dependent on the conditional jumps (if
clauses) during program execution. There is no one true D(A)G.
Imagine variants of a DAG for this code snipped for example:
def baz():
import datetime
if datetime.datetime.now().second % 2:
foo()
else:
bar()
Neither baz -> foo
, nor baz -> bar
would be the only possible solution.
However:
(1) your debugger might track every code that was executed in one specific run and present a graph of functions used after the actual run, including their relationship towards each other.
(2) There is no static code analysis tool for the general case. For code written for ETL worflow frameworks like luigi, pinball, airflow, dagobah, dask, and theano (comparisons 1, 2) it is possible to visualize the code execution before starting the application. For more information, see the awesome-pipeline repository.
-
Yes, it is for code inspection. Is the Strg key equivalent to the Ctrl key? If so, I pressed them. There is no reaction. Is there a pull down menu, or right mouse key alternative? – Hans Apr 22 '17 at 23:03
-
@hans Try `Ctrl-Shift-A` and then type "graph". This will show you a list of all commands which match the typed search phrase. – Code-Apprentice Apr 22 '17 at 23:04
-
A window does pop up. But this seems like a search function. This can be accomplished by Ctrl-Shift-F, which I am already using. This does show the neighbouring nodes of the function under consideration, but it does not present a ADG that shows the call relations beyond the immediate neighbouring nodes, not to mention those amongst all the functions (nodes). – Hans Apr 22 '17 at 23:08
-
Thank you for your expanded answer. Your point regarding the dependency on conditional jumps of the feasibility of static code analysis is right on the point. Not only the DAG's are different for different conditions, the union of those ADG's would produce cycles. Regarding PyCharm, looking into the class diagram link you provided, I find "Model dependency diagrams are available for: Django models, Google App Engine models, SQLAlchemy. Does it support conventional Python code? – Hans Apr 25 '17 at 00:05
-
Yes, it does support "normal" Python code, as long as it is structured in classes. There is a [tutorial](http://confluence.jetbrains.com/display/PYH/Working+with+UML+class+diagrams+in+PyCharm) on the PyCharm website. – mab Apr 25 '17 at 12:02