Skip to main content

A result(input, ...) value tracer for debugging purposes

Project description

Description

PyTraceToIX is a Python result(input, ...) value tracer for debugging purposes.

Code editors can't set breakpoints inside expressions, lambda functions, list comprehensions and chained methods, forcing major code changes to debug this code.

PyTraceToIX offers a simple solution to this problem.

It was built to be simple with awkward functions to be easy to spot it and remove it once the bug is found.

PyTraceToIX has 2 major functions:

  • c__ capture the input of a function or expression input. ex: c__(x)
  • d__ display the result of a function or expression and all the captured inputs. ex: d__(c__(x) + c__(y))

If you find this project useful, please, read the Support this Project on how to contribute.

Features

  • Multithreading support.
  • Simple and short minimalist function names.
  • Result with Inputs tracing.
  • Configurable formatting at global level and at function level.
  • Configurable result and input naming.
  • Output to the stdout or a stream.
  • Multiple levels.
  • Capture Input method with allow callback.
  • Display Result method with allow, before, after callbacks.

Installation

pip install pytracetoix

Usage

#!/usr/bin/env python3
from pytracetoix import d__, c__

[x, y, w, k, u] = [1, 2, 3, 4 + 4, lambda x:x]
#  expression
z = x + y * w + (k * u(5))

# Display expression with no inputs
z = d__(x + y * w + (k * u(5)))

# Output:
# _:`47`

# Display expression result with inputs
z = d__(c__(x) + y * c__(w) + (k * u(5)))

# Output:
# i0:`1` | i1:`3` | _:`47`

# Display expression result with inputs within an expression
z = d__(c__(x) + y * c__(w) + d__(k * c__(u(5), level=1)))

# Output:
# i0:`5` | _:`40`
# i0:`1` | i1:`3` | _:`47`

# lambda function
f = lambda x, y: x + (y + 1)
f(5, 6)

# Display lambda function result and inputs
f = lambda x, y: d__(c__(x) + c__(y + 1))
f(5, 6)

# Output:
# i0:`5` | i1:`7` | _:`12`

# Display lambda function inputs and result with input and result names
f = lambda x, y: d__(c__(x, name='x') + c__(y + 1, name='y+1'), name='f')
f(5, 6)

# Output:
# x:`5` | y+1:`7` | f:`12`

#  list comprehension
l = [5 * y * x for x, y in [(10, 20), (30, 40)]]

# Display list comprehension with input and result names
l = d__([5 * c__(y, name=f"y{y}") * c__(x, name=lambda index, _, __: f'v{index}') for x, y in [(10, 20), (30, 40)]])

# Output:
# y20:`20` | v1:`10` | y40:`40` | v3:`30` | _:`[1000, 6000]`

# Display expression if `input count` is 2
d__(c__(x) + c__(y), allow=lambda data: data['input_count__'] == 2)

# Display expression if the first input value is 10.0
d__(c__(x) + c__(y), allow=lambda data: data['i0'] == 10.0)

# Display expression if the `allow_input_count` is 2, in this case if `x > 10`
d__(c__(x, allow=lambda index, name, value: value > 10) + c__(y),
        allow=lambda data: data['allow_input_count__'] == 2)

# Display expression if the generated output has the text 10
d__([c__(x) for x in ['10', '20']], before=lambda data: '10' in data['output__'])

# Display expression and after call `call_after` if it was allowed to display
d__([c__(x) for x in ['10', '20']], allow=lambda data: data['allow_input_count__'] == 2,
        after=lambda data: call_after(data) if data['allow__'] else "")

class Chain:
    def __init__(self, data):
        self.data = data

    def map(self, func):
        self.data = list(map(func, self.data))
        return self

    def filter(self, func):
        self.data = list(filter(func, self.data))
        return self


# A class with chain methods
Chain([10, 20, 30, 40, 50]).map(lambda x: c__(x * 2)).filter(lambda x: c__(x > 70))

# Display the result and capture the map and filter inputs
d__(Chain([10, 20, 30, 40, 50]).map(lambda x: c__(x * 2)).filter(lambda x: c__(x > 70)).data)

# Output:
# i0:`20` | i1:`40` | i2:`60` | i3:`80` | i4:`100` | i5:`False` | i6:`False` | i7:`False` | i8:`True` | i9:`True` | _:`[80, 100]`

Formatting

The d__ function can override the default formatting, and it can also be defined at global level.

from pytracetoix import init__

init__(format={
    'result': '{name}:`{value}`',
    'input': '{name}:`{value}`',
    'sep': ' | ',
    'new_line': True
})

Formatting parameters:

  • result: The result value format will be displayed.
  • input: The result value format will be displayed.
  • sep: The separator text between each input and the result.
  • new_line: If True it will add a new line at the end of output.

Multithreading

To activate the multithreading support:

from pytracetoix import d__, c__, t__, init__

init__(multithreading=True)

## It displays the threadId: i0: `4` | _: `5`
def thread_function():
    d__(c__(4) + 1)

## It displays the something: i0: `4` | _: `5`
def thread_function_with_name():
    t("something")
    d__(c__(4) + 1)

threads = []
for _ in range(5):
    thread = threading.Thread(target=thread_function)
    threads.append(thread)
threads.append(threading.Thread(target=thread_function_with_name))

for thread in threads:
    thread.start()
for thread in threads:
    thread.join()

Metadata

The allow, before and after will receive a parameter data with the allowed inputs plus the following meta items:

  • meta__: list of meta keys including the name key.
  • thread_id__: thread_id being executed
  • allow_input_count__: total number of inputs that are allowed.
  • input_count__: total number of inputs being captured.
  • allow__: If false it was allowed. Use this for after callback.
  • output__: Text passed to before without new_line.
  • name: name parameter

Support this Project

If you find this project useful, consider supporting it:

License

MIT License

Copyrights

(c) 2024 Alexandre Bento Freire

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytracetoix-0.1.5.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

pytracetoix-0.1.5-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file pytracetoix-0.1.5.tar.gz.

File metadata

  • Download URL: pytracetoix-0.1.5.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for pytracetoix-0.1.5.tar.gz
Algorithm Hash digest
SHA256 bcc868ebdb34e9630ba1f4e9629c52b46191c3a039906abbba785c0c996a64bc
MD5 5b70d52c995c5942a18249791ae54c27
BLAKE2b-256 030906eeac33e4a71d9b63046e935aff35ef7217fc2168d1c6d62dad55cf2561

See more details on using hashes here.

File details

Details for the file pytracetoix-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: pytracetoix-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for pytracetoix-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 336611cde1538fcf49f8bcb1f890371ca4d92ab7206f1d19d857a06fbb3eaec2
MD5 2180dc548a156ca4f45eeba042a70ff7
BLAKE2b-256 3e60c2c376a4089c77930be10a5bd7bcd827c1a09097a6be2bdaced30439d732

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page