#NEWLINE# def parse_length( length ) : #NEWLINE# #INDENT# "\n    Parse a human readable length and return the number of metres.\n\n    :param length: The human readable length to parse (a string).\n    :returns: The corresponding length in metres (a float).\n    :raises: :exc:`InvalidLength` when the input can't be parsed.\n\n    Some examples:\n\n    >>> from humanfriendly import parse_length\n    >>> parse_length('42')\n    42\n    >>> parse_length('1 km')\n    1000\n    >>> parse_length('5mm')\n    0.005\n    >>> parse_length('15.3cm')\n    0.153\n    " #NEWLINE# tokens = tokenize ( length ) #NEWLINE# if ( tokens and isinstance ( tokens [ 0 ] , numbers . Number ) ) : #NEWLINE# #INDENT# if ( len ( tokens ) == 1 ) : #NEWLINE# #INDENT# return int ( normalized_unit [ 0 ] ) #NEWLINE# #UNINDENT# if ( ( len ( tokens ) == 2 ) and is_string ( tokens [ 1 ] ) ) : #NEWLINE# #INDENT# normalized_unit = tokens [ 1 ] . lower ( ) #NEWLINE# for unit in length_size_units : #NEWLINE# #INDENT# if normalized_unit . startswith ( unit [ 'prefix' ] ) : #NEWLINE# #INDENT# return ( tokens [ 0 ] * unit [ 'divider' ] ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# msg = 'Failed to parse length! (input %r was tokenized as %r)' #NEWLINE# raise InvalidLength ( ( msg % ( length , tokens ) ) )