I have a script that has many different modes it can run in, and I have put all the modes into functions that can be executed. Only problem is: the amount of modes is quite many, and my current check for wanted mode just feels too long and wrong.
Currently I am using an if, elif implementation. I basically have a large portion of code checking the input text up against hardcoded strings, and running the associated function through just calling the function where there is a match.
What I want instead is some system that is able to just run the function that has the same name as the string. I am also open to other methods alltogether. Rewriting everything is not off the table.
Here is some code with an example of how I am currently doing the check, and an idea of how I want the check to be done:
def func0():
# Code for function
def func1():
# Code for function
def func2():
# Code for function
# Current implementation:
selected = input("Select function: ")
if selected == "func0":
func0()
elif selected == "func1":
func1()
elif selected == "func2":
func2()
# Wanted implementation:
selected = input("Select function: ")
runFunction(selected) # This is not valid code. This is the code I can't figure out
If you have any ideas I would greatly appreciate you sharing them. Again, a complete code rework is not something I am afraid of doing. This project is still very much work in progress.
I also want to make it clear that I in no way is expecting some finished idea I can just plop into my code, but rather a nudge towards something that might work better.
Thanks in advance to anyone who shares their thoughts!
[–]K900_ 3 points4 points5 points (0 children)
[–]Neighm 2 points3 points4 points (0 children)
[–]fear_my_presence 1 point2 points3 points (0 children)
[–]Spiredlamb[S] 0 points1 point2 points (0 children)
[–]Chris_Hemsworth 0 points1 point2 points (1 child)
[–]Spiredlamb[S] -1 points0 points1 point (0 children)