def get_LOS(headway = None, frequency = None, veh_per_hr = None):
You have no docstring. That would helpful in explaining what the parameters are doing.
if frequency != None and headway = veh_per_hr == None:
When checking wheter something is none it is best to use is None
or is not None
headway = 1 / frequency
The problem is that if someone passes frequency along with one of the other parameters, this function will go merrily onwards and probably not even produce an error. I recommend having a get_LOS_from_frequency function which takes the frequency and then calls this function.
if headway!= None or veh_per_hr != None:
if headway 10 < or veh_per_hr > 6:
I'm pretty sure that won't compile
return 'A'
elif 10 <= headway < 15 or 5 < veh_per_hr <= 6:
return 'B'
elif 15 <= headway < 20 or 3 < veh_per_hr <= 5:
return 'C'
elif 20 <= headway < 31 or 2 < veh_per_hr <= 3:
return 'D'
elif 31 <= headway < 60 or 1 <= veh_per_hr < 2:
return 'E'
elif headway >= 60 or veh_per_hr < 1:
return 'F'
I'd store these values in a list which makes it easy to pull them from configuration at a later date if neccesary.
else:
return 'Error'
Don't report error by returning strings. Throw an exception, or at least assert False.
How I'd do this:
import bisect
def get_los_from_frequency(frequency):
return get_los(1 / frequency)
HEADWAY_LIMITS = [10, 15, 20, 31, 60]
VEH_PER_HR_LIMITS = [1,2,3,5,6]
GRADES = "ABCDEF"
def get_los(headway = None, veh_per_hr = None):
if headway is None:
headway_score = len(GRADES)
else:
headway_score = bisect.bisect_left(HEADWAY_LIMITS, headway)
if veh_pr_hr is None:
veh_pr_hr_score = len(GRADES)
else:
veh_pr_hr_score = len(GRADES) - bisect.bisect_left(VEH_PR_HR_LIMITS, veh_pr_hr)
return GRADES[min(headway_score, veh_pr_hr_score)]
headway = veh_per_hr == None
is illegal as isheadway 10 <
. Please be sure that code you want reviewed has a small chance of actually running. And if it doesn't run, please use StackOverflow to find and fix the problems. \$\endgroup\$