Skip to content

Instantly share code, notes, and snippets.

@bsmithyman
Last active May 4, 2016 19:42
Show Gist options
  • Save bsmithyman/8c6d5decb4f359ef28742c6f4e1d4389 to your computer and use it in GitHub Desktop.
Save bsmithyman/8c6d5decb4f359ef28742c6f4e1d4389 to your computer and use it in GitHub Desktop.
Generate a call graph for the enclosed context
import contextlib
import os.path
from pycallgraph import PyCallGraph
from pycallgraph.output import GraphvizOutput
@contextlib.contextmanager
def graph_stack(filename):
'Generate a call graph for the enclosed context'
go = GraphvizOutput(output_file=filename)
go.output_type = os.path.splitext(filename)[-1][1:]
with PyCallGraph(output=go):
yield
# --------
if __name__ == '__main__':
def do_hello(times):
for i in xrange(times):
hello()
def hello():
print('Hello World!')
with graph_stack('test.pdf'):
do_hello(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment