Next / Previous / Contents / TCC Help System / NM Tech homepage

9.15. parseFloatSuffix: Parse a number followed by a code

This utility routine is used to handle the common case where a floating-point number must be followed by a specific letter, such as in the angle expression “107m”.

sidereal.py
# - - -   p a r s e F l o a t S u f f i x

def parseFloatSuffix ( s, codeRe, message ):
    """Parse a float followed by a letter code.

      [ (s is a string) and
        (codeRe is a compiled regular expression) and
        (message is a string describing what is expected) ->
          if  s starts with a float, followed by code (using 
          case-insensitive comparison) ->
            return (x, tail) where x is that float as type float
            and tail is the part of s after the float and code
          else -> raise SyntaxError, "Expecting (message)" ]
    """

We use Section 9.16, “parseFloat(): Parse a floating-point number” to remove the required floating number from the front of s. We could just pass through the SyntaxError exception that that routine will raise, but we catch and re-raise it here so as to provide more information about what is expected.

sidereal.py
    #-- 1 --
    # [ if  s starts with a float ->
    #     x  :=  that float as type float
    #     codeTail  :=  the part of s after that float
    #   else -> raise SyntaxError, "Expecting (message)" ]
    x, codeTail  =  parseFloat ( s, message )

    #-- 2 --
    # [ if codeTail starts with code (case-insensitive) ->
    #     return (x, the part of codeTail after the match)
    #   else -> raise SyntaxError ]
    discard, tail  =  parseRe ( codeTail, codeRe, message )

    #-- 3 --
    return (x, tail)