Next / Previous / Contents / Shipman's homepage

11.15. BirdId.scanAbbr()

This is a helper method used by .scan() and .scanFlat(). It is a static method.

abbr.py
# - - -   B i r d I d . s c a n A b b r   - - -

    def scanAbbr ( txny, scan ):
        """Scan one bird code.
        """

We use the regular expression RE_ABBR to match any leading characters in the scan instance. If it matches, it returns a Match instance whose .group() method returns the matching text. If it returns None, there is no match.

abbr.py
        #-- 1 --
        # [ if scan starts with characters matching RE_ABBR ->
        #     scan  :=  scan advanced past matching characters
        #     m     :=  a Match instance representing the matched text
        #   else ->
        #     m     :=  None ]
        m = scan.tabReMatch ( RE_ABBR )

        #-- 2 --
        # [ if m is None ->
        #     scan  +:=  error message
        #     raise ValueError
        #   else ->
        #     abbr  :=  m.group()
        if  m is None:
            scan.error ( "Expecting a bird code." )
            raise ValueError, "Expecting a bird code."
        else:
            abbr = m.group()

        #-- 3 --
        # [ if abbr is defined in txny ->
        #     return abbr
        #   else -> raise ValueError ]
        try:
            taxon = txny.lookupAbbr ( abbr )
            return abbr
        except KeyError, detail:
            message = "Unknown bird code '%s'" % abbr
            scan.error ( message )
            raise ValueError, message

Finally, make this a static method.

abbr.py
    scanAbbr = staticmethod ( scanAbbr )