Skip to main content
https://travis-ci.org/canni/switch.svg?branch=v1.1.0 https://coveralls.io/repos/canni/switch/badge.png?branch=v1.1.0

Switch

Changelog

  • v1.1.0: No BC breaks, new features:
    • Multiple case test within single case (see examples)

    • Cases now support regexp matching (see examples)

  • v1.0.4: 15% performance improvement, no BC breaks

  • 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, 3):
            values.append('Found 2 or 3')

    return values

assert simple_example(1) == ['Found 1']
assert simple_example(2) == ['Found 2 or 3']
assert simple_example(3) == ['Found 2 or 3']
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, 3):
            values.append('Found 2 or 3')

        if case.default:
            values.append('No love for 1, 2 or 3?')

    return values

assert simple_example_with_default(1) == ['Found 1']
assert simple_example_with_default(2) == ['Found 2 or 3']
assert simple_example_with_default(3) == ['Found 2 or 3']
assert simple_example_with_default('anything else') == ['No love for 1, 2 or 3?']

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, 3):
            values.append('Found 2 or 3')

        if case.default:
            values.append('No love for 1, 2 or 3?')

    return values

assert fall_through_example(1) == ['Found 1', 'Found 2 or 3']
assert fall_through_example(2) == ['Found 2 or 3']
assert fall_through_example(3) == ['Found 2 or 3']
assert fall_through_example('anything else') == ['No love for 1, 2 or 3?']

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?']

Cases can have test against regexp:

from switch import Switch

def test_regexp(val):
    values = []

    with Switch(val) as case:
        if case(1):
            values.append('Found 1')

        if case.match(r'10|ten'):
            values.append('Found 10')

        if case.default:
            values.append('No love for 1 or 10?')

    return values

assert test_regexp(1) == ['Found 1']
assert test_regexp(10) == ['Found 10']
assert test_regexp('ten') == ['Found 10']
assert test_regexp('anything else') == ['No love for 1 or 10??']

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

Release files for switch 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distributions (sdists)

Source distribution for switch 1.1.0
File Size Uploaded
switch-1.1.0.zip 6.1 kB Details
switch-1.1.0.tar.gz 4.1 kB Details

Release files / switch-1.1.0.zip

Download URL switch-1.1.0.zip
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
d803027e8fa5c9b8c3b72e6f6cc00f565c5f8d58ea8220c462831f27d1f56ebd
BLAKE2b-256 checksum
How to use checksums
17eeba4f303c77343ef8f5a968cb987f3a697b9b003ab61dbfe2e412ba385b02
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / switch-1.1.0.tar.gz

Download URL switch-1.1.0.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
bc71e10d57bb0f8732cc5d060db0ecb004f65d21caa6a200675379f18eacb357
BLAKE2b-256 checksum
How to use checksums
e623485451334a8994b7afa9798517d15ca030bc638ee5cbd7fb3c086c5d6553
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

4 release files

1.0.1

3 release files

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page