Next / Previous / Contents / Shipman's homepage

17.19. Txny.__bindHigherAlt(): Bind a higher-taxon code

nomcompile3
# - - -   T x n y . _ _ b i n d H i g h e r A l t

    def  __bindHigherAlt ( self, scan, code, sci, eng ):
        '''Create a higher-taxon code binding.

          [ (scan is a Scan instance) and
            (code is an uppercased, stripped bird code) and
            (sci is a scientific name) and
            (eng is an English name) ->
              if sci is defined in self.__sciMap ->
                self.abTab  :=  self.abTab with a standard binding
                    for code with eng=(eng), and sci=(sci)
              else ->
                Log()  +:=  error message(s)
                raise SyntaxError ]
        '''

To define this new code as the taxon with scientific name sci, we will need to create a standard binding for the symbol table; see Section 27, “class StdBind: Code bound to a taxon”. Such a binding requires the Taxon instance for this scientific name.

nomcompile3
        #-- 1 --
        # [ if sci is a scientific name defined in self.taxaTree ->
        #     taxon  :=  the Taxon instance for that name
        #   else ->
        #     Log()  +:=  error message(s)
        #     raise SyntaxError ]
        try:
            taxon = self.taxaTree[sci]
        except KeyError:
            scan.syntax ( "Scientific name '%s' is undefined." % sci )

First we create the StdBind instance, then we find the existing symbol table entry or create a new one. Then we try to add the binding to the symbol table entry, which will fail if there is any existing binding.

nomcompile3
        #-- 2 --
        # [ stdBind  :=  a new StdBind with abbr=(code),
        #       taxon=(taxon), and eng=(eng) ]
        stdBind = StdBind ( code, taxon, eng )

        #-- 3 --
        # [ if code.strip().upper() is in self.abTab ->
        #     sym  :=  the corresponding symbol table entry
        #   else ->
        #     self.abTab  +:=  a new, empty symbol table entry
        #                      for (code)
        #     sym  :=  that symbol table entry ]
        sym = self.abTab.addAbbr ( code )

        #-- 4 --
        # [ if stdBind can be combined with sym.binding ->
        #     sym  :=  sym with stdBind bound to it
        #   else ->
        #     Log()  +:=  error message(s)
        #     raise SyntaxError ]
        try:
            sym.bind ( stdBind )
        except ValueError:
            scan.syntax ( "Code '%s'  was previously used as "
                "%s." % (code, sym.binding) )