Skip to main content

Intercepts

CI Status codecov PyPI License

Intercepts allows you to intercept function calls in Python and handle them in any manner you choose. For example, you can pre-process the inputs to a function, or apply post-processing on its output. Intercepts also allows you to completely replace a function with a custom implementation.

>>> increment(41)
42
>>> intercepts.register(increment, handler)
>>> increment(41)
40
>>> intercepts.unregister(increment)
>>> intercepts.register(increment, handler2)
>>> increment(41)
'The answer is: 42'
>>> intercepts.unregister_all()

Handler functions receive all paramters to the intercepted function call and can access the intercepted function through the variable _.

>>> def handler(num):
...   result = _(num)
...   return num - (result - num)
>>> def handler2(*args, **kwargs):
...   result = _(*args, **kwargs)
...   return f"The answer is: {result}"

The intercepts module also allows intercepting python built-in functions, such as print and sorted. For best results, the intercepts module should be the first module imported.

>>> def print_handler(message):
...     return _(''.join(reversed(message)))
>>> print("Hello world")
Hello world
>>> intercepts.register(print, print_handler)
>>> print("Hello world")
dlrow olleH

Installation

Intercepts requires Python 3.7+ on Linux or Windows and can be installed using pip.

$ pip install intercepts

Or, use pip to install the latest version from the github source.

$ pip install -U git+https://github.com/dlshriver/intercepts.git@main

Documentation

Some documentation is available here.

Metadata

Release files for intercepts 0.4.0

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

Source distribution (sdist)

Source distribution for intercepts 0.4.0
File Size Uploaded Provenance
intercepts-0.4.0.tar.gz 12.2 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 20.8 kB

Release files / intercepts-0.4.0.tar.gz

Download URL intercepts-0.4.0.tar.gz
Size 12.2 kB
Tags Source
SHA-256 checksum
How to use checksums
5d30b920e1b7e389bbaf5b30d252bf3075aa5cb746c5cf4362f4e64b0aeb521b
BLAKE2b-256 checksum
How to use checksums
439a38b08760e08e6e87206383d615957644c6a403c71c97ae23df1583ce4935
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.10

Release files / intercepts-0.4.0-py3-none-any.whl

Download URL intercepts-0.4.0-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8cc9549243ededcdc5d0e4a3e93686833db8e28360b3d58cc417a75707b39135
BLAKE2b-256 checksum
How to use checksums
0b3b9a630ebe2891d4ec07845aba476e69b21599d58529fa0b90cb35eff5774c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.10

Security for intercepts 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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