Created
May 16, 2013 15:21
-
-
Save abhishekkr/5592520 to your computer and use it in GitHub Desktop.
pycallgraph examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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