I have some computational functions for configurations for software that will drive a hardware setup. In some cases, the user may input invalid configuration values. I handle this by throwing an exception if invalid values are used, or simply returning my calculated value if the configuration works:
def calc_exhale_dur(breath_rate, inh_dur, breath_hold_dur):
"""Calculate exhalation duration.
Args:
breath_rate (float): animal breath rate (br/min)
inh_dur (float): animal inhalation duration (ms)
breath_hold_duration (float): animal breath hold duration (ms)
"""
single_breath_dur = calc_breath_dur(breath_rate)
exhale_dur = single_breath_dur - (inh_dur + breath_hold_dur)
if (inh_dur + breath_hold_dur + exhale_dur) > single_breath_dur:
raise error.ConfigurationError
elif exhale_dur <= 0:
raise error.ConfigurationError
else:
return exhale_dur
Is it considered bad practice to do it this way? Do I always need to have some valid return value if there is a return value to begin with? I'm trying to learn how best to write Pythonic code while still satisfying the needs of my computational methods.