backcall

Backwards compatible callback APIs

BSD-3-CLAUSE License

Downloads
14.2M
Stars
17
Committers
2

========
backcall

.. image:: https://travis-ci.org/takluyver/backcall.png?branch=master :target: https://travis-ci.org/takluyver/backcall

Specifications for callback functions passed in to an API

If your code lets other people supply callback functions, it's important to specify the function signature you expect, and check that functions support that. Adding extra parameters later would break other peoples code unless you're careful.

backcall provides a way of specifying the callback signature using a prototype function::

from backcall import callback_prototype

@callback_prototype
def handle_ping(sender, delay=None):
    # Specify positional parameters without a default, and keyword
    # parameters with a default.
    pass

def register_ping_handler(callback):
    # This checks and adapts the function passed in:
    callback = handle_ping.adapt(callback)
    ping_callbacks.append(callback)

If the callback takes fewer parameters than your prototype, backcall will wrap it in a function that discards the extra arguments. If the callback expects more arguments, a TypeError is thrown when it is registered.

For more details, see the docs <http://backcall.readthedocs.org/en/latest/>_ or the Demo notebook <http://nbviewer.ipython.org/github/takluyver/backcall/blob/master/Demo.ipynb>_.

The tests are run with pytest <http://pytest.org/latest/>_. In the root directory, execute::

py.test
Package Rankings
Top 5.16% on Pypi.org
Top 19.92% on Spack.io
Top 13.05% on Alpine-v3.18
Top 17.33% on Alpine-edge
Top 15.38% on Alpine-v3.15
Top 32.58% on Anaconda.org
Top 13.79% on Alpine-v3.10
Top 16.7% on Alpine-v3.16
Top 18.52% on Alpine-v3.17
Top 13.65% on Alpine-v3.14
Top 12.77% on Alpine-v3.13
Top 12.29% on Alpine-v3.11
Top 12.24% on Alpine-v3.12
Top 27.92% on Conda-forge.org