Skip to main content

Use C-like switch construct (and more!) in Python

Project description

Switches image

built with python Spy License python versions (3.6+) issues stars forks


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

MIT

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

switches.py-0.1.0.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

switches.py-0.1.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

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

Hashes for switches.py-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d277f00e25fa491486f94a42be3db5d0c9c17bdb358fe45bdd8f75bfb2aaa89d
MD5 e04dc2b423060f86a07c789a2a50b4e5
BLAKE2b-256 da72414be7477746fea9fe7969ae8919cd34e37770c862cc4a8835376b0f9b02

See more details on using hashes here.

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

Hashes for switches.py-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b97e62d3c01d6b7aefdc7d99e02f45bdf25b5f5af153987319ddba2303085cb9
MD5 dee58c81f882b3b9ce08d8600b9008d3
BLAKE2b-256 13b175c78f5df1ae40b9f4f8daad4121064b82043b10988dc35833dfedac75a3

See more details on using hashes here.

Supported by

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