Tracer
- This project is inspired by the CS61A course of UC Berkeley.
- It adopts the tracer decorator and Tracer context manager to track function calls.
Using the tracer Decorator
- Refer to the file tests\core\test_tracer.py for details.
from tracer import tracer
def add(x,y):
return x + y
def chain(n):
if n <= 1:
return add(1,1)
return add(n, chain(n-1))
@tracer(trace_fn=[add, chain],
trace_entity=True)
def fun():
return chain(3)
@tracer(trace_entity=True)
def fib(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n-2) + fib(n-1)
if __name__ == "__main__":
fun()
print()
fib(3)
Execution Output:
-> fun()
| -> chain(n=3)
| | -> chain(n=2)
| | | -> chain(n=1)
| | | | -> add(x=1, y=1)
| | | | | <- add returned 2
| | | | <- chain returned 2
| | | -> add(x=2, y=2)
| | | | <- add returned 4
| | | <- chain returned 4
| | -> add(x=3, y=4)
| | | <- add returned 7
| | <- chain returned 7
| <- fun returned 7
-> fib(n=3)
| -> fib(n=1)
| | <- fib returned 1
| -> fib(n=2)
| | -> fib(n=0)
| | | <- fib returned 0
| | -> fib(n=1)
| | | <- fib returned 1
| | <- fib returned 1
| <- fib returned 2
Using the Tracer Context Manager
- Refer to the file tests\core\test_Tracer.py for details.
from tracer import Tracer
def add(x,y):
return x + y
def chain(n):
if n <= 1:
return add(1,1)
return add(n, chain(n-1))
def fun():
return chain(3)
def fib(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n-2) + fib(n-1)
if __name__ == "__main__":
with Tracer(trace_fn=[add,chain,fun,fib]):
fun()
print()
fib(3)
Execution Output:
-> fun()
| -> chain(n=3)
| | -> chain(n=2)
| | | -> chain(n=1)
| | | | -> add(x=1, y=1)
| | | | | <- add returned 2
| | | | <- chain returned 2
| | | -> add(x=2, y=2)
| | | | <- add returned 4
| | | <- chain returned 4
| | -> add(x=3, y=4)
| | | <- add returned 7
| | <- chain returned 7
| <- fun returned 7
-> fib(n=3)
| -> fib(n=1)
| | <- fib returned 1
| -> fib(n=2)
| | -> fib(n=0)
| | | <- fib returned 0
| | -> fib(n=1)
| | | <- fib returned 1
| | <- fib returned 1
| <- fib returned 2
Note: The Tracer context manager does not provide the trace_entity: bool interface.
Tracer Core Functionalities
1. Logging Function
-
Implemented based on the built-in
loggingmodule. -
Enabled via the
logging_pathparameter of both thetracerdecorator andTracercontext manager. -
logging_pathdefaults toNone. If a valid file path is passed, logs will be persisted to the specified path.
Refer to tests/core/test_log_tracer.py and tests/core/test_log_Tracer.py for usage examples.
2. Time Tracing
-
Implemented based on the
timeanddataclassmodules. -
Unique call records are identified via frame markers.
-
Enabled via the
time_traceparameter oftracerandTracer. -
time_tracedefaults toFalse. Set it toTrueto enable time tracing.
Example Code:
from tracer import tracer
def add(x,y):
return x + y
def chain(n):
if n <= 1:
return add(1,1)
return add(n, chain(n-1))
@tracer(trace_fn=[add, chain],
trace_entity=True,
logging_path="/home/ricaedo/Tracer/test.log",
time_trace=True)
def fun():
return chain(3)
@tracer(trace_entity=True,logging_path="/home/ricaedo/Tracer/test.log",time_trace=True)
def fib(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n-2) + fib(n-1)
if __name__ == "__main__":
fun()
print()
fib(3)
Execution Output:
-> fun()
| -> chain(n=3)
| | -> chain(n=2)
| | | -> chain(n=1)
| | | | -> add(x=1, y=1)
| | | | | <- add returned 2 | cost=0.000013s
| | | | <- chain returned 2 | cost=0.000042s
| | | -> add(x=2, y=2)
| | | | <- add returned 4 | cost=0.000012s
| | | <- chain returned 4 | cost=0.000092s
| | -> add(x=3, y=4)
| | | <- add returned 7 | cost=0.000012s
| | <- chain returned 7 | cost=0.000150s
| <- fun returned 7 | cost=0.000223s
===== Function Trace Statistics Report =====
Function Calls Total(s) Avg(s) Min(s) Max(s)
add 3 0.0000 0.0000 0.0000 0.0000
chain 3 0.0003 0.0001 0.0000 0.0001
fun 1 0.0002 0.0002 0.0002 0.0002
============================================
-> fib(n=3)
| -> fib(n=1)
| | <- fib returned 1 | cost=0.000012s
| -> fib(n=2)
| | -> fib(n=0)
| | | <- fib returned 0 | cost=0.000011s
| | -> fib(n=1)
| | | <- fib returned 1 | cost=0.000011s
| | <- fib returned 1 | cost=0.000059s
| <- fib returned 2 | cost=0.000115s
===== Function Trace Statistics Report =====
Function Calls Total(s) Avg(s) Min(s) Max(s)
fib 5 0.0002 0.0000 0.0000 0.0001
============================================
Refer to tests/core/test_time_Tracer.py for time tracing usage with the Tracer context manager.
3. Startup Interactive Inspection
-
Implemented based on the
code.interactmodule. -
Enabled via the boolean parameter
interactive. -
When enabled, an interactive shell will be launched for real-time data inspection.
Example Code:
from tracer import tracer
def add(x,y):
return x + y
def chain(n):
if n <= 1:
return add(1,1)
return add(n, chain(n-1))
@tracer(trace_fn=[add, chain],
trace_entity=True,
logging_path="/home/ricaedo/Tracer/test.log",
interactive=True)
def fun():
return chain(3)
@tracer(trace_entity=True,
logging_path="/home/ricaedo/Tracer/test.log",
interactive=True)
def fib(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n-2) + fib(n-1)
if __name__ == "__main__":
fun()
print()
fib(3)
Execution Output:
==== Tracer Interactive Shell ====
===+fun()+===
>>> exit()
[Tracer] Interactive exit: SystemExit
-> fun()
| -> chain(n=3)
| | -> chain(n=2)
| | | -> chain(n=1)
| | | | -> add(x=1, y=1)
| | | | | <- add returned 2
| | | | <- chain returned 2
| | | -> add(x=2, y=2)
| | | | <- add returned 4
| | | <- chain returned 4
| | -> add(x=3, y=4)
| | | <- add returned 7
| | <- chain returned 7
| <- fun returned 7
==== Tracer Interactive Shell ====
===+fib(3)+===
>>> exit()
[Tracer] Interactive exit: SystemExit
-> fib(n=3)
| -> fib(n=1)
| | <- fib returned 1
| -> fib(n=2)
| | -> fib(n=0)
| | | <- fib returned 0
| | -> fib(n=1)
| | | <- fib returned 1
| | <- fib returned 1
| <- fib returned 2
This parameter is also supported by theTracer context manager. Refer to tests/interact/test_Tracer_interact.py for usage demonstrations.
4. Key Event Terminal Interaction
-
Implemented based on the
code.interactmodule. -
Enabled via the boolean parameter
interactive_on_event. -
When enabled, an interactive shell will pop up at key execution nodes for data inspection.
Example Code:
from tracer import tracer
def add(x,y):
return x + y
def chain(n):
if n <= 1:
return add(1,1)
return add(n, chain(n-1))
@tracer(trace_fn=[add, chain],
trace_entity=True,
logging_path="/home/ricaedo/Tracer/test.log",
interactive_on_event=True)
def fun():
return chain(3)
@tracer(trace_entity=True,
logging_path="/home/ricaedo/Tracer/test.log",
interactive_on_event=True)
def fib(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n-2) + fib(n-1)
if __name__ == "__main__":
fib(2)
Execution Output:
==== Tracer Interactive Shell ====
===+fib(n=2)+===
>>> exit()
[Tracer] Interactive exit: SystemExit
==== Tracer Interactive Shell ====
===+fib(n=0)+===
>>> exit()
[Tracer] Interactive exit: SystemExit
==== Tracer Interactive Shell ====
===+fib returned 0====
>>> exit()
[Tracer] Interactive exit: SystemExit
==== Tracer Interactive Shell ====
===+fib(n=1)+===
>>> exit()
[Tracer] Interactive exit: SystemExit
==== Tracer Interactive Shell ====
===+fib returned 1====
>>> exit()
[Tracer] Interactive exit: SystemExit
==== Tracer Interactive Shell ====
===+fib returned 1====
>>> exit()
[Tracer] Interactive exit: SystemExit
-> fib(n=2)
| -> fib(n=0)
| | <- fib returned 0
| -> fib(n=1)
| | <- fib returned 1
| <- fib returned 1
After enabling interactive_on_event, interactive shells will be triggered at the start and return moments of all traced functions.
This parameter is also compatible with the Tracer context manager. Refer to tests/interact/test_Tracer_interact_on_event.py for relevant examples.
Important Notes:
Enabling interactive_on_event is not recommended in most scenarios. To preserve terminal stability, the tool suppresses SystemExit, EOFError, and ValueError, which may cause the program to be unclosable except by terminating the terminal process. Additionally, each function triggers two interactive sessions (on entry and return), leading to severe efficiency degradation in large-scale program execution.
Only enable this parameter if you fully understand its working mechanism and usage scenarios.
Metadata
Release files for pypi-tracer 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pypi_tracer-0.1.0.tar.gz | 46.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pypi_tracer-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 79.6 kB
Release files / pypi_tracer-0.1.0.tar.gz
| Download URL | pypi_tracer-0.1.0.tar.gz |
|---|---|
| Size | 46.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
262cd3862202504e4497dfa4226bafca4b32be1332a5444f25b5137b0374fa90
|
|
BLAKE2b-256 checksum How to use checksums |
f06fb5a033075088568256fd8d5081e1115355d25086f85f1fae4d2168872f26
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.20
|
Release files / pypi_tracer-0.1.0-py3-none-any.whl
| Download URL | pypi_tracer-0.1.0-py3-none-any.whl |
|---|---|
| Size | 33.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
220b405ad739c88b91e7d29ccb56a1b13b4777ce13ab1c7cc41388f6968e87bb
|
|
BLAKE2b-256 checksum How to use checksums |
7c143c09de8802f6664743938961812e743a15d1960736783d91ae9d0978ec35
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.20
|