Skip to main content

Utilities for tracing operations.

Latest release 20260912: Initial PyPI release: Trace class for recording salient decisions and actions for later debugging.

Short summary:

  • Trace: A class/decorator to trace control flow and decisions. This makes it possible to record function calls and their inner decision chains, and to show these in a nice printout after the fact.

Classes

class Trace(cs.threads.HasThreadState)

A class/decorator to trace control flow and decisions. This makes it possible to record function calls and their inner decision chains, and to show these in a nice printout after the fact.

A new trace object adds itself to the records of the ambient trace object.

A trace object supports the context manager protocol, making it the ambient object, so that it accrues any new trace objects make inside the context.

with Trace("name') as T:
    ... add records via T ...

Calling a trace object adds a new record to the trace

if T("test x==2", x==2):
    T("acting on x==2")
else:
    T("x != 2")

As a trace object:

>>> from builtins import print
>>> with Trace("decide!") as T:
...   print("start")
...   if T("test 1 for never", 1==2):
...     print("never")
...   elif T("test 2 for always", 1==1):
...     print("always")
...     with Trace("inside test 2", T) as T2:
...       assert T2 in T.tests
...       if T2("inside1",1==1):
...         print("true")
...       else:
...         print("false")
...
start
always
true
>>> T.printt()
decide!
├─test 1 for never -> bool   False
├─test 2 for always -> bool  True
╰─inside test 2
  ╰─inside1 -> bool          True

As a decorator it calls the function with an additional named argument T which is the Trace instance for that call of the function:

>>> @Trace
... def func(x, T):
...   x2 = T(f'{x=} + 2', x+2)
...   return x2
...
>>> with Trace("func trace") as T:
...   x2 = T("call func with 3", func(3))
...   print("x2", x2)
...
x2 5
>>> T.printt() # doctest: +ELLIPSIS
func trace
├─func(....)
│ │ from <module>() <doctest cs.trace.Trace[4]>:2
│ │ x2 = T("call func with 3", func(3))
│ ├─x=3 + 2 -> int                                 5
│ ╰─return -> int                                  5
╰─call func with 3 -> int                          5

Trace.__call__(self, label: str, result='', print=False)

Calling the trace object records (abel,result) and optionally prints.

Trace.__firstlineno__

int([x]) -> integer int(x, base=10) -> integer

Convert a number or string to an integer, or return 0 if no arguments are given. If x is a number, return x.int(). For floating-point numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in the given base. The literal can be preceded by '+' or '-' and be surrounded by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to interpret the base from the string as an integer literal.

int('0b100', base=0) 4

Trace.__static_attributes__

Built-in immutable sequence.

If no argument is given, the constructor returns an empty tuple. If iterable is specified the tuple is initialized from iterable's items.

If the argument is a tuple, the return value is the same object.

Trace.perthread_state

A Thread local object with attributes which can be used as a context manager to stack attribute values.

Example:

from cs.threads import ThreadState

S = ThreadState(verbose=False)

with S(verbose=True) as prev_attrs:
    if S.verbose:
        print("verbose! (formerly verbose=%s)" % prev_attrs['verbose'])

Trace.printt(self, **printt_kw)

Use cs.lex.printt() to print this trace object. Keyword arguments are passed through.

Trace.tabulate(self)

Tabulate this trace object for use with cs.lex.printt().

Release Log

Release 20260912: Initial PyPI release: Trace class for recording salient decisions and actions for later debugging.

Release files for cs-trace 20260912

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cs-trace 20260912
File Size Uploaded
cs_trace-20260912.tar.gz 4.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cs-trace 20260912
File Interpreter ABI Platform
cs_trace-20260912-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 9.4 kB

Release files / cs_trace-20260912.tar.gz

Download URL cs_trace-20260912.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
1aff7c72820e3c24f33f63cb687e40e15c56bbc79e662fb6dac33f932b2028cb
BLAKE2b-256 checksum
How to use checksums
dd2fb54550b0cac68b832dd1a4f78fd697ccf97120e2c6b8525341d36de21952
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.1

Release files / cs_trace-20260912-py2.py3-none-any.whl

Download URL cs_trace-20260912-py2.py3-none-any.whl
Size 5.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
49112b0f8577327f21759567c78fed40bd9a824aeb2f0b09e651c7e6c929b61d
BLAKE2b-256 checksum
How to use checksums
e06a7589088a710cb898a39e5d7fd79d48be147dbc5818a9a51f16f5f84359cc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.1

Release history Release notifications | RSS feed

This release

20260912 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page