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
(currently unavailable)
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 goto. Please check out our USAGE_EXAMPLES.md to 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.