def parse_timedelta(text):
 td_kwargs = {}
 for match in _PARSE_TD_RE.finditer(text):
  (value, unit) = (match.group('value'), match.group('unit'))
  try:
   unit_key = _PARSE_TD_KW_MAP[unit]
  except KeyError:
   raise ValueError(('invalid time unit %r, expected one of %r' % (unit, _PARSE_TD_KW_MAP.keys())))
  try:
   value = float(value)
  except ValueError:
   raise ValueError(('invalid time value for unit %r: %r' % (unit, value)))
  td_kwargs[unit_key] = value
 return timedelta(**td_kwargs)

def parse_timedelta ( text ) : td_kwargs = { } for match in _PARSE_TD_RE . finditer ( text ) : ( value , unit ) = ( match . group ( 'value' ) , match . group ( 'unit' ) ) try : unit_key = _PARSE_TD_KW_MAP [ unit ] except KeyError : raise ValueError ( ( 'invalid time unit %r, expected one of %r' % ( unit , _PARSE_TD_KW_MAP . keys ( ) ) ) ) try : value = float ( value ) except ValueError : raise ValueError ( ( 'invalid time value for unit %r: %r' % ( unit , value ) ) ) td_kwargs [ unit_key ] = value return timedelta ( ** td_kwargs )
parse a string like 5 days into a timedelta object .