# 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 `self` and `SelfAttr` handling.
* Simple API with `@dispatcher` for functions and `@method_dispatcher` for methods.
* Easily extendable with multiple registrations using `.reg()`.
---
## Installation
Currently, ATdispatcher is a standalone module. You can include it in your project directly:
```bash
git clone https://github.com/avitwil/ATdispatcher.git
```
Or copy `ATdispatcher.py` into your project directory.
---
## Usage
### Importing
```python
from ATdispatcher import dispatcher, method_dispatcher, SelfAttr
```
---
### 1. Function Dispatching
```python
@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.
```python
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 fetches `self.attr_name` as the default.
* Works with multiple method registrations using `.reg()` as well.
---
### 3. Multiple Method Variants
```python
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:
```python
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
```python
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.
Raw data
{
"_id": null,
"home_page": null,
"name": "ATdispatcher",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "dispatcher, function routing, dynamic dispatch, python",
"author": null,
"author_email": "Avi Twil <avitwil@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/d5/d6/0bfd96166a870138d37845b85524b2837dc1e808f4502cb1409664a8b9e8/atdispatcher-1.0.1.tar.gz",
"platform": null,
"description": "\r\n# ATdispatcher\r\n\r\n**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.\r\n\r\n---\r\n\r\n## Features\r\n\r\n* Dispatch multiple **function variations** under the same name.\r\n* Support for **default arguments**.\r\n* Support for **type hints** to select the correct variant.\r\n* Dispatch **methods** with automatic `self` and `SelfAttr` handling.\r\n* Simple API with `@dispatcher` for functions and `@method_dispatcher` for methods.\r\n* Easily extendable with multiple registrations using `.reg()`.\r\n\r\n---\r\n\r\n## Installation\r\n\r\nCurrently, ATdispatcher is a standalone module. You can include it in your project directly:\r\n\r\n```bash\r\ngit clone https://github.com/avitwil/ATdispatcher.git\r\n```\r\n\r\nOr copy `ATdispatcher.py` into your project directory.\r\n\r\n---\r\n\r\n## Usage\r\n\r\n### Importing\r\n\r\n```python\r\nfrom ATdispatcher import dispatcher, method_dispatcher, SelfAttr\r\n```\r\n\r\n---\r\n\r\n### 1. Function Dispatching\r\n\r\n```python\r\n@dispatcher\r\ndef func(a: int, b: int):\r\n return a + b\r\n\r\n@func.reg()\r\ndef _(a: int, b: int, c: int):\r\n return a + b + c\r\n\r\n@func.reg()\r\ndef _(a: int, b: int, c: int = 3):\r\n return a * b * c\r\n\r\nprint(func(5, 6)) # Output: 11 (matches first variant)\r\nprint(func(5, 6, 7)) # Output: 18 (matches second variant)\r\nprint(func(5, 6, 3)) # Output: 90 (matches third variant with default)\r\n```\r\n\r\n\u2705 Notes:\r\n\r\n* You can register multiple variants with different signatures using `.reg()`.\r\n* Type hints are used to select the correct variant.\r\n* Default arguments are supported.\r\n\r\n---\r\n\r\n### 2. Method Dispatching with `SelfAttr`\r\n\r\n`SelfAttr` allows method defaults to refer to instance attributes automatically.\r\n\r\n```python\r\nclass MyClass:\r\n def __init__(self):\r\n self.mult = 2\r\n\r\n @method_dispatcher\r\n def method(self, x: int, y: int = SelfAttr(\"mult\")):\r\n return x * y\r\n\r\nobj = MyClass()\r\n\r\nprint(obj.method(10)) # Output: 20 (y defaults to obj.mult)\r\nprint(obj.method(10, 5)) # Output: 50 (y explicitly passed)\r\n```\r\n\r\n\u2705 Notes:\r\n\r\n* `SelfAttr(\"attr_name\")` automatically fetches `self.attr_name` as the default.\r\n* Works with multiple method registrations using `.reg()` as well.\r\n\r\n---\r\n\r\n### 3. Multiple Method Variants\r\n\r\n```python\r\nclass MyClass:\r\n def __init__(self):\r\n self.mult = 3\r\n\r\n @method_dispatcher\r\n def calc(self, x: int):\r\n return x * 2\r\n\r\n @calc.reg()\r\n def _(self, x: int, y: int = SelfAttr(\"mult\")):\r\n return x * y\r\n\r\nobj = MyClass()\r\n\r\nprint(obj.calc(5)) # Output: 10 (first variant)\r\nprint(obj.calc(5, 4)) # Output: 20 (second variant with y=4)\r\nprint(obj.calc(5, 3)) # Output: 15 (second variant, y defaults to obj.mult)\r\n```\r\n\r\n---\r\n\r\n### 4. Error Handling\r\n\r\nIf no matching signature is found:\r\n\r\n```python\r\ntry:\r\n func(\"a\", 5)\r\nexcept TypeError as e:\r\n print(e) # Output: No matching function signature found\r\n```\r\n\r\n\u2705 Type checking ensures invalid calls raise `TypeError`.\r\n\r\n---\r\n\r\n### 5. API Reference\r\n\r\n| Class / Function | Description |\r\n| ------------------------- | ------------------------------------------------------------------- |\r\n| `dispatcher(func)` | Create a function dispatcher. |\r\n| `.reg()` | Register a new variant for the same dispatcher. |\r\n| `method_dispatcher(func)` | Create a method dispatcher for instance methods. |\r\n| `SelfAttr(\"attr\")` | Used for method default arguments that reference `self` attributes. |\r\n\r\n---\r\n\r\n### 6. Example: Combined Usage\r\n\r\n```python\r\nfrom ATdispatcher import dispatcher, method_dispatcher, SelfAttr\r\n\r\n@dispatcher\r\ndef add(a: int, b: int):\r\n return a + b\r\n\r\n@add.reg()\r\ndef _(a: int, b: int, c: int = 10):\r\n return a + b + c\r\n\r\nclass Calc:\r\n def __init__(self):\r\n self.multiplier = 5\r\n\r\n @method_dispatcher\r\n def multiply(self, x: int, y: int = SelfAttr(\"multiplier\")):\r\n return x * y\r\n\r\ncalc = Calc()\r\n\r\nprint(add(1, 2)) # 3\r\nprint(add(1, 2, 3)) # 6\r\nprint(calc.multiply(4)) # 20\r\nprint(calc.multiply(4, 2))# 8\r\n```\r\n\r\n---\r\n\r\n### License\r\n\r\nMIT License \u2013 free to use, modify, and distribute.\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A flexible Python function dispatcher with rules for values, kwargs, and types.",
"version": "1.0.1",
"project_urls": {
"Homepage": "https://github.com/avitwil/ATdispatcher",
"Issues": "https://github.com/avitwil/ATdispatcher/issues",
"Repository": "https://github.com/avitwil/ATdispatcher"
},
"split_keywords": [
"dispatcher",
" function routing",
" dynamic dispatch",
" python"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "9230dafca6633a5dd5a6cc0bb2859d21ed7fce1d6534d4bd9c8036b8336b98e8",
"md5": "2e43ea5dd5c2ace2b545a0074e7c9b28",
"sha256": "c7db2e688599b939d53be8a657fb39943dceb451a10f4069f39e48f2e034bb34"
},
"downloads": -1,
"filename": "atdispatcher-1.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "2e43ea5dd5c2ace2b545a0074e7c9b28",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 5845,
"upload_time": "2025-08-27T18:07:38",
"upload_time_iso_8601": "2025-08-27T18:07:38.635346Z",
"url": "https://files.pythonhosted.org/packages/92/30/dafca6633a5dd5a6cc0bb2859d21ed7fce1d6534d4bd9c8036b8336b98e8/atdispatcher-1.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "d5d60bfd96166a870138d37845b85524b2837dc1e808f4502cb1409664a8b9e8",
"md5": "468bc81bebb7a7125ca56c03e21ee64c",
"sha256": "4fe41f4dd0cdb8fd554c772898af5d328a1fd140b907a561de0ffb7e3b0b6a36"
},
"downloads": -1,
"filename": "atdispatcher-1.0.1.tar.gz",
"has_sig": false,
"md5_digest": "468bc81bebb7a7125ca56c03e21ee64c",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 5021,
"upload_time": "2025-08-27T18:07:39",
"upload_time_iso_8601": "2025-08-27T18:07:39.570281Z",
"url": "https://files.pythonhosted.org/packages/d5/d6/0bfd96166a870138d37845b85524b2837dc1e808f4502cb1409664a8b9e8/atdispatcher-1.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-08-27 18:07:39",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "avitwil",
"github_project": "ATdispatcher",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "atdispatcher"
}