From f395ce9224dba873ed65c33906d3959e6a9e50de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Agusti=CC=81n=20Benassi?= Date: Wed, 12 Jul 2017 23:50:36 -0300 Subject: [PATCH] Fix decorator implementation and import. Document use of decorators in README. --- README.rst | 28 ++++++++++++++++++++++++++++ pycallgraph/__init__.py | 5 ++++- pycallgraph/decorators.py | 7 ++++++- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index a3d0972b..8db8fcbf 100644 --- a/README.rst +++ b/README.rst @@ -66,6 +66,34 @@ A simple use of the API is:: with PyCallGraph(output=GraphvizOutput()): code_to_profile() +Use decorators for an even more simple use of the API:: + + from pycallgraph.decorators import trace + + @trace("path/to/output.png") + def main(): + code_to_profile() + + main() + +Or decorate a specific function inside your code you want to profile:: + + from pycallgraph.decorators import trace + + @trace("path/to/output.png") + def function_1(): + do_stuff + + def function_2(): + do_stuff + + def code_to_profile(): + function_1() + function_2() + + code_to_profile() + + Documentation ============= diff --git a/pycallgraph/__init__.py b/pycallgraph/__init__.py index 6bfdd1d2..644e4eaa 100644 --- a/pycallgraph/__init__.py +++ b/pycallgraph/__init__.py @@ -14,7 +14,10 @@ from .pycallgraph import PyCallGraph from .exceptions import PyCallGraphException -from . import decorators +try: + from . import decorators +except Exception: + import decorators from .config import Config from .globbing_filter import GlobbingFilter from .grouper import Grouper diff --git a/pycallgraph/decorators.py b/pycallgraph/decorators.py index 78b3ce07..ad415b34 100644 --- a/pycallgraph/decorators.py +++ b/pycallgraph/decorators.py @@ -1,13 +1,18 @@ import functools from .pycallgraph import PyCallGraph +from .output import GraphvizOutput def trace(output=None, config=None): def inner(func): @functools.wraps(func) def exec_func(*args, **kw_args): - with(PyCallGraph(output, config)): + + graphviz = GraphvizOutput() + graphviz.output_file = output + + with(PyCallGraph(output=graphviz, config=config)): return func(*args, **kw_args) return exec_func