I have roughly 40 or 50 functions that are similar to the one provided by the code below.
I kept all the common calculations between multiple functions separate such as service_coverage_area
. I then kept all the specific calculations that are not repeated or needed by any other functions in the function that needs those calculations such as calculating f_sc.
My question is: Am I doing this right? as in is this the best practice to code utility functions? should I be separating all the mini calculations into separate functions but then have to deal with 200 functions instead of the current 50?
Note that the self is there cause this code is part of a class. I just copy and pasted 2 functions into this paste
import bisect
def service_coverage_area(self, mode):
#simplified for codereview
return 1
def some_calc(self, mode, street_type, grade, elderly_proportion, f_px):
""" where:
r = r_0 * f_sc * f_s * f_pop * f_px
r_0 = ideal transit stop service radius
400m for bus and 800m for rail / express bus and commuter
needed variable: mode
f_sc = street connectivity factor
{'grid':1, 'hybrid':0.85, 'cul_de_sac':0.45}
needed variable: street connectivity tye
f_s = grade fator
0.00 to 0.05 -> 1.00
0.06 to 0.08 -> 0.95
0.09 to 0.11 -> 0.80
0.12 to 0.15 -> 0.65
needed variable: grade
f_pop population factor
1 for normal population
0.85 of 20% is elderly population
needed variable:elderly_proportion
f_px pedestrian crossing factor
provided by another calculation
"""
#calculate r_0
r_0 = self.service_coverage_area(mode)
#calculate f_sc
if street_type == 'grid':
f_sc = 1
elif street_type == 'hybrid':
f_sc = 0.85
elif street_type == 'cul_de_sac':
f_sc = 0.45
#calculate f_s
grades = [0.05, 0.08, 0.11]
f_s_values = [1.00, 0.85, 0.80, 0.65]
f_s_score = bisect.bisect(grades, grade)
f_s = f_s_values[f_s_score]
#calculate f_pop
if elderly_proportion <= 0.2:
f_pop = 1
else:
f_pop = 0.85
return r_0 * f_sc * f_s * f_pop * f_px