Skip to main content

Intercept function and method calls

Project description

Intercepts

Build Status PyPI license

Intercepts allows you to intercept any function call in Python and handle it 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 the intercepted function as their first argument, as well as all of the arguments to the intercepted function.

>>> def handler(func, num):
...  result = func(num)
...  return num - (result - num)
>>> def handler2(func, *args, **kwargs):
...  return "The answer is: %s" % func(*args, **kwargs)

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(print_func, message):
...     return print_func(''.join(reversed(message)))
>>> print("Hello world")
Hello world
>>> intercepts.register(print, print_handler)
>>> print("Hello world")
dlrow olleH

Requirements

Intercepts requires python 3.5+. There are currently no additional dependencies.

Installation

Intercepts 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@master

Documentation

Some documentation is available here.

This software is in early stages of development and may be unstable.

Project details


Download files

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

Files for intercepts, version 0.3.1
Filename, size File type Python version Upload date Hashes
Filename, size intercepts-0.3.1.tar.gz (12.6 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page