Next / Previous / Contents / Shipman's homepage

17.10. Txny.__checkSubgenus(): Add a new subgenus?

# - - -   T x n y . _ _ c h e c k S u b g e n u s

    def __checkSubgenus ( self, scan, spTail ):
        '''Possibly add a new subgenus.

          [ (scan is a Scan instance) and
            (spTail is an SpTail instance) ->
              if spTail names no subgenus) or
              (that subgenus exists in self.taxaTree) or
              (self.hier has no subgenus rank) ->
              else if that subgenus can be added to self.taxaTree ->
                  self.taxaTree  +:=  that subgenus
              else ->
                Log()  +:=  error message(s)
                raise SyntaxError ]

If spTail has no .subgenus value, or if the hierarchy does not include a subgenus rank, we're done. The “not in” operator for Hier instances tests to see whether a given rank code is included; see Section 15.4, “Hier.__contains__(): Is a given code in this hierarchy?”.

        #-- 1 --
        # [ if (spTail.subgenus is None) or
        #   (self.hier includes no subgenus rank) ->
        #     return
        #   else -> I ]
        if ( ( spTail.subgenus is None ) or
             ( SUBGENUS_CODE not in self.hier ) ):

Assemble the scientific name. For example, the scientific name of subgenus Olor of genus Cygnus is Cygnus (Olor). If this scientific name is already in the tree (see Section 18.8, “TaxaTree.__contains__(): Membership test for a scientific name”), we're done.

        #-- 2 --
        # [ sci  :=  full subgenus name from spTail
        #   eng  :=  same with underbars around genus and subgenus
        #            name ]
        sci = "%s (%s)" % (spTail.genus, spTail.subgenus)
        eng = "_%s_ (_%s_)" % (spTail.genus, spTail.subgenus)

There is a subtle error condition for which we must now check. Consider this imaginary (bad) input:

   Carduelis (Spinus) psaltria/Goldfinch, Lesser
   Loxia curvirostra/Crossbill, Red
   Carduelis (Spinus) spinus/Siskin, Eurasian

During processing of the third line above, we might notice that subgenus “Carduelis (Spinus)” has already been defined, and exit this method. However, according to our method for finding the parent taxon, the new species will be placed under “Loxia”, and that is not where it goes!

Hence, we must test that the parent's scientific name is the same one from the spTail.

        #-- 3 --
        # [ if taxon.parent.sci == spTail.genus ->
        #     I
        #   else ->
        #     Log()  +:=  error message(s)
        #     raise SyntaxError ]
        if taxon.parent.sci != spTail.genus:
            scan.syntax ( "This subgenus occurs in more than one "
                "location in the file." )

If the subgenus has already been defined, we are done.

        #-- 4 --
        if sci in self.taxaTree:

Assemble the attributes of the new taxon in a RawTaxon instance; see Section 23, “class RawTaxon: Temporary container for taxon attributes”. Then, assuming that this subgenus can be appended to the tree, do so.

        #-- 5 --
        # [ rawSubgenus  :=  a new RawTaxon instance with rank code
        #       SUBGENUS_CODE; scientific name (sci); English name
        #       (eng); normal status; and no codes. ]
        rawSubgenus = RawTaxon ( SUBGENUS_CODE, sci, eng,
                                 STATUS_NORMAL, None, None )

        #-- 6 --
        # [ if the rawSubgenus can be appended to self.taxaTree ->
        #     self.taxaTree  +:=  a new Taxon instance made from rawTaxon
        #   else ->
        #     Log()  +:=  error message(s)
        #     raise SyntaxError ]
        self.__appendTaxon ( scan, rawGenus )