sweetcase

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


License
MIT
Install
pip install sweetcase==0.0.5

Documentation

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.