Skip to main content

context

Python debugging with context helper

Don't want complicated debugger but the print("here") isn't enough to help you catch mistakes ? Then you should try this module !

Installation

pip install ctxt

Usage

This example was generated using the notebook in the demo folder.

from context import here, ctxt
"""
here: minimalist debugging
ctxt: complete debugging

Want an intermediate debugging? Instantiate your own Context object :-)
"""

sep = "\n" + 70 * "=" + "\n"

here()
[<ipython-input-1-79eead714f34>:11] in <module>
x = here(5 + 10)
print("x =", x)

print(sep)

y = ctxt(5 + 10)
print("y =", y)

print(sep)

z = ctxt(5, 6, a=10)
print("z =", z)
[<ipython-input-2-db88b4f95185>:1] in <module>
x = 15

======================================================================

[<ipython-input-2-db88b4f95185>:6] in <module>:
> 15
y = 15

======================================================================

[<ipython-input-2-db88b4f95185>:11] in <module>:
> 5
> 6
> a = 10
z = ((5, 6), dict_items([('a', 10)]))
# Handle call inside a function
def my_square(x):
    y = x ** 2
    here()
    return y

s = my_square(5)
print("s =", s)

print(sep)


# Can also wrap a function call
s = here(my_square(10))
print("s =", s)
[<ipython-input-3-323904d716c7>:4] in my_square
s = 25

======================================================================

[<ipython-input-3-323904d716c7>:4] in my_square
[<ipython-input-3-323904d716c7>:14] in <module>
s = 100
# Can also be a function wrapper
@here  # binds to here.wrap
def my_sqrt(x):
    return pow(x, 0.5)

r = my_sqrt(4)
print("r =", r)

print(sep)

# Handles nested call, adding indent per-level
@here
def my_sqrt_bis(x):
    y =  here(pow(x, 0.5))
    return y

r = my_sqrt_bis(16)
print("r =", r)

print(sep)

# Same but with more context
@ctxt
def my_sqrt_ter(x):
    y =  ctxt(pow(x, 0.5))
    return y

r = my_sqrt_ter(25)
print("r =", r)
[CALL: my_sqrt, FROM: <ipython-input-4-10cc6d34de9d>:6 in <module>]
r = 2.0

======================================================================

[CALL: my_sqrt_bis, FROM: <ipython-input-4-10cc6d34de9d>:17 in [...]
  [<ipython-input-4-10cc6d34de9d>:14] in my_sqrt_bis
r = 4.0

======================================================================

[CALL: my_sqrt_ter, FROM: <ipython-input-4-10cc6d34de9d>:28 in [...]
<- in:
> 25
  [<ipython-input-4-10cc6d34de9d>:25] in my_sqrt_ter:
  > 5.0
-> out:
> 5.0
r = 5.0
# If you still want to use the print function to add more context,
# you can wrap the print function using our wrapper

import builtins

@here
def print(*args, **kwargs):
    return builtins.print(*args)

# or
# print = here(builtins.print)

print("hello")

print(sep)  # Yes it will also be affected ;-)

def some_function():
    print("in some_function")


some_function()

# Reset old print function

print = builtins.print
[CALL: print, FROM: <ipython-input-8-b7886cfc88db>:13 in <module>]
hello
[CALL: print, FROM: <ipython-input-8-b7886cfc88db>:15 in <module>]

======================================================================

[CALL: print, FROM: <ipython-input-8-b7886cfc88db>:18 in [...]
in some_function

TODO List:

  • allows more arguments to Context class
  • allows block context with with here as h:

Metadata

Release files for ctxt 0.0.2

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

Source distribution (sdist)

Source distribution for ctxt 0.0.2
File Size Uploaded
ctxt-0.0.2.tar.gz 3.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ctxt 0.0.2
File Interpreter ABI Platform
ctxt-0.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 8.3 kB

Release files / ctxt-0.0.2.tar.gz

Download URL ctxt-0.0.2.tar.gz
Size 3.9 kB
Tags Source
SHA-256 checksum
How to use checksums
aab54874ca96967c1c45f8c951881719d7d48d8f21a5ec96bd094d9883ea11da
BLAKE2b-256 checksum
How to use checksums
5f70adf4db0b653cc214106805cc7a31ca861a3ab63a8263910a1546996159ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.7.0 requests/2.22.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.8.5

Release files / ctxt-0.0.2-py3-none-any.whl

Download URL ctxt-0.0.2-py3-none-any.whl
Size 4.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e25d207eab4bc760ffd769e460d4562d8dff30e3c4211eb3ba99f2cddcc923dd
BLAKE2b-256 checksum
How to use checksums
99458303e6e13e50e76e2931cec1bccb3f10f4862aea187d4ca420de7b3cd905
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.7.0 requests/2.22.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.8.5

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

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