Skip to main content

The missing switch statement

Project description

https://travis-ci.org/canni/switch.svg?branch=v1.0.1

Switch

Default behaviour

from switch import Switch

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 Distributions

switch-1.0.1.zip (3.3 kB view details)

Uploaded Source

switch-1.0.1.tar.gz (2.6 kB view details)

Uploaded Source

Built Distribution

switch-1.0.1-py27-none-any.whl (3.2 kB view details)

Uploaded Python 2.7

File details

Details for the file switch-1.0.1.zip.

File metadata

  • Download URL: switch-1.0.1.zip
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for switch-1.0.1.zip
Algorithm Hash digest
SHA256 e2af1ad0ecf568cea97b4c5f37968a2f2b976cae7d016a45cd30f8a3591fa175
MD5 f5c74f2c152eeb09c6cc7fb4a7bd4057
BLAKE2b-256 ec5ce17bb531ad14efcc0a830f8d8bde63e9e8109f862ccafaa31f01603c69ef

See more details on using hashes here.

File details

Details for the file switch-1.0.1.tar.gz.

File metadata

  • Download URL: switch-1.0.1.tar.gz
  • Upload date:
  • Size: 2.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for switch-1.0.1.tar.gz
Algorithm Hash digest
SHA256 2634a590c1d4db31c39cd8d1c1d0958996a28e14121ef4a8095f390fe6987411
MD5 0f86572fd13fefdfb76248669fba1752
BLAKE2b-256 88b4aca7b513917080df39d4548aab6d621dcf6218ad191c1067e68bc96bd3ad

See more details on using hashes here.

File details

Details for the file switch-1.0.1-py27-none-any.whl.

File metadata

File hashes

Hashes for switch-1.0.1-py27-none-any.whl
Algorithm Hash digest
SHA256 4b78d898c807422c0d5003354e4a443b081884361e49d2eca69decc36a7beb57
MD5 d527608f41ed90430ce068ce3c40b417
BLAKE2b-256 7c616ec4a18b6ecfc13ceffe462190b9bd2b80e08ef6aacc78da4e35a064308a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page