The missing switch statement
Project description
Switch
Default behaviour
def test_switch(val):
ret = []
with Switch(val) as case:
if case(1, fall_through=True):
ret.append(1)
if case(2):
ret.append(2)
if case.call(lambda v: 2 < v < 4):
ret.append(3)
if case.call(lambda v: 3 < v < 5, fall_through=True):
ret.append(4)
if case(5):
ret.append(5)
if case.default:
ret.append(6)
return ret
assert test_switch(1) == [1, 2]
assert test_switch(2) == [2]
assert test_switch(3) == [3]
assert test_switch(4) == [4, 5]
assert test_switch(5) == [5]
assert test_switch(7) == [6]
Default fallthrough
from switch import Switch
def test_switch(val):
ret = []
with Switch(val, fall_through=True) as case:
if case(1):
ret.append(1)
if case(2):
ret.append(2)
if case.call(lambda v: 2 < v < 4):
ret.append(3)
if case.call(lambda v: 3 < v < 5, fall_through=False):
ret.append(4)
if case(5):
ret.append(5)
if case.default:
ret.append(6)
return ret
assert test_switch(1) == [1, 2, 3, 4]
assert test_switch(2) == [2, 3, 4]
assert test_switch(3) == [3, 4]
assert test_switch(4) == [4]
assert test_switch(5) == [5]
assert test_switch(7) == [6]
C like switch shortcut
from switch import CSwitch
# CSwitch(valk) is equvalent to Switch(val, fall_through=True)
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
switch-1.0.0.tar.gz
(2.9 kB
view details)
File details
Details for the file switch-1.0.0.tar.gz
.
File metadata
- Download URL: switch-1.0.0.tar.gz
- Upload date:
- Size: 2.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1398682f60a0d2d267d85ad7884dd9c516ceb5a45d179aba6d4295a77963d680 |
|
MD5 | de9449e09f72110e101a7d006d832cd4 |
|
BLAKE2b-256 | 03cbea3ea162776064aade5f385048f7beae9eebf8b6113a794913800d3a1752 |