The missing switch statement
Project description
Switch
Changelog
v1.0.3: Maintenance release, no significant code changes
v1.0.2: 100% unit test coverage
Simple example:
from switch import Switch
def simple_example(val):
values = []
with Switch(val) as case:
if case(1):
values.append('Found 1')
if case(2):
values.append('Found 2')
return values
assert simple_example(1) == ['Found 1']
assert simple_example(2) == ['Found 2']
assert simple_example('anything else') == []
Simple example with default case:
from switch import Switch
def simple_example_with_default(val):
values = []
with Switch(val) as case:
if case(1):
values.append('Found 1')
if case(2):
values.append('Found 2')
if case.default:
values.append('No love for 1 or 2?')
return values
assert simple_example_with_default(1) == ['Found 1']
assert simple_example_with_default(2) == ['Found 2']
assert simple_example_with_default('anything else') == ['No love for 1 or 2?']
Fall through example:
from switch import Switch
def fall_through_example(val):
values = []
with Switch(val) as case:
if case(1, fall_through=True):
values.append('Found 1')
if case(2):
values.append('Found 2')
if case.default:
values.append('No love for 1 or 2?')
return values
assert fall_through_example(1) == ['Found 1', 'Found 2']
assert fall_through_example(2) == ['Found 2']
assert fall_through_example('anything else') == ['No love for 1 or 2?']
Cases can have callable test:
from switch import Switch
def ouh_callable_too(val):
values = []
with Switch(val) as case:
if case(1):
values.append('Found 1')
if case.call(lambda v: v < 100):
values.append('Found <100')
if case.default:
values.append('No love for anything lower than 100?')
return values
assert ouh_callable_too(1) == ['Found 1']
assert ouh_callable_too(50) == ['Found <100']
assert ouh_callable_too('anything else') == ['No love for anything lower than 100?']
Fall through by default:
from switch import CSwitch, Switch
def fall_through_by_default(val):
values = []
with Switch(val, fall_through=True) as case:
if case(1):
values.append('Found 1')
if case(2):
values.append('Found 2')
if case(3, fall_through=False):
values.append('Found 3')
if case(4):
values.append('Found 4')
if case.default:
values.append('No love for 1, 2, 3 or 4?')
return values
def cswitch_shortcut(val):
values = []
with CSwitch(val) as case:
if case(1):
values.append('Found 1')
if case(2):
values.append('Found 2')
if case(3, fall_through=False):
values.append('Found 3')
if case(4):
values.append('Found 4')
if case.default:
values.append('No love for 1, 2, 3 or 4?')
return values
assert fall_through_by_default(1) == ['Found 1', 'Found 2', 'Found 3']
assert fall_through_by_default(2) == ['Found 2', 'Found 3']
assert fall_through_by_default(3) == ['Found 3']
assert fall_through_by_default(4) == ['Found 4']
assert fall_through_by_default('anything else') == ['No love for 1, 2, 3 or 4?']
assert cswitch_shortcut(1) == fall_through_by_default(1)
assert cswitch_shortcut(2) == fall_through_by_default(2)
assert cswitch_shortcut(3) == fall_through_by_default(3)
assert cswitch_shortcut(4) == fall_through_by_default(4)
assert cswitch_shortcut('anything else') == fall_through_by_default('anything else')
Having a case after a default is a bad thing:
Unless some case executes early and finishes without fall through.
from switch import Switch
def case_after_default_is_baad(val):
values = []
with Switch(val) as case:
if case(1):
values.append('Found 1')
if case.default:
values.append('Found default')
if case('this is baad'):
values.append('Should not happen!')
return values
assert case_after_default_is_baad(1) == ['Found 1']
try:
case_after_default_is_baad('this is baad')
assert False
except SyntaxError:
assert 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 Distributions
switch-1.0.3.zip
(5.7 kB
view details)
switch-1.0.3.tar.gz
(3.8 kB
view details)
File details
Details for the file switch-1.0.3.zip
.
File metadata
- Download URL: switch-1.0.3.zip
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 42a268de04cffc33a32c4aaf6611af32fe77aaf192809dc66296b540a9df9dc9 |
|
MD5 | 84de4bf75f356909c31b3f755ed56c22 |
|
BLAKE2b-256 | 660a196319d23ded5025bced3306348d430aca0375d4fe09ef1b30ffb41a8f51 |
File details
Details for the file switch-1.0.3.tar.gz
.
File metadata
- Download URL: switch-1.0.3.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 785e80b91e45454b7ea767e9f5b0861b912e50750a78f7bf40fef6686ef545ef |
|
MD5 | bd40ec9cee005794937e47145e9f9e11 |
|
BLAKE2b-256 | 7c98b0140ad13480598464ecf955f87dca2e966fe7a729e51c38d69a1f4e4de7 |