Next / Previous / Contents / Shipman's homepage

13.6. BaseSpeciesSet.addSpecies(): Add a new species line

This service method is intended to be called by concrete .readLine() methods once they have broken out the fields of the input line. It checks for duplicate 4-letter codes and English names; such duplications are considered invalid lines. The original version checked for duplicate 6-letter codes, but IBP's files are full of those, such as three different color morphs of Snow Goose encoded as CHECCA.

baseclasses.py
# - - -   B a s e S p e c i e s S e t . a d d S p e c i e s   - - -

    def addSpecies ( self, species ):
        """Add a new species to self.

          [ species is a Species object ->
              if (species.spec4 is a key in self.spec4Map,
              case-insensitive) or
              (species.eng is a key in self.engMap) ->
                Log()  +:=  error message(s)
                raise IOError
              else ->
                self.spec4Map[self.spec4]  :=  species
                self.spec6Map[self.spec6]  :=  species
                self.engMap[self.eng]      :=  species ]
        """

        #-- 1 --
        # [ if species.spec4 is a key in self.spec4Map,
        #   case-insensitive ->
        #     Log()  +:=  error message(s)
        #     raise IOError
        #   else -> I ]
        key4  =  species.spec4.upper()
        if  self.spec4Map.has_key ( key4 ):
            raise IOError, ( "Duplicate 4-letter code: %s; "
                "old entry: %s" % (species, self.spec4Map[key4]) )

        #-- 2 --
        # [ if species.eng is a key in self.engMap ->
        #     Log()  +:=  error message(s)
        #     raise IOError
        #   else -> I ]
        if  self.engMap.has_key ( species.eng ):
            raise IOError, ( "Duplicate English name: %s; "
                "old entry: %s" % (species, self.engMap[species.eng]) )

Having checked for all duplications, we can now add the new species to self.

baseclasses.py
        #-- 3 --
        self.spec4Map[key4]  =  species
        self.spec6Map[species.spec6.upper()]  =  species
        self.engMap[species.eng]  =  species