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
explicit usage
from switcheroo import Switch
def handle_foo(x):
return x+1
def handle_others(x):
return x-1
switch = Switch()
switch.register('foo', handler=handle_foo)
switch.default(handle_others)
>>> switch.lookup('foo')(1)
2
>>> switch.lookup('bar')(1)
0
>>> switch.override('foo', lambda x: x+2)
>>> switch.lookup('foo')(1)
3
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
@switch.overrides('foo')
def new_handle_foo(x):
return x+2
>>> switch['foo'](1) 3
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
changes
2.0.0 (20 Sep 2022)
Drop Python 2 support.
1.1.0 (26 Nov 2020)
Add support for overrides.
Add support for more explicit usage.
1.0.0 (27 Feb 2019)
100% coverage checking and automated releases.
0.2.0 (13 Dec 2018)
Handle subclasses when using the subclass pattern.
0.1.0 (24 Nov 2018)
Initial release.
Metadata
Release files for switcheroo 2.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| switcheroo-2.0.0.tar.gz | 3.7 kB | Without provenance | Details for switcheroo-2.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| switcheroo-2.0.0-py3-none-any.whl | Python 3 | none | any | Details for switcheroo-2.0.0-py3-none-any.whl |
Total release size: 7.1 kB
Release files / switcheroo-2.0.0.tar.gz
| Download URL | switcheroo-2.0.0.tar.gz |
|---|---|
| Size | 3.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7963632cd636bb8a70bc3d298d45bd7aff7c11b9bbd55d7336caf9d2fd633557
|
|
BLAKE2b-256 checksum How to use checksums |
357118c78722f16254693eb453a3abfee8d3dcd79449c73c81d9b9ae7ab62ba8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.13 CPython/3.9.13 Linux/5.13.0-1023-aws
|
Release files / switcheroo-2.0.0-py3-none-any.whl
| Download URL | switcheroo-2.0.0-py3-none-any.whl |
|---|---|
| Size | 3.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b9ca49a88ad3dd0b74aeee30459336131b99fca32c229273b6482121d05e047b
|
|
BLAKE2b-256 checksum How to use checksums |
e83c32f784b366e9b0b378f3bbe3154f40f43fba03c44a831056d240cebf1f97
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.13 CPython/3.9.13 Linux/5.13.0-1023-aws
|
Security for switcheroo 2.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.