Reputation: 2292
I am using nested if in my python code, my example code is :
message = event_data["event"]
if message.get("subtype") is None :
if "friday" in message.get('text'):
callfridayfunction()
if "saturday" in message.get('text'):
callsaturdayfunction()
if "monday" in message.get('text'):
callmondayfunction()
How can I write this as a switch case or using dictionary ? Please help
Upvotes: 0
Views: 99
Reputation: 27485
This may be the closest to what you want. This will work if multiple days are in the text also.
days_switch = {
'monday': callmondayfunction,
'saturday': callsaturdayfunction
'friday': callfridayfunction
}
message = event_data["event"]
if message.get("subtype") is None :
text = message.get('text', '')
days = set(text.split()) & set(days_switch)
for day in days:
days_switch[day]()
if you know there aren't multiple days in the text, then the loop isn't needed.
days_switch[set(text.split()) & set(days_switch)]()
Upvotes: 2