Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
pycallgraph examples
#!/usr/bin/env python
"""
require 'pycallgraph' library to be installed with graphviz,
and the attached "pycallgraph_decorator.py" file to be present as well
to provide the decorator used
"""
from pycallgraph_decorator import callgraph
def add(arg1, arg2):
return (arg1 + arg2)
def mul(arg1, arg2):
return (arg1 * arg2)
def say(msg):
print("[+] %s" % msg)
@callgraph()
def add_n_mul(arg1, arg2):
add_val = add(arg1, arg2)
say("just a sample meth o d")
mul_val = mul(arg1, arg2)
return (add_val, mul_val)
print(add_n_mul(5, 10))
import pycallgraph
"""
Let's you use PyCallGraph as a decorator
"""
def callgraph():
def argwrapper(func):
def callwrapper(*args, **kwargs):
if callwrapper.already_called:
return func(*args, **kwargs)
callwrapper.already_called = True
pycallgraph.start_trace(reset = False)
result = func(*args, **kwargs)
pycallgraph.stop_trace()
filename = "/tmp/%s.png" % str(func.__name__)
pycallgraph.make_dot_graph(filename) #, format='jpg') #, tool='neato')
return result
callwrapper.already_called = False
return callwrapper
return argwrapper
#!/usr/bin/env python
"""
require just 'pycallgraph' library to be installed with graphviz, all tracing code is present inline
"""
import pycallgraph
def add(arg1, arg2):
return (arg1 + arg2)
def mul(arg1, arg2):
return (arg1 * arg2)
def say(msg):
print("[+] %s" % msg)
def add_n_mul(arg1, arg2):
pycallgraph.start_trace()
add_val = add(arg1, arg2)
pycallgraph.stop_trace()
say("just a sample meth o d")
pycallgraph.start_trace(reset = False)
pycallgraph.call_stack.pop() # to get 'add__mul' as parent of current calls again
mul_val = mul(arg1, arg2)
pycallgraph.stop_trace()
pycallgraph.make_dot_graph('/tmp/selective_add_n_mul.png')
return (add_val, mul_val)
print(add_n_mul(5, 10))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment