Decorators and Jupyter IPython magic to display a dynamic call graph.
Project description
Callgraph is a Python package that uses GraphViz to draw dynamic call graphs of Python function calls.
It’s intended for classroom use, but may also be useful for self-guided exploration.
The package defines a Jupyter IPython magic, %callgraph, that displays a call graph within a Jupyter cell:
from functools import lru_cache
@lru_cache()
def lev(a, b):
if "" in (a, b):
return len(a) + len(b)
candidates = []
if a[0] == b[0]:
candidates.append(lev(a[1:], b[1:]))
else:
candidates.append(lev(a[1:], b[1:]) + 1)
candidates.append(lev(a, b[1:]) + 1)
candidates.append(lev(a[1:], b) + 1)
return min(candidates)
%callgraph -w10 lev("big", "dog"); lev("dig", "dog")
It also provides a Python decorator callgraph.decorator, that instruments a function to collect call graph information and render the result.
Jupyter Usage
$ pip install callgraph
In a Jupyter notebook:
%load_ext callgraph
def nchoosek(n, k):
if k == 0:
return 1
if n == k:
return 1
return nchoosek(n - 1, k - 1) + nchoosek(n - 1, k)
%callgraph nchoosek(4, 2)
See https://github.com/osteele/callgraph/blob/master/examples/callgraph-magic-examples.ipynb for additional instructions and examples.
Decorator Usage
$ pip install callgraph
from functools import lru_cache
import callgraph.decorator as callgraph
@callgraph()
@lru_cache()
def nchoosek(n, k):
if k == 0:
return 1
if n == k:
return 1
return nchoosek(n - 1, k - 1) + nchoosek(n - 1, k)
nchoosek(5, 2)
nchoosek.__callgraph__.view()
See https://github.com/osteele/callgraph/blob/master/examples/callgraph-decorator-examples.ipynb for additional instructions and examples.
Development
Install dev tools, and set up a Jupyter kernel for the current python enviromnent:
$ pip install flit
$ pip install ipykernel
$ python -m ipykernel install --user
Install locally:
flit install --symlink
Acknowledgements
Callgraph uses the Python graphviz package. Python graphviz uses the Graphviz package.
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for callgraph-0.1.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8a47ed144a912b287a628fc92e0a3de5a93d475f65a83946ba48dc16fbec6568 |
|
MD5 | 5239756be5a6e9ae807e8db06f482b3f |
|
BLAKE2b-256 | feeac64e444975e4b6eb75fb9a54d159e00be29f3cf7b416ba40c8b31a4db62e |