Skip to main content
Donate to the Python Software Foundation or Purchase a PyCharm License to Benefit the PSF! Donate Now

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

This version
History Node

0.1

Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
chatora.dispatch-0.1-py37-none-any.whl (8.3 kB) Copy SHA256 hash SHA256 Wheel py37
chatora.dispatch-0.1.tar.gz (8.0 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page