这个包定义了decorators和ipython magic来显示动态调用图。
callgraph的Python项目详细描述
callgraph是一个python包,它定义了一个decorator和jupyter magic, 绘制python函数调用的dynamic call graphs。
它是为课堂使用而设计的,但也可以用于自我引导 探索。
包定义了一个jupyterIPythonmagic,%callgraph,它 在Jupyter单元格中显示调用图:
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")
它还提供了一个python修饰符callgraph.decorator,该修饰符 插入一个函数来收集调用图信息并呈现 结果。
jupyter/ipython用法
$ pip install callgraph
在jupyter ipython笔记本中:
%load_extcallgraphdefnchoosek(n,k):ifk==0:return1ifn==k:return1returnnchoosek(n-1,k-1)+nchoosek(n-1,k)%callgraphnchoosek(4,2)
作为在每个笔记本中包含%load_ext callgraph的替代方法 使用%callgraph,可以将扩展添加到笔记本 ipython配置文件中的配置文件。
您的配置文件可能名为~/.ipython/profile_default/ipython_config.py。 (您可以运行ipython profile locate找到它。) 编辑此文件以包含以下行:
c.InteractiveShellApp.extensions = ["callgraph.extension"]
(如果配置文件已包含未注释的语句 c.InteractiveShellApp.extensions = […],编辑中的扩展列表 包含"callgraph.extension"的行。
有关其他示例,请参见extension example notebook。
装饰器用法
$ 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()
有关其他文档,请参见API documentation。
有关其他说明和示例,请参见decorator example notebook。
开发
安装dev工具,并为当前python设置jupyter内核 环境:
$ pip install -r requirements-dev.txt $ python -m ipykernel install --user
本地安装:
flit install --symlink
致谢
callgraph使用pythongraphviz package。python graphviz使用 Graphviz包。
许可证
麻省理工学院