Skip to main content

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.

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

trouting-0.2.2.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

trouting-0.2.2-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file trouting-0.2.2.tar.gz.

File metadata

  • Download URL: trouting-0.2.2.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.13

File hashes

Hashes for trouting-0.2.2.tar.gz
Algorithm Hash digest
SHA256 7e38099016908702afbb02d54b7da463112e0da0942729a73f770fd630687f33
MD5 99bc3d2876dcfce451f4452f2ea1613d
BLAKE2b-256 3730d9e4cd9f29f45698de3f1d1d47327b56c3fb7341d9f1204182f17be0a8ae

See more details on using hashes here.

File details

Details for the file trouting-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: trouting-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.13

File hashes

Hashes for trouting-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1f15f6736a31b37cba7e4c5b9e04f9b000433d78b9c835c8b67c94d83d72091e
MD5 3f9383d1fe83b9d11da877a3b1a253d0
BLAKE2b-256 5c0edcf3073707495b9ca62b7be5611f829f57236fd015c178e9a3171cad08a6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page