A flexible Python function dispatcher with rules for values, kwargs, and types.
Project description
ATdispatcher
ATdispatcher is a flexible Python dispatcher for functions and methods, enabling dynamic function selection based on argument values, types, or keyword presence.
📦 Installation
Install via pip:
pip install ATdispatcher
Or install directly from GitHub:
pip install git+https://github.com/avitwil/ATdispatcher.git
🧩 Overview
ATdispatcher allows you to define custom rules to select which function to call dynamically based on:
- Argument values
- Argument types
- Presence of specific keyword arguments
The package works for both standalone functions and class methods.
🚀 Basic Usage
from ATdispatcher import dispatcher
@dispatcher
def greet(*args, **kwargs):
raise NotImplementedError("No matching rule")
# Dispatch based on argument values and types
@greet.values(name="avi").types(str, int)
def _(name: str, age: int):
return f"{name} is {age} years old"
# Dispatch based on keyword arguments
@greet.kwargs("name", "age")
def _(name: str, age: int):
return f"{name} is {age} years old"
print(greet("avi", 40)) # Output: avi is 40 years old
🛠️ Usage with Methods
class Person:
@dispatcher
def introduce(self, *args, **kwargs):
raise NotImplementedError("No matching rule")
@introduce.values(name="avi").types(str, int)
def _(self, name: str, age: int):
return f"{name} is {age} years old"
@introduce.kwargs("name", "age")
def _(self, name: str, age: int):
return f"{name} is {age} years old"
p = Person()
print(p.introduce("avi", 40)) # Output: avi is 40 years old
🔍 Key Features
- Flexible dispatching: Add custom rules using
add_rule. - Supports methods: Works as a decorator for class methods.
- Rule builders: Match rules by values, types, or keyword presence.
- Readable syntax: Easy-to-understand, clean code style.
- Chaining support: Define multiple rules for the same dispatcher in a readable way.
📚 Documentation & Resources
- GitHub Repository: https://github.com/avitwil/ATdispatcher
- License: MIT License (see LICENSE file)
⚡ Example: Combined Rules
@dispatcher
def process(*args, **kwargs):
raise NotImplementedError("No matching rule")
# Match a specific value
@process.values(task="email").types(str)
def _(task):
return f"Processing task: {task}"
# Match based on keyword presence
@process.kwargs("task", "priority")
def _(task, priority):
return f"Processing {task} with priority {priority}"
print(process("email")) # Processing task: email
print(process(task="backup", priority="high")) # Processing backup with priority high
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
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 atdispatcher-0.1.0.tar.gz.
File metadata
- Download URL: atdispatcher-0.1.0.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
80035ab0c3893d8c87a8f22da3b0ea060d57e8d0c16efcc78a5c146b2e9f6891
|
|
| MD5 |
a8f3ac8212b7ba1fa16745c52353876a
|
|
| BLAKE2b-256 |
b7dd0f958d40b8370cde9c1571b30fa8e52492a41e27df4ffa04d1477f802726
|
File details
Details for the file atdispatcher-0.1.0-py3-none-any.whl.
File metadata
- Download URL: atdispatcher-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
738020907b2d1cb824eee01eb2b84e07b5ea24db6199de57cff8a2f986f02cdf
|
|
| MD5 |
3fad101d5ab8f5865d2774fb840d9697
|
|
| BLAKE2b-256 |
d5898c97b8b1a4e72ecae59c6e4cad584e09446ac54ccbb3ad16e56bf1e38b20
|