Dynamic dispatch for python similar to clojure's multimethod.
Project description
flex_dispatch
A super flexible dynamic dispatch implementation for python, inspired by Clojure's multimethod.
Install with: pip install flex-dispatch
To use: from flex_dispatch import Dispatcher
and annotate any callable with @Dispatcher
Example
from flex_dispatch import Dispatcher
@Dispatcher
def greet(*args):
if len(args) == 1:
return '_just_name'
elif len(args) == 2:
return '_name_msg'
@greet.map('_just_name')
def say_hey(name):
print(f'Hello, {name}!')
@greet.map('_name_msg')
def say_message(name, msg):
print(f'{msg} {name}')
greet('Chris') # calls say_hey and prints "Hello, Chris!"
greet('Bob', 'Boo') # calls say_message and prints "Boo Bob"
greet('b', 'b', 3) # greet returns none as a dispatch value, and DispatchError is raised.
Any callable decorated with @Dispatcher becomes a dispatcher function. It should inspect its arguments and return a "dispatch value"; i.e., a value used
to determine which callable to dispatch the call to. Callables decorated with @<dispatcher>.map(<value>)
(like @greet.map('_just_name')
above) register the decorated function as the dispatch target for the given value.
In other words, when greet('Chris')
is called, first the greet
function is called to return a dispatch value, in this case '_just_name'
. Since say_hey
was decorated with @greet.amp('_just_name')
, it was registered as the target function to call when greet
returns the dispatch value '_just_name'
.
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file flex-dispatch-0.1.1.tar.gz
.
File metadata
- Download URL: flex-dispatch-0.1.1.tar.gz
- Upload date:
- Size: 2.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.10.4 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17ef20b6f0f5c71ccc588d6c937b6b75b121efe56922a432dd841689eaa483b1 |
|
MD5 | 52f302a169c58d141d18f9e3e85125bc |
|
BLAKE2b-256 | 45ef6e07152b3fabc5847597ea8278ef5ab8df84a0891140fcac3c6fc93f2ab0 |
File details
Details for the file flex_dispatch-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: flex_dispatch-0.1.1-py3-none-any.whl
- Upload date:
- Size: 2.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.10.4 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3aaa380ecdefe11c64bc26486f8d500b0dcb461f1d0929b40e017c16a389e03f |
|
MD5 | 228033a279c596c6b8ea13118e910bcb |
|
BLAKE2b-256 | 4ead6b02f08a9ec781a4c9073834220355a331fdcc19382186ccafb22e15fc71 |