Switch-case statement for Python
Project description
switch_case
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"
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_case-1.3.tar.gz
(4.5 kB
view hashes)
Built Distribution
Close
Hashes for switch_case-1.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b276fe2c915b3cef43c4fa2e21d53a0ef9c28b21e0a70475fe6209f8b16d101e |
|
MD5 | 5ac0d7e467270b944f9ce020d7ab30e0 |
|
BLAKE2b-256 | cdc64cca65dc67a66f2aeaada3574f412078e7e357aa3029ecd7967438f89b20 |