typedispatch
typedispatch is a Python library that provides a flexible and powerful mechanism for registering functions based on type resolution. It allows you to associate types with functions and dynamically look up functions based on the type of an object, including handling inheritance hierarchies and predicate-based dispatch.
Features
- Register functions for specific types
- Automatic type resolution based on inheritance
- Decorator syntax for easy registration
- Predicate-based function dispatch
- Supports multiple inheritance
Installation
pip install typedispatch
Usage
Basic Registration
You can register functions directly with the register method:
from typedispatch import TypeDispatch, TypeDispatchError
typedispatch = TypeDispatch()
typedispatch.register(int, lambda x: f"Processing integer: {x}")
print(typedispatch.lookup(10)) # Output: Processing integer: 10
Using the Decorator
The library provides a convenient decorator for registering functions with specific types:
from typedispatch import TypeDispatch
typedispatch = TypeDispatch()
@typedispatch.register(int)
def handle_integer(x):
return f"Handling integer: {x}"
@typedispatch.register(str)
def handle_string(x):
return f"Handling string: {x}"
print(typedispatch.lookup(42)) # Output: Handling integer: 42
print(typedispatch.lookup("hello")) # Output: Handling string: hello
Inheritance-based Dispatch
It automatically handles inheritance hierarchies:
typedispatch.register(list, lambda x: f"List of length {len(x)}")
class MyList(list):
pass
print(typedispatch.lookup(MyList([1, 2, 3]))) # Output: List of length 3
Predicate-based Dispatch
You can use predicates to further refine the dispatch logic:
@typedispatch.register(int, a=True)
def handle_a_ints(x):
return f"A integer {x}"
@typedispatch.register(int, b=True)
def handle_b_ints(x):
return f"B integer {x}"
class MyInt(int):
pass
@typedispatch.register(MyInt, a=True)
def handle_a_myints(x):
return f"A MyInt {x}"
print(typedispatch.lookup(2, a=True)) # Output: A integer 2
print(typedispatch.lookup(2, b=True)) # Output: B integer 2
print(typedispatch.lookup(MyInt(2), a=True)) # Output: A MyInt 2
print(typedispatch.lookup(MyInt(2), b=True)) # Output: B integer 2
Error Handling
When no matching function is found, a TypeDispatchError is raised:
try:
typedispatch.lookup(3.14) # Assuming no function registered for float
except TypeDispatchError as e:
print(f"Error: {e}") # Output: Error: No function found for type <class 'float'> or its superclasses.
API Reference
TypeDispatch
-
register(obj_type: Type, func: Callable = None, **predicate: Any) -> Callable: Register a function for a specific type. If called with a function, it registers it immediately; if called with only a type, it returns a decorator for registering functions. -
lookup(obj: Any, **predicate: Dict[str, Any]) -> Any: Find and execute the function associated with the object's type and optional predicates. -
is_registered(obj_type: Type) -> bool: Check if a type or any of its superclasses is registered in the TypeDispatch.
Exceptions
TypeDispatchError: Raised when no matching function is found or when an invalid type is registered.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License.
Metadata
Release files for typedispatch 1.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| typedispatch-1.1.0.tar.gz | 5.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| typedispatch-1.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.3 kB
Release files / typedispatch-1.1.0.tar.gz
| Download URL | typedispatch-1.1.0.tar.gz |
|---|---|
| Size | 5.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
665324999941223fcb518cd707fa8602d73a4186b2c80a64173844db4835d24a
|
|
BLAKE2b-256 checksum How to use checksums |
39634231ca523dc5eadc09267552661de8e20e73535705c7436481488bc228bd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.20
|
Release files / typedispatch-1.1.0-py3-none-any.whl
| Download URL | typedispatch-1.1.0-py3-none-any.whl |
|---|---|
| Size | 6.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1c2fa782b789a31a2576c68a67f2a6960cd86ac8e69c13d26ed3bced531ae278
|
|
BLAKE2b-256 checksum How to use checksums |
9666e85caeaf8ca26d4062924b58a774e1514ae27e0a814ac856a983b4ae4e77
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.20
|