Skip to main content

An ugly pseudo solution for using switches in python.

Project description

ugly_switch

Build Status codecov

An ugly pseudo solution for using switches in python.

Installation

pip install ugly_switch

Usage

from ugly_switch import switch

A Switch can be used once:

switch[
    True, door_lock,
    False, door_unlock
](not door_state)

# looks the door when it's state is False and vice versa

or can be stored to be used multiple times in the future:

s = switch[
    'a', lambda: print('was a'),
    'b', lambda: print('was b')
]

# some stuff

value = 'a'
s(value)

# OUT: "was a"

When the number of args in the switch is even, the pairs make up the switch as sets of cases and actions. actions must be callable methods or lambdas. If the number of args may be odd, the last item will be used as default, expecting it to be a callable.

With the example directly above, nothing will happen, if value happens to be neither 'a' nor 'b'.

Below it has a default action.

value = 'c'
s = switch[
    'a', lambda: print('was a'),
    'b', lambda: print('was b'),
    lambda: print('something else')
]

# some stuff

s(value)

# OUT: "something else"

Project details


Release history Release notifications

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for ugly-switch, version 0.1.0
Filename, size File type Python version Upload date Hashes
Filename, size ugly_switch-0.1.0-py3-none-any.whl (3.4 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size ugly_switch-0.1.0.tar.gz (2.7 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page