def get_lookback(lookback_length):
"""Take a string from the user and return a datetime.timedelta object."""
time_units = {'weeks': 0, 'days': 0, 'hours': 0}
time_shortunits = [x[0] for x in time_units.keys()]
lookback_unit = lookback_length[-1]
lookback_value = lookback_length[:-1]
try:
lookback_unit = next(i for i in list(time_units) if
i.startswith(lookback_unit))
except StopIteration:
panic("hs_history_lookback time unit {lookback_unit} is not "
"suppported. Please choose one of: "
"{time_shortunits}.".format(**locals()))
try:
time_units[lookback_unit] = int(lookback_value)
except ValueError as exc:
panic("hs_history_lookback should be an integer value followed by a "
"single time unit element from {time_shortunits}.\n"
"ValueError: {exc}".format(**locals()))
lookback_timedelta = timedelta(weeks=time_units['weeks'],
days=time_units['days'],
hours=time_units['hours'])
return lookback_timedelta
评论列表
文章目录