# - - - 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
( fields from the census record into an
abbrMod.BirdId instance, which requires our taxonomy
self.txny. We need this
instance for two reasons.
It is required by the
so that the fully marked-up bird name can appear in its
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 value is the actual index in
.self_rowMap, the dictionary that contains
CensusRow instances. The
str() functions convert the Unicode
values from the database.
With the Postgresql database, when
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
c.rel value to length
one to make everything happy.
#-- 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
#-- 4 row.addCensus(colx, c)