Efficient dispatch-based calling, that might be a switch statement in another language.
Project description
Switcheroo
Efficient dispatch-based calling, that might be a switch statement in another language.
short usage
from switcheroo import Switch
switch = Switch({
'foo': lambda x: x+1,
})
>>> switch['foo'](1) 2
>>> switch['bar'](1) Traceback (most recent call last): ... KeyError: 'bar'
from switcheroo import Switch, default
switch = Switch({
'foo': lambda x: x+1,
default: lambda x: x-1,
})
>>> switch['foo'](1) 2
>>> switch['bar'](1) 0
decorator usage
from switcheroo import Switch
switch = Switch()
@switch.handles('foo')
def handle_foo(x):
return x+1
@switch.default
def handle_others(x):
return x-1
>>> switch['foo'](1) 2
>>> switch['bar'](1) 0
class helper usage
class MoarThingz(object):
switch = Switch()
def __init__(self, state):
self.state = state
@switch.handles('foo')
def handle_foo(self, x):
return self.state - x
@switch.default
def handle_foo(self, x):
return self.state + x
def dispatch(self, case, factor, x):
return factor * self.switch[case](self, x)
>>> things = MoarThingz(3) >>> things.dispatch('foo', factor=1, x=1) 2 >>> things.dispatch('bar', factor=-1, x=2) -5
subclass usage
from switcheroo import Switch, handles, default
class MySwitch(Switch):
@handles('foo')
def handles(x):
return x+1
@default
def default(x):
return x-1
>>> MySwitch['foo'](1) 2 >>> MySwitch['bar'](1) 0
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
switcheroo-0.2.0.tar.gz
(2.4 kB
view hashes)
Built Distribution
Close
Hashes for switcheroo-0.2.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d3943740265f7b391c43e52159edc88037f5c1190980d62def55307b3c1ac15a |
|
MD5 | 9c4042c6b3a26f3afcdab42077591672 |
|
BLAKE2b-256 | e4fa77b0c058e6c7c90ac353e41b52c7f1a0135c4f9137c132a3744bcf3874bd |