A Python package for advanced function dispatching based on complex, nested, and parameterized types. Inspired by functools.singledispatch.
Project description
Dispatchery 🧙♂️✨
Dispatch your functions based on complex types.
dispatchery is a very simple Python package that extends the standard singledispatch decorator, allowing your functions to handle complex, nested, and parameterized types. With dispatchery, you can dispatch based on types like tuple[int, str, dict[str, int]] or list[dict[str, list[int]]] without the need for cumbersome isinstance checks.
Features
- Advanced Type Dispatching: Supports complex and nested generic types.
- Recursive Type Matching: Handles nested types like
tuple[int, str, dict[str, int]]. - 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.
Example
Suppose we want a function, process, that behaves differently based on complex types like tuple[int, str], list[str], or dict[str, int]. It also works with nested types like list[tuple[int, dict[str, float]]].
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(list[tuple[int, dict[str, tuple[list[bool], dict[str, float]]]]])
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([(1, {"a": ([True, False], {"x": 3.14})})]))
# "Now this is just getting silly."
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.1.0.tar.gz.
File metadata
- Download URL: dispatchery-0.1.0.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e11a9097d3fbe2f874fb528edd3b0d46d3bb5838c42ed4990720998722a79531
|
|
| MD5 |
13b4b16e42d24b8f5801d52446cf459c
|
|
| BLAKE2b-256 |
b6f1a71a2882eb463924593f395a6f0e3a040271afd4ef1d25494f13137327b2
|
File details
Details for the file dispatchery-0.1.0-py3-none-any.whl.
File metadata
- Download URL: dispatchery-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.4 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 |
a206f2e10988af36c5d393125752864df1a77d71abbd53bed5296a50da703ab7
|
|
| MD5 |
f9aaded843180d51c525bca968c69f80
|
|
| BLAKE2b-256 |
2b41ca3f3bab4e2bca3c42849c79eaf6e7866c5cfbd0499d2d65a5125c13ac1f
|