Skip to main content

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:

pip install trouting

The logo of trouting was generated using 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:

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:

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

Metadata

Release files for trouting 0.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for trouting 0.3.3
File Size Uploaded
trouting-0.3.3.tar.gz 8.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for trouting 0.3.3
File Interpreter ABI Platform
trouting-0.3.3-py3-none-any.whl Python 3 none any Details

Total release size: 17.5 kB

Release files / trouting-0.3.3.tar.gz

Download URL trouting-0.3.3.tar.gz
Size 8.7 kB
Tags Source
SHA-256 checksum
How to use checksums
5e428647948025241223fc24ffbf3a0f29d8b50bd458855a266c811aa49a1c7f
BLAKE2b-256 checksum
How to use checksums
373972b80a5ccf0b83892cc1ac124a521186207a8334c161e93140654c6252c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.13

Release files / trouting-0.3.3-py3-none-any.whl

Download URL trouting-0.3.3-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8d23849c3b96ad889d0e3c1d60d5d510e195ad2463650cef3ca41ab0c651e106
BLAKE2b-256 checksum
How to use checksums
67be9f2e75b5a227dc0b3a20c49a0cecfc9628fc0d315656e80a2bc7380f5527
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.13

Release history Release notifications | RSS feed

This release

0.3.3 This release

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.2.0

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page