Git Product home page Git Product logo

python-switch-case's Introduction

Python - Switch Case Simulation (C Like)

Python implementation of C Switch Case clause

How does it work?

Using python context manager and if blocks, we can simulate switch case by instantiating Switch class and calling its method __call__()

Using

Import Switch class from switch.py module

from switch import Switch

Create the cases block inside Switch instance context block

variable = 10

with Switch(variable) as case:
    if case(0):
        print('case 0')
    elif case(*range(1, 6)):
        print('case 1, 2, 3, 4 and 5')
    elif case(6, 7, 8):
        print('case 6, 7, and 8')
    elif case(9, 10):
        print('case 9 and 10')
    else:
        print('default')

# >> case 9 and 10

Suppressing exceptions

You can also handle exceptions that can occurs inside the switch block, without using try/except

variable = 11
mylist = list(range(10))

with Switch(variable, suppress=IndexError) as case:
    if case(*range(10)):
        print(mylist[variable])  # the same as print(mylist[case.value])
    else:
        print(mylist[variable])
        print('Index out of the range')

Without the suppress argument, it will raise the exception

variable = 11
mylist = list(range(10))

with Switch(variable) as case:
    if case(*range(10)):
        print(mylist[variable])  # the same as print(mylist[case.value])
    else:
        print(mylist[variable])
        print('Index out of the range')

# Traceback (most recent call last):
#   File "~/tests.py", line 10, in <module>
#     print(mylist[variable])
# IndexError: list index out of range

python-switch-case's People

Contributors

senavs avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.