Multiple argument dispatching for Python.
Project description
chatora.dispatch
Multiple argument dispatching for Python.
Usage
Transform a function into a dispatch generic function, such as the functools.singledispatch. Unlike the functools.singledispatch, it supports multi-dispatch.
from chatora.dispatch.api import dispatch
@dispatch
def func(arg0, arg1):
return '1st func'
assert func(0, 0) == '1st func'
assert func('0', '0') == '1st func'
@func.register
def _(arg0, arg1: int):
return '2nd func'
assert func(0, 0) == '2nd func'
assert func('0', 0) == '2nd func'
assert func('0', '0') == '1st func'
@func.register
def _(arg0: int, arg1: int):
return '3rd func'
assert func(0, 0) == '3rd func'
assert func('0', 0) == '2nd func'
assert func('0', '0') == '1st func'
It partially supports arguments with typing.Union, typing.Optioanl and typing.Any. typing.Any is equivalent to empty annotation.
from chatora.dispatch.api import dispatch
import typing
@dispatch
def func(arg0: typing.Any, arg1: typing.Any):
return '1st func'
@func.register
def _(arg0: typing.Optional[str], arg1: typing.Union[str, list, tuple]):
return '2nd func'
assert func(0, 0) == '1st func'
assert func('0', 0) == '1st func'
assert func('0', '0') == '2nd func'
assert func('0', []) == '2nd func'
assert func(None, ()) == '2nd func'
It also partially supports return type annotation.
from chatora.dispatch.api import dispatch
import typing
class ResultTuple(tuple):
def __new__(cls, *args):
return super().__new__(cls, args)
class ResultClass:
def __init__(self, a, b):
self.a, self.b = a, b
@dispatch
def func(arg0: typing.Optional[str], arg1: tuple):
return '1st func'
@func.register
def _(arg0: typing.Optional[str], arg1: tuple) -> typing.Sequence[str]:
return ['2nd', 'func']
@func.register
def _(arg0: typing.Optional[str], arg1: tuple) -> typing.Tuple[str]:
return ('3rd', 'func')
@func.register
def _(arg0: typing.Optional[str], arg1: tuple) -> ResultTuple:
return ResultTuple('4th', 'func')
@func.register
def _(arg0: typing.Optional[str], arg1: tuple) -> ResultClass:
return ResultClass('4th', 'func')
assert func('0', ()) == '1st func'
assert func('0', (), _return_type=typing.Sequence[str]) == ['2nd', 'func']
assert func('0', (), _return_type=typing.Tuple[str]) == ('3rd', 'func')
assert func('0', (), _return_type=ResultTuple) == ResultTuple('4th', 'func')
assert isinstance(func('0', (), _return_type=ResultClass), ResultClass)
Changelog
0.1 (2019-05-01)
- Birth!
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 chatora.dispatch-0.1.tar.gz.
File metadata
- Download URL: chatora.dispatch-0.1.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
551cee894149404538642eeef544fb937b4843c596d6be97de6f0bc4796f1307
|
|
| MD5 |
e32041997cd33fe973323322551f0abb
|
|
| BLAKE2b-256 |
df493e51a039ce9ca0b6b027782d0cc71cc7e24fe03aaa208e9c59530df99b13
|
File details
Details for the file chatora.dispatch-0.1-py37-none-any.whl.
File metadata
- Download URL: chatora.dispatch-0.1-py37-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3.7
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a2767494945542334975a3953de612131f43a9ae0e05807ea5046e9542c158b6
|
|
| MD5 |
0bd82ae1927a61d1086071d57b6f0edb
|
|
| BLAKE2b-256 |
d07481444c4ba2733ea111d5b2870ed90fb1ae356b998949ff535623ac3af588
|