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.
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
Built Distribution
Hashes for signature_dispatch-0.1.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3edafa8abc749e7fa1bcc7cb8e3219bcabde6e5f3f0c186a63225eb5bb22a118 |
|
MD5 | f841c1d148f55dc6fb6ac7ba0ea23d11 |
|
BLAKE2b-256 | 7314f3a41f3547da45c52a4e86b8b3045974a11b313c363569c67c1c6bba0953 |