Skip to main content

Execute the first function that matches the given arguments.

Project description

signature_dispatch is a simple python utility for executing the first of many functions whose signature matches the set of given arguments.

Last release Python version Test status Test coverage GitHub last commit

Installation

Install from PyPI:

$ pip install signature_dispatch

Version numbers follow semantic versioning.

Usage

Create a dispatcher and use it to decorate multiple functions. Note that the module itself is directly invoked to create a dispatcher:

>>> import signature_dispatch
>>> dispatch = signature_dispatch()
>>> @dispatch
... def f(x):
...    return x
...
>>>
>>> @dispatch
... def f(x, y):
...    return x, y
...

When called, all of the decorated functions will be tested in order to see if they accept the given arguments. The first one that does will be invoked. A TypeError will be raised if none of the functions can accept the arguments:

>>> f(1)
1
>>> f(1, 2)
(1, 2)
>>> f(1, 2, 3)
Traceback (most recent call last):
    ...
TypeError: can't dispatch the given arguments to any of the candidate functions:
arguments: 1, 2, 3
candidates:
(x): too many positional arguments
(x, y): too many positional arguments

Each decorated function will be replaced by the same callable. To avoid confusion, then, it’s best to use the same name for each function. The docstring of the ultimate callable will be taken from the final decorated function.

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

signature_dispatch-0.1.0.tar.gz (8.7 kB view hashes)

Uploaded Source

Built Distribution

signature_dispatch-0.1.0-py2.py3-none-any.whl (4.1 kB view hashes)

Uploaded Python 2 Python 3

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