Skip to main content

A simple and flexible switch statement implementation for Python.

Project description

Switch

Introduction

Switch is a Python library that provides a simple and flexible switch statement implementation. It allows you to write cleaner and more readable code by eliminating the need for long chains of if-else statements.

Usage

The switch library provides a Switch class that can be used to create switch statements. The Switch class takes a value as an argument, which is the value to be compared against the cases.

The case method takes a condition as an argument. If the condition evaluates to True, the corresponding function is executed. The default method is executed if no match is found.

Here is an example of how to use the Switch class:

from switch import Switch

with Switch(1) as s:
    if s.case(s.value == 1):
        pass
    if s.case(s.value == 2):
        pass
    if s.case(s.value == 3):
        print("1, 2, 3!!")
        raise Break()  # Exit the Switch context
    if s.case(s.value == 4):
        print("Won't see this... :(")
    if s.default:
        print('default')

Features:

  • Simple and intuitive syntax
  • Flexible conditions
  • Support for multiple cases
  • Default case handling
  • Exception handling for breaking out of the switch statement

Benefits:

  • Improved code readability
  • Reduced code complexity
  • Easier to maintain and debug

Conclusion:

The switch library is a valuable tool for any Python developer who wants to write cleaner and more readable code. It is a simple and flexible way to implement switch statements in Python.

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

simple-switch-0.2.0.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

simple_switch-0.2.0-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file simple-switch-0.2.0.tar.gz.

File metadata

  • Download URL: simple-switch-0.2.0.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for simple-switch-0.2.0.tar.gz
Algorithm Hash digest
SHA256 6c8d05c570107b18da280dac566c9f391ed5531b4cd9ecf85254614e9bade198
MD5 df17f596dd47d4e24717d31a599829a7
BLAKE2b-256 21f0413939142b1a19a7e84524ec405694e030a124c9375260479ddf55fe8457

See more details on using hashes here.

File details

Details for the file simple_switch-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for simple_switch-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ff32391151d2ad385f7a0b4929b8a60e4210722421ccfc624318dcce79ef99bc
MD5 bd52dc61a08efe82096902a325c59b37
BLAKE2b-256 c026f80b9a6a64933aeb2d494d715624902edbf2c67448e5e099b67e4644dbc2

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