Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Magic to display dynamic call graphs of Python function calls

License

NotificationsYou must be signed in to change notification settings

osteele/callgraph

Repository files navigation

Latest PyPI VersionDocumentation StatusLicenseSupported Python Versions

Callgraph is a Python package that defines a decorator, and Jupyter magic,to drawdynamic call graphs of Python function calls.

It’s intended for classroom use, but may also be useful for self-guidedexploration.

The package defines a JupyterIPythonmagic,%callgraph, thatdisplays a call graph within a Jupyter cell:

fromfunctoolsimportlru_cache@lru_cache()deflev(a,b):if""in (a,b):returnlen(a)+len(b)candidates= []ifa[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)returnmin(candidates)%callgraph-w10lev("big","dog");lev("dig","dog")

image0

It also provides a Python decorator,callgraph.decorator, thatinstruments a function to collect call graph information and render theresult.

Jupyter / IPython Usage

$ pip install callgraph

In a Jupyter IPython notebook:

%load_extcallgraphdefnchoosek(n,k):ifk==0:return1ifn==k:return1returnnchoosek(n-1,k-1)+nchoosek(n-1,k)%callgraphnchoosek(4,2)

As an alternative to including%load_ext callgraph in each notebook thatuses%callgraph, you can add the extension to the Notebookconfiguration file in your IPython profile.

Your configuration file is probably called~/.ipython/profile_default/ipython_config.py.(You can runipython profile locate to find it.)Edit this file to include the following line:

c.InteractiveShellApp.extensions = ["callgraph.extension"]

(If your configuration file already includes an uncommented statementc.InteractiveShellApp.extensions = […], edit the list of extensions inthat line to include"callgraph.extension".

Seeextension example notebook for additional examples.

Decorator Usage

$ pip install callgraph
fromfunctoolsimportlru_cacheimportcallgraph.decoratorascallgraph@callgraph()@lru_cache()defnchoosek(n,k):ifk==0:return1ifn==k:return1returnnchoosek(n-1,k-1)+nchoosek(n-1,k)nchoosek(5,2)nchoosek.__callgraph__.view()

See theAPI documentation for additional documentation.

See thedecorator example notebook for additional instructions and examples.

Development

Install dev tools, and set up a Jupyter kernel for the current pythonenviromnent:

$ pip install -r requirements-dev.txt$ python -m ipykernel install --user

Install locally:

flit install --symlink

Acknowledgements

Callgraph uses the Pythongraphviz package. Python graphviz usestheGraphviz package.

License

MIT

About

Magic to display dynamic call graphs of Python function calls

Topics

Resources

License

Stars

Watchers

Forks


[8]ページ先頭

©2009-2025 Movatter.jp