switch-statement

Using lambda function to switch between different functions in python gives TypeError if invalid function name is given with argument

Using lambda function to switch between different functions in python gives TypeError if invalid function name is given with argument Question: I recently learned lambda functions and switch case and I’m playing with them to understand better. Here, I’m trying to use lambda function to switch between different functions in python like the following def …

Total answers: 2

Can I use list slicing on my match/case term in Python3

Can I use list slicing on my match/case term in Python3 Question: To elaborate I have a string x that will be in the structure of "Test_Case_Box 1" "Test_Case_Box 2" "Test_Case_Circle 1" I was using if and elif statements by checking for example if x[0:19] == "Test_Case_Box" With the match/case syntax if I set the …

Total answers: 1

How to check if list includes an element using match case?

How to check if list includes an element using match case? Question: I’m trying to check if a single element is in a list using match case. I’m not very familiar with these new keywords so 90% sure I’m using them wrong. Regardless, is there a way to do this? This is my code. I’m …

Total answers: 2

How to flag a given value based on a range rule

How to flag a given value based on a range rule Question: I’ve flags, each range of value has a flag. for ex : value = 0 is D value > 0 and < 0.2 is C value >=0.2 and <=0.8 is B value > 0.8 is A flag = ["A", "B", "C", "D"] def …

Total answers: 4

How to replace a lot of if/elif statements in python?

How to replace a lot of if/elif statements in python? Question: i am writing function in python for control outputs on QUIDO board throught post html requests. I have a lot of if and elif statements in my program. Is there some way how to make code more transparent? Quido board has 16 outputs so …

Total answers: 6

Python replacement of switch case into dictionary (for function calls)

Python replacement of switch case into dictionary (for function calls) Question: I’m trying to experiment with a replacement for switch/cases in Python that I had found online. It works quite well for most variables but I’m trying to make it such that I can call functions but I’m getting some strange behavior. This is my …

Total answers: 2

Python 3.10 match case with arbitrary conditions

Python 3.10 match case with arbitrary conditions Question: Is there any way to use arbitrary conditions on cases in a Python 3.10+ match statement or is it necessary to fall back on if-then control structures? Clarification: an arbitrary condition might be a function with myVariable as argument that evaluates to type bool. The constraint here …

Total answers: 1

How to use multiple cases in Match (switch in other languages) cases in Python 3.10

How to use multiple cases in Match (switch in other languages) cases in Python 3.10 Question: I am trying to use multiple cases in a function similar to the one shown below so that I can be able to execute multiple cases using match cases in python 3.10 def sayHi(name): match name: case [‘Egide’, ‘Eric’]: …

Total answers: 2

Python Match Case (Switch) Performance

Python Match Case (Switch) Performance Question: I was expecting the Python match/case to have equal time access to each case, but seems like I was wrong. Any good explanation why? Lets use the following example: def match_case(decimal): match decimal: case ‘0’: return "000" case ‘1’: return "001" case ‘2’: return "010" case ‘3’: return "011" …

Total answers: 3

Capture makes remaining patterns unreachable

Capture makes remaining patterns unreachable Question: Why does this code fail: OKAY = 200 NOT_FOUND = 404 INTERNAL_SERVER_ERROR = 500 match status: case OKAY: print(‘It worked’) case NOT_FOUND: print(‘Unknown’) case INTERNAL_SERVER_ERROR: print(‘Out of service’) case _: print(‘Unknown code’) It generates this error message: File "/Users/development/Documents/handler.py", line 66 case OKAY: ^^^^ SyntaxError: name capture ‘OKAY’ makes …

Total answers: 2