Skip to main content

switch_case

License Codecov PyPI Python Version

In Python 3.10 you can use Structural Pattern Matching

Installation

pip3 install switch_case

Usage

from switch_case import *
reason = (
    switch
        | case(_ == 200) >> 'OK'
        | case(_ == 500) >> 'ERROR'
        | default        >> 'UNKNOWN')
assert reason(200) == 'OK'
assert reason(500) == 'ERROR'
assert reason(400) == 'UNKNOWN'

Which is syntax sugar for:

from switch_case import *
from operator import eq
reason = (
    switch
        | case(_ /eq/ 200) >> 'OK'
        | case(_ /eq/ 500) >> 'ERROR'
        | default        >> 'UNKNOWN')

So you can use it like this:

from switch_case import *
get_type = (
    switch
        | case(_ /isinstance/ str)   >> "string"
        | case(_ /isinstance/ int)   >> "integer"
        | case(_ /isinstance/ float) >> "float"
        | case(_ /isinstance/ bool)  >> "bool"
        | default                    >> "other")

Or as a function:

from switch_case import *
def get_type(smth):
    return ~(
        switch(smth)
            | case(_ /isinstance/ str)   >> "string"
            | case(_ /isinstance/ int)   >> "integer"
            | case(_ /isinstance/ float) >> "float"
            | case(_ /isinstance/ bool)  >> "bool"
            | default                    >> "other")
assert get_type(42) == "integer"
assert get_type("42") == "string"
assert get_type(3.14) == "float"

Release files for switch-case 1.5

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

Source distribution (sdist)

Source distribution for switch-case 1.5
File Size Uploaded
switch_case-1.5.tar.gz 5.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for switch-case 1.5
File Interpreter ABI Platform
switch_case-1.5-py3-none-any.whl Python 3 none any Details

Total release size: 13.3 kB

Release files / switch_case-1.5.tar.gz

Download URL switch_case-1.5.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
ab5a6e75ab8c704b6870bf99d7c47b355466ab4405c9483928e2e42dce3bdcfa
BLAKE2b-256 checksum
How to use checksums
b086d14bca6c35c4e420d77e533c3adfc113c0662a530abb2c263974f919d06c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

Release files / switch_case-1.5-py3-none-any.whl

Download URL switch_case-1.5-py3-none-any.whl
Size 8.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a21fba060fbc1060ba61b8a707fcac1173ec4b85b746196fd01655cf77ba4cc5
BLAKE2b-256 checksum
How to use checksums
bd50c83862f0c838a1eff36c96f10ea7f474332aba9c986f112b4b0d8c83c557
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

Release history Release notifications | RSS feed

This release

1.5 This release

2 release files

1.4

2 release files

1.3

2 release files

1.2

2 release files

1.1

2 release files

1.0

2 release files

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