Skip to main content

Intercept function and method calls

Project description

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.

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

intercepts-0.4.0.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

intercepts-0.4.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file intercepts-0.4.0.tar.gz.

File metadata

  • Download URL: intercepts-0.4.0.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for intercepts-0.4.0.tar.gz
Algorithm Hash digest
SHA256 5d30b920e1b7e389bbaf5b30d252bf3075aa5cb746c5cf4362f4e64b0aeb521b
MD5 ef7a4377b45148f5df4c95b66d76f74f
BLAKE2b-256 439a38b08760e08e6e87206383d615957644c6a403c71c97ae23df1583ce4935

See more details on using hashes here.

File details

Details for the file intercepts-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: intercepts-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 8.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.10

File hashes

Hashes for intercepts-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8cc9549243ededcdc5d0e4a3e93686833db8e28360b3d58cc417a75707b39135
MD5 df9cbc39bfb70542a31c056b02ec0401
BLAKE2b-256 0b3b9a630ebe2891d4ec07845aba476e69b21599d58529fa0b90cb35eff5774c

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