Skip to main content

Simple and light-weight module allowing the use of a switch-case alike syntax in python

Project description

sweetcase

Simple and light-weight module allowing the use of a switch-case alike syntax in python.

switch-case is a very common and useful syntax in many programming languages such as JavaScript, C#, Java, C++, Go, php and C, however, it's missing in Python. sweetcase allows Python programmers using a very similar syntax and get the same result.


Install and Import

pip install sweetcase
from sweetcase import switch, case, default

prerequisites

Just any version of python 3 - No need of any additional modules.


Usage Examples

Basic Use:

from sweetcase import switch, case, default

operator = "*"
n1 = 8
n2 = 2

res = switch(operator, [
    case("+",
         lambda: n1 + n2),
    case("-",
         lambda: n1 - n2),
    case("*",
         lambda: n1 * n2),
    case("/",
         lambda: n1 / n2),
    case(default,
         lambda: "unsupported operator"),
])

equivalent JavaScript code:

const operator = "*"
const n1 = 8
const n2 = 2

const myFunc = () => {
    switch (operator) {
      case '+':
        return n1 + n2;
      case '-':
        return n1 - n2;
      case '*':
        return n1 * n2;
      case '/':
        return n1 / n2;
      default:
        return 'unsupported operator';   
    }
}
const res = myFunc()

sweetcase supports many more common uses of switch-case like break, multi-case and regex. It's highly recommended to check out our USAGE_EXAMPLES.md and explore the different usages.


Documentation

Full documentation of the functions can be found in DOCS.md.

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

sweetcase-0.0.5.tar.gz (6.5 kB view details)

Uploaded Source

File details

Details for the file sweetcase-0.0.5.tar.gz.

File metadata

  • Download URL: sweetcase-0.0.5.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.4

File hashes

Hashes for sweetcase-0.0.5.tar.gz
Algorithm Hash digest
SHA256 72345bd292c9398240a7573c78b32dcefb0725dd33be0a65077f90af63fa59c9
MD5 ffc7f3b8282c584177b06d8b3f503226
BLAKE2b-256 05df40ba17df0e5d913b789703e2b407497890158b5eafefb811183c36af78c3

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