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

28.10. CbcHist._addOneCensus()

cbchistlib.py
# - - -   C b c H i s t . _ a d d O n e C e n s u s

    def _addOneCensus(self, c, colx):
        '''Add a filtered census record to the correct census row.

          [ (c is a pycbc.Census instance) and
            (0 <= colx < self.nCols) ->
              let
                triple == (c.form, c.rel, c.alt_form)
              in:
                if self._rowMap has a key row-key(triple) ->
                  self._rowMap[row-key(triple)]  +:=  census data from
                      c in column (colx)
                else ->
                  self._rowMap[row-key(triple)]  :=   a new CensusRow
                      with the census data from c in column (colx) ]
        '''

The first step is to convert the triple of (form, rel, alt_form) fields from the census record into an abbrMod.BirdId instance, which requires our taxonomy self.txny. We need this BirdId instance for two reasons.

  1. It is required by the CensusRow constructor so that the fully marked-up bird name can appear in its row label.

  2. It is the input to the method described in Section 28.11, “CbcHist._rowKey(): What is the ._rowMap key for this census record?”, which implements row-key: see Section 14.3, “row-key. The row-key value is the actual index in .self_rowMap, the dictionary that contains all the CensusRow instances. The str() functions convert the Unicode values from the database.

Note

With the Postgresql database, when c.rel is abbrMode.REL_SIMPLE, that is, one space, that's what the database gave back.

MySQL deblanks this for us, which breaks the abbrMod.BirdId constructor. Hence we blank-pad the c.rel value to length one to make everything happy.

cbchistlib.py
        #-- 1
        # [ if the triple (c.form, c.rel, c.alt_form) defines a form
        #   valid according to self.txny ->
        #     birdId  :=  a new BirdId instance that represents that
        #                 triple
        #   else -> raise ScriptError ]
        try:
            rel = c.rel.ljust(1)
            birdId = abbrMod.BirdId(self.txny, str(c.form), str(rel),
                                    str(c.alt_form))
        except KeyError:
            if c.rel.strip() == '':
                offender = c.form
            else:
                offender = ("%s %s %s" %
                    (c.form, c.rel, c.alt_form))
            raise ScriptError("Form '%s' is not in the taxonomy "
                             "database." % offender)
        
        #-- 2
        # [ rowKey  :=  row-key(birdId) ]
        rowKey = self._rowKey(birdId)

        #-- 3
        # [ if rowKey is a key in self._rowMap ->
        #     row  :=  the related value
        #   else ->
        #     self._rowMap[rowKey]  :=  a new CensusRow with
        #         cbcHist=(self) and birdId=(birdId)
        #     row  :=  that same CensusRow instance ]
        try:
            row = self._rowMap[rowKey]
        except KeyError:
            row = self._rowMap[rowKey] = CensusRow(self, birdId)

Now that we now which row this census goes in, we can pass it along to the CensusRow instance.

cbchistlib.py
        #-- 4
        row.addCensus(colx, c)