A flexible Python function dispatcher with rules for values, kwargs, and types.
Project description
ATdispatcher
ATdispatcher is a flexible Python dispatcher library for creating functions and methods with multiple options (overloads), default arguments, type checking, and automatic handling of instance attributes for methods. It allows a simple API to manage multiple variations of the same function or method.
Features
- Dispatch multiple function variations under the same name.
- Support for default arguments.
- Support for type hints to select the correct variant.
- Dispatch methods with automatic
selfandSelfAttrhandling. - Simple API with
@dispatcherfor functions and@method_dispatcherfor methods. - Easily extendable with multiple registrations using
.reg().
Installation
Currently, ATdispatcher is a standalone module. You can include it in your project directly:
git clone https://github.com/avitwil/ATdispatcher.git
Or copy ATdispatcher.py into your project directory.
Usage
Importing
from ATdispatcher import dispatcher, method_dispatcher, SelfAttr
1. Function Dispatching
@dispatcher
def func(a: int, b: int):
return a + b
@func.reg()
def _(a: int, b: int, c: int):
return a + b + c
@func.reg()
def _(a: int, b: int, c: int = 3):
return a * b * c
print(func(5, 6)) # Output: 11 (matches first variant)
print(func(5, 6, 7)) # Output: 18 (matches second variant)
print(func(5, 6, 3)) # Output: 90 (matches third variant with default)
✅ Notes:
- You can register multiple variants with different signatures using
.reg(). - Type hints are used to select the correct variant.
- Default arguments are supported.
2. Method Dispatching with SelfAttr
SelfAttr allows method defaults to refer to instance attributes automatically.
class MyClass:
def __init__(self):
self.mult = 2
@method_dispatcher
def method(self, x: int, y: int = SelfAttr("mult")):
return x * y
obj = MyClass()
print(obj.method(10)) # Output: 20 (y defaults to obj.mult)
print(obj.method(10, 5)) # Output: 50 (y explicitly passed)
✅ Notes:
SelfAttr("attr_name")automatically fetchesself.attr_nameas the default.- Works with multiple method registrations using
.reg()as well.
3. Multiple Method Variants
class MyClass:
def __init__(self):
self.mult = 3
@method_dispatcher
def calc(self, x: int):
return x * 2
@calc.reg()
def _(self, x: int, y: int = SelfAttr("mult")):
return x * y
obj = MyClass()
print(obj.calc(5)) # Output: 10 (first variant)
print(obj.calc(5, 4)) # Output: 20 (second variant with y=4)
print(obj.calc(5, 3)) # Output: 15 (second variant, y defaults to obj.mult)
4. Error Handling
If no matching signature is found:
try:
func("a", 5)
except TypeError as e:
print(e) # Output: No matching function signature found
✅ Type checking ensures invalid calls raise TypeError.
5. API Reference
| Class / Function | Description |
|---|---|
dispatcher(func) |
Create a function dispatcher. |
.reg() |
Register a new variant for the same dispatcher. |
method_dispatcher(func) |
Create a method dispatcher for instance methods. |
SelfAttr("attr") |
Used for method default arguments that reference self attributes. |
6. Example: Combined Usage
from ATdispatcher import dispatcher, method_dispatcher, SelfAttr
@dispatcher
def add(a: int, b: int):
return a + b
@add.reg()
def _(a: int, b: int, c: int = 10):
return a + b + c
class Calc:
def __init__(self):
self.multiplier = 5
@method_dispatcher
def multiply(self, x: int, y: int = SelfAttr("multiplier")):
return x * y
calc = Calc()
print(add(1, 2)) # 3
print(add(1, 2, 3)) # 6
print(calc.multiply(4)) # 20
print(calc.multiply(4, 2))# 8
License
MIT License – free to use, modify, and distribute.
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-1.0.1.tar.gz.
File metadata
- Download URL: atdispatcher-1.0.1.tar.gz
- Upload date:
- Size: 5.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4fe41f4dd0cdb8fd554c772898af5d328a1fd140b907a561de0ffb7e3b0b6a36
|
|
| MD5 |
468bc81bebb7a7125ca56c03e21ee64c
|
|
| BLAKE2b-256 |
d5d60bfd96166a870138d37845b85524b2837dc1e808f4502cb1409664a8b9e8
|
File details
Details for the file atdispatcher-1.0.1-py3-none-any.whl.
File metadata
- Download URL: atdispatcher-1.0.1-py3-none-any.whl
- Upload date:
- Size: 5.8 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 |
c7db2e688599b939d53be8a657fb39943dceb451a10f4069f39e48f2e034bb34
|
|
| MD5 |
2e43ea5dd5c2ace2b545a0074e7c9b28
|
|
| BLAKE2b-256 |
9230dafca6633a5dd5a6cc0bb2859d21ed7fce1d6534d4bd9c8036b8336b98e8
|