Skip to main content

Capture code blocks while executing them

Project description

CodeCapture

CodeCapture is a context manager that enables capturing blocks of code while running it. This is meant for educational purposes, e.g., extracting blocks of code from a file for inclusion in readers.

>>> from code_capture import CodeCapture
>>> with CodeCapture("foo"):
        foo = 3
        bar = "test "
        foobar = foo * bar
        print(foobar)
test test test
>>> print(CodeCapture.store.foo)
foo = 3
bar = "test "
foobar = foo * bar
print(foobar)

Installation

pip install code-capture

Context manager

CodeCapture captures all code within the associated code block, based on the indentation of the code block.

Bunch

The capture code blocks are stored in the CodeCapture.store bunch. This allows for dot-access to the stored values. However, you can also treat CodeCapture.store as a dictionary, e.g., CodeCapture.store['foo'] equals CodeCapture.store.foo.

For more information, see [https://pypi.org/project/bunch-py3/].

Acknowledgement

The code for this project was largely copied from G. Shand's answer to this stackoverflow question.

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

code_capture-0.1.2.tar.gz (2.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

code_capture-0.1.2-py3-none-any.whl (2.2 kB view details)

Uploaded Python 3

File details

Details for the file code_capture-0.1.2.tar.gz.

File metadata

  • Download URL: code_capture-0.1.2.tar.gz
  • Upload date:
  • Size: 2.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.18

File hashes

Hashes for code_capture-0.1.2.tar.gz
Algorithm Hash digest
SHA256 5173c47279ca3b99c0f07d85a7fcfb31feafea598c92c154add53a0d4df11898
MD5 e175776b99f93d6d7964cc975bd49956
BLAKE2b-256 3f297aceb9f43aff9b8791c9b3c8e940d34e83b32d15cc530f83b78986782034

See more details on using hashes here.

File details

Details for the file code_capture-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for code_capture-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b9d76b48f972c447f5ba3e0e4e4b07b668b8ca5f3be6cf101e70adc28a7b4179
MD5 8ab732d6df8d606c0ae0f8cf9f0b994c
BLAKE2b-256 d6183f73599d4c7ddcb83d92bbf6af4e4b54b6b6e4b290eed904c52967f54e20

See more details on using hashes here.

Supported by

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