def apply_units(string, units, inter=None, final=float, blank_reg=_BLANK_RE, value_reg=_VALUE_RE):
 if (inter is None):
  inter = final
 fstring = _BLANK_RE.sub('', string)
 if (not (fstring and _VALIDATION_RE.match(fstring))):
  raise ValueError(('Invalid unit string: %r.' % string))
 values = []
 for match in value_reg.finditer(fstring):
  dic = match.groupdict()
  (lit, unit) = (dic['value'], dic.get('unit'))
  value = inter(lit)
  if (unit is not None):
   try:
    value *= units[unit.lower()]
   except KeyError:
    raise KeyError(('invalid unit %s. valid units are %s' % (unit, units.keys())))
  values.append(value)
 return final(sum(values))

def apply_units ( string , units , inter = None , final = float , blank_reg = _BLANK_RE , value_reg = _VALUE_RE ) : if ( inter is None ) : inter = final fstring = _BLANK_RE . sub ( '' , string ) if ( not ( fstring and _VALIDATION_RE . match ( fstring ) ) ) : raise ValueError ( ( 'Invalid unit string: %r.' % string ) ) values = [ ] for match in value_reg . finditer ( fstring ) : dic = match . groupdict ( ) ( lit , unit ) = ( dic [ 'value' ] , dic . get ( 'unit' ) ) value = inter ( lit ) if ( unit is not None ) : try : value *= units [ unit . lower ( ) ] except KeyError : raise KeyError ( ( 'invalid unit %s. valid units are %s' % ( unit , units . keys ( ) ) ) ) values . append ( value ) return final ( sum ( values ) )
parse the string applying the units defined in units .