Skip to main content

A package that implements a switch statment

Project description

Switch Class

The switch class in Python mimics a switch-case statement found in other programming languages. This class allows you to define a set of case-output pairs and an optional end output for unmatched cases. Each case can map to either a static value or a callable function.

Table of Contents

Installation

No additional packages are required. Simply copy the Switch class definition into your Python project.

Usage

The Switch class is initialized with multiple case-output pairs, followed by an optional end output that acts as the default response when no cases match.

Cases can be dynaimically changed by setting the values with __setitem__, and also accessed with __getitem__

Initialization

To create a Switch instance, pass cases and outputs in pairs, optionally followed by an end output:

from switchObject import switch
switch_instance = switch(case1, output1, case2, output2, ..., end=default_output)

Example

Below is a example of a useage of the switch object

# Define functions to use as case outputs
def case_one_action():
    return "Action for Case 1"

def case_two_action():
    return "Action for Case 2"

# Initialize a Switch instance with cases and an end default
switch_example = switch(
    "case1", case_one_action,    # Function to call for "case1"
    "case2", case_two_action,    # Function to call for "case2"
    end="Default action if no case matched"
)

# Call a case
print(switch_example("case1"))  # Output: "Action for Case 1"
print(switch_example("unknown_case"))  # Output: "Default action if no case matched"

# Access via getitem
print(switch_example["case1"])  # Output: <function case_one_action>
print(switch_example["unknown_case"]())  # Output: "Default action if no case matched"

# Modify a case
switch_example["case1"] = "New Output for Case 1"
print(switch_example("case1"))  # Output: "New Output for Case 1"

# Display all cases and outputs
print(switch_example)

License

Veiw License Agreement Here: License.

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

switchobject-1.2.0.1.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

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

switchObject-1.2.0.1-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file switchobject-1.2.0.1.tar.gz.

File metadata

  • Download URL: switchobject-1.2.0.1.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.5

File hashes

Hashes for switchobject-1.2.0.1.tar.gz
Algorithm Hash digest
SHA256 4e5e7a2210289b3c2c2afb3d191397efcdc021ed22afa009f4d6a0bee4cdaf03
MD5 b429f03c1d64997c6767996b97835d63
BLAKE2b-256 2781b763ace94a1cf3c7896c82f089feb377f32ae1c0ae3a6c75afc69e6b3c15

See more details on using hashes here.

File details

Details for the file switchObject-1.2.0.1-py3-none-any.whl.

File metadata

  • Download URL: switchObject-1.2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.5

File hashes

Hashes for switchObject-1.2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c8b5c21b3b46791984f9ce1a7b61beb64c2c380998f827a5b7d40017f9f134b9
MD5 7396d7c6eb2d559599ca7f3ed037242b
BLAKE2b-256 993a6855c0b8c3bf4017d9f941aa303006037d7df7128ae2139249988ebdcb08

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