Name | trouting JSON |
Version |
0.3.3
JSON |
| download |
home_page | |
Summary | Trouting (short for Type Routing) is a simple class decorator that allows to define multiple interfaces for a method that behave differently depending on input types. |
upload_time | 2023-01-17 01:23:33 |
maintainer | |
docs_url | None |
author | |
requires_python | >=3.8 |
license | Apache-2.0 |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# Trouting

Trouting (short for Type Routing) is a simple class decorator that allows to define multiple interfaces for a method that behave differently depending on input types.
To install for PyPI trouting, run:
```bash
pip install trouting
```
The logo of trouting was generated using [Stable Diffusion](https://github.com/CompVis/stable-diffusion) with prompt *"A Kandinsky painting titled The Trout Who Routes"* and slightly edited by the author.
## Example
Imagine you want to define a class whose method behaves differently depending on whether the input is a string or an integer. You can do this with trouting as follows:
```python
from typing import Any, Union
from trouting import trouting
class MyClass:
@trouting
def add_one(self, a: Any) -> Any:
# fallback method
raise TypeError(f"Type {type(a)} not supported")
@add_one.add_interface(a=(int, float))
def add_one_int(self, a: Union[int, float]) -> float:
# a is an int or float
return float(a + 1)
@add_one.add_interface(a=str)
def add_one_str(self, a: str) -> str:
# a is a str
return a + "1"
```
Now, when using `MyClass`, the method `add_one` will behave differently depending on the input type:
```python
my_class = MyClass()
my_class.add_one(1) # returns 2.0
my_class.add_one("1") # returns "11"
my_class.add_one([1]) # raises TypeError
```
Raw data
{
"_id": null,
"home_page": "",
"name": "trouting",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": "",
"keywords": "",
"author": "",
"author_email": "Luca Soldaini <luca@soldaini.net>",
"download_url": "https://files.pythonhosted.org/packages/37/39/72b80a5ccf0b83892cc1ac124a521186207a8334c161e93140654c6252c6/trouting-0.3.3.tar.gz",
"platform": null,
"description": "# Trouting\n\n\n\nTrouting (short for Type Routing) is a simple class decorator that allows to define multiple interfaces for a method that behave differently depending on input types.\n\nTo install for PyPI trouting, run:\n\n```bash\npip install trouting\n```\n\nThe logo of trouting was generated using [Stable Diffusion](https://github.com/CompVis/stable-diffusion) with prompt *\"A Kandinsky painting titled The Trout Who Routes\"* and slightly edited by the author.\n\n## Example\n\nImagine you want to define a class whose method behaves differently depending on whether the input is a string or an integer. You can do this with trouting as follows:\n\n```python\nfrom typing import Any, Union\nfrom trouting import trouting\n\nclass MyClass:\n @trouting\n def add_one(self, a: Any) -> Any:\n # fallback method\n raise TypeError(f\"Type {type(a)} not supported\")\n\n @add_one.add_interface(a=(int, float))\n def add_one_int(self, a: Union[int, float]) -> float:\n # a is an int or float\n return float(a + 1)\n\n @add_one.add_interface(a=str)\n def add_one_str(self, a: str) -> str:\n # a is a str\n return a + \"1\"\n```\n\nNow, when using `MyClass`, the method `add_one` will behave differently depending on the input type:\n\n```python\nmy_class = MyClass()\nmy_class.add_one(1) # returns 2.0\nmy_class.add_one(\"1\") # returns \"11\"\nmy_class.add_one([1]) # raises TypeError\n```\n",
"bugtrack_url": null,
"license": "Apache-2.0",
"summary": "Trouting (short for Type Routing) is a simple class decorator that allows to define multiple interfaces for a method that behave differently depending on input types.",
"version": "0.3.3",
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "67be9f2e75b5a227dc0b3a20c49a0cecfc9628fc0d315656e80a2bc7380f5527",
"md5": "f4dec946abf7b6a2af505592dcf06f1d",
"sha256": "8d23849c3b96ad889d0e3c1d60d5d510e195ad2463650cef3ca41ab0c651e106"
},
"downloads": -1,
"filename": "trouting-0.3.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "f4dec946abf7b6a2af505592dcf06f1d",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 8843,
"upload_time": "2023-01-17T01:23:31",
"upload_time_iso_8601": "2023-01-17T01:23:31.551335Z",
"url": "https://files.pythonhosted.org/packages/67/be/9f2e75b5a227dc0b3a20c49a0cecfc9628fc0d315656e80a2bc7380f5527/trouting-0.3.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "373972b80a5ccf0b83892cc1ac124a521186207a8334c161e93140654c6252c6",
"md5": "65fea3c445c06bda1b1c99f26fae5ca5",
"sha256": "5e428647948025241223fc24ffbf3a0f29d8b50bd458855a266c811aa49a1c7f"
},
"downloads": -1,
"filename": "trouting-0.3.3.tar.gz",
"has_sig": false,
"md5_digest": "65fea3c445c06bda1b1c99f26fae5ca5",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 8679,
"upload_time": "2023-01-17T01:23:33",
"upload_time_iso_8601": "2023-01-17T01:23:33.410927Z",
"url": "https://files.pythonhosted.org/packages/37/39/72b80a5ccf0b83892cc1ac124a521186207a8334c161e93140654c6252c6/trouting-0.3.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-01-17 01:23:33",
"github": false,
"gitlab": false,
"bitbucket": false,
"lcname": "trouting"
}