function_trace (GitHub) prints out a nested output of function calls with argument and return values as well as thrown exceptions:
It works by temporarily replacing all functions/methods within the listed classes/modules with traced versions. Then when the ‘trace’ block exits, all the original values are restored.
It works using context manager, an examples as follows:
from function_trace import trace_on import math class MyObject(): def __init__(self, v): self.set_v(v) def get_v(self): return self.v def set_v(self, v): self.v = math.log(v) def nono(self): raise Exception('nono') with trace_on([math, MyObject]): math.log(123) o = MyObject(456) o.get_v() o.set_v(789)
You put what you want to trace into the list, classes or modules. Here is the output:
- math.log(123) -> 4.812184355372417 - __main__.set_v(<__main__.MyObject instance at 0x736638>, 456) | - math.log(456) | -> 6.1224928095143865 -> None - __main__.get_v(<__main__.MyObject instance at 0x736638>) -> 6.1224928095143865 - __main__.set_v(<__main__.MyObject instance at 0x736638>, 789) | - math.log(789) | -> 6.670766320845874 -> None - __main__.nono(<__main__.MyObject instance at 0x736680>) -> Exception('nono',) Traceback (most recent call last): File "./test.py", line 30, in <module> o.nono() File "/tmp/function_trace/function_trace/__init__.py", line 62, in g return tracer(f, *args, **kwargs) File "/tmp/function_trace/function_trace/__init__.py", line 47, in stdout_tracer r = f(*args, **kwargs) File "./test.py", line 23, in nono raise Exception('nono') Exception: nono
You can see it tracing the math.log calls as well as the instance methods of MyObject class. Also an exception is also traced in the output.
This library is licensed under the Python Software Foundation License, currently version 0.1 (2014-03-31).
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.