A Python package for advanced function dispatching based on complex, nested, and parameterized types. Inspired by singledispatch.
Project description
Dispatchery 🧙♂️✨
Dispatch your functions based on complex types.
dispatchery is a lightweight Python package inspired by the standard singledispatch decorator, but with support for complex, nested, parameterized types. With dispatchery, you can dispatch based on annotations such as tuple[int, str, dict[str, int]] or list[dict[str, list[int]]].
Unlike singledispatch, dispatchery can also dispatch based on multiple arguments and keyword arguments, rather than only the first one. It also supports nested types and union types such as Union[int, str] or int | str, making it a powerful tool for writing clean, type-specific code.
Features
- Advanced Type Dispatching: Supports complex generic types.
- Recursive Type Matching: Handles nested types like
tuple[int, str, dict[str, int]]. - Union Types: Dispatch based on union types like
Union[int, str]. - Multi Argument Dispatch: Dispatch based on multiple arguments types, not just the first.
- Simple Integration: Works just like
functools.singledispatchwith added power.
Installation
Install dispatchery from PyPI:
pip install dispatchery
Usage
If you know how to use functools.singledispatch then you already know how to use dispatchery. Decorate your main function with @dispatchery and register specific types as needed.
Examples
Suppose we want a function, process, that behaves differently based on complex types like tuple[int, str], list[str], or str | int, we can use dispatchery to achieve this:
from dispatchery import dispatchery
@dispatchery
def process(value):
return "Standard stuff."
@process.register(list[str])
def _(value):
return "Nice, a parameterized type."
@process.register(list[int])
def _(value):
return "That's different? Cool."
@process.register(list[tuple[int, str]])
def _(value):
return "Nested, too? Alright."
@process.register(bool | str | int)
def _(value):
return "Union types? No problem."
@process.register(list[tuple[int | list[float], dict[str, tuple[list[bool], dict[str, float | str]]]]])
def _(value):
return "Now this is just getting silly."
print(process(42))
# "Standard stuff."
print(process(["hello", "world"]))
# "Nice, a parameterized type."
print(process([1, 2, 3]))
# "That's different? Cool."
print(process([(1, "hello"), (2, "world")]))
# "Nested, too? Alright."
print(process(True))
# "Union types? No problem."
print(process([(1, {"a": ([True, False], {"x": 3.14})})]))
# "Now this is just getting silly."
dispatchery also supports dispatching based on multiple arguments:
@dispatchery
def process(a, b):
pass
@process.register(int, str)
def _(a, b):
return "Bip boop."
@process.register(str, int)
def _(a, b):
return "Boopidy bop."
print(process(42, "hello"))
# "Bip boop."
print(process("hello", 42))
# "Boopidy bop."
And even dispatching with kwargs:
@dispatchery
def process(a, key="hello"):
pass
@process.register(str, key=int)
def _(a, key=42):
return "I like round numbers."
@process.register(str, key=float)
def _(a, key=3.14):
return "Floats are fine too I guess."
print(process("hello", key=1987))
# "I like round numbers."
print(process("hello", key=1.618))
# "Floats are fine too I guess."
Why Use Dispatchery?
- Better Readability: Your code is clean and type-specific without bulky
ifstatements. - Enhanced Maintainability: Add new types easily without modifying existing code.
- More Pythonic: Embrace the power of Python’s dynamic typing with elegant dispatching.
Dependencies
None, but you might want typing-extensions>=3.7 if you need backward compatibility for typing features.
Tip
To integrate dispatchery in an existing codebase, you can import it as singledispatch for a seamless transition:
from dispatchery import dispatchery as singledispatch
License
dispatchery is licensed under the MIT License.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dispatchery-0.2.1.tar.gz.
File metadata
- Download URL: dispatchery-0.2.1.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d2adfbb37fd5d095a62fecc1b6bc2e12db8a8d9eef31a6ce91e5bac9d64daf1d
|
|
| MD5 |
0f16f5ffa69a67ca8f63abe973a3f170
|
|
| BLAKE2b-256 |
62efa39c944f324316cd0dbb5e698c302593403c78a176c0757ec91c30b5b52e
|
File details
Details for the file dispatchery-0.2.1-py3-none-any.whl.
File metadata
- Download URL: dispatchery-0.2.1-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1ff3ab24444f08b072a7f8380e713dd1b121b32e2469800ecddeae84a44618d7
|
|
| MD5 |
17c71d611160925e96f9424c09784ed9
|
|
| BLAKE2b-256 |
7e9322461c54553c546354029d4e41627bed08ba9819d3846c9cb5aca26a0b20
|