In the simplest case, I have some code where the user may want to do one thing (a), another thing, (b), or both (a+b).
The options are reasonably complex and have their own functions, but I would like an elegant way of executing one, the other, or multiple.
I want to consider the types of the choices supplied, as I think this could add unnecessary complexity when expanding to more options.
Dictionaries:
def execute(choices: List[str]):
options = {'a', a, 'b': b}
for choice in choices:
options[choice]()
# could also be empty, or contain only 'a' or 'b':
choices = ['a', 'b']
execute(choices)
Enums:
class Choice(Enum):
A = auto()
B = auto()
BOTH = auto()
def execute(choice: Choice):
if choice.name == 'A':
a()
elif choice.name == 'B':
b()
elif choice.name == 'BOTH':
a()
b()
Keyword argument options:
def execute(*, a=False, b=False):
if a:
a()
if b:
b()
Combining dicts and enums:
class Choice(Enum):
A = auto()
B = auto()
def execute(choices: List[Choice]):
options = {
'A': a,
'B': b,
}
for choice in choices:
options[choice.name]()
I think the last option will scale the best. Short of supplying a config file, am I missing something? I can use Python 3.10 so perhaps match case could be of use.
Are there considerations to be made for extended cases e.g. "cannot perform option C when performing option A", perhaps an option validation function as well?
Please consider typing and scalability.