Use C-like switch construct (and more!) in Python
Project description
Table of Contents
Switches
if statements in Python can become cluttered when there are too many conditions to check or match, and more often, there is need to make these multiple if constructs more readable, syntactically clearer or at the very least aesthetically pleasing :), thus arising the inspiration for Switches.
Switches is a package that helps support C-like switch statements (and more!) in Python.
Usage
Consider the following (overly simplified) code snippet:
val = get_some_value()
if val == 10:
do_stuff1()
elif val == 15:
do_stuff2()
elif val == 20:
do_stuff3()
elif val == 30:
do_stuff4()
else:
do_other_stuff()
Using Switches, we could rewrite this as:
from switches.switch import switch
val = get_some_value()
with switch(val) as s:
s.case(10, do_stuff1)
s.case(15, do_stuff2)
s.case(20, do_stuff3)
s.case(30, do_stuff4)
s.default(do_other_stuff)
When writing if conditional tests of equality, Switches can make things pretty easier.
In the snippet above, breaks are automatically (implicitly) added to each case statement when fallthrough is set to False(False by default), unlike C-style switches.
For more information about fallthrough attribute and break statements see the doc.
Documentation
More information is contained in the documentation. Read the docs.
Tests
See the tests folder for tests and other examples.
Installation
Via Pip:
pip install switches.py
Alternatively:
Clone this repo, and do:
cd Switches
python setup.py install
Dependencies
Python 3.6+
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file switches.py-0.1.0.tar.gz.
File metadata
- Download URL: switches.py-0.1.0.tar.gz
- Upload date:
- Size: 8.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d277f00e25fa491486f94a42be3db5d0c9c17bdb358fe45bdd8f75bfb2aaa89d
|
|
| MD5 |
e04dc2b423060f86a07c789a2a50b4e5
|
|
| BLAKE2b-256 |
da72414be7477746fea9fe7969ae8919cd34e37770c862cc4a8835376b0f9b02
|
File details
Details for the file switches.py-0.1.0-py3-none-any.whl.
File metadata
- Download URL: switches.py-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b97e62d3c01d6b7aefdc7d99e02f45bdf25b5f5af153987319ddba2303085cb9
|
|
| MD5 |
dee58c81f882b3b9ce08d8600b9008d3
|
|
| BLAKE2b-256 |
13b175c78f5df1ae40b9f4f8daad4121064b82043b10988dc35833dfedac75a3
|