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 hashes)