I'm trying to make some code that I wrote hastily with a lot of copy/pastes a little cleaner. I noticed that I have two functions that are irritatingly close to doing the same thing. Here's the gists of them:
def determine_risk(difference):
if difference > ON_TRACK:
return 'On Track'
elif difference < HIGH_RISK:
return 'High Risk'
else:
return 'Low Risk'
def determine_completeness(pct_complete):
if pct_complete == UNSTARTED:
return 'Unstarted'
elif pct_complete > READY:
return 'Ready'
else:
return 'In Process'
I'd love to turn this into one function. Something like
def determine_condition(metric, [list_of_conditions], [list_of_outcomes], fallback)
for condition, outcome in zip(list_of_conditions, list_of_outomes):
if metric satisfies condition:
return outcome
return fallback
The problem is I don't think it's possible to store conditional checks in a list like that! Posting here in case someone can show me a way, or sees an alternative approach to combining these similar types of function into one