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

11.4. TaxonPhotoSet._addContained(): Recursive case

This method is used when the form name to be added is not referred to self's taxon. It recursively adds new nodes to self's subtree down to the level where the form name is referred, at which point the basis case adds the photo at that level.

catweb
# - - -   T a x o n P h o t o S e t . _ _ a d d C o n t a i n e d

    def _addContained(self, birdId, archImage):
        '''Recursive case: add a photo to a descendant taxon.

          [ (birdId is a BirdId referred to a descendant of
            self.taxon) and
            (archImage is a catalog entry as an archindex.ArchImage) ->
              self._childMap  :=  self._childMap with new descendant
                  nodes added as necessary down to birdId.taxon,
                  and archImage added under the name from birdId to
                  the node for birdId.taxon ]            
        '''

Each recursive call first ensures that the tree has a node for the child of self containing the new form name, and then it adds the new catalog entry to the child.

The first task is to determine which child taxon contains the new form name. It might be an actual child of self, or it might be an ancestor further down the tree.

catweb
        #-- 1
        newTaxon = birdId.taxon

        #-- 2
        # [ newTaxon is a descendant of self.taxon ->
        #     childTaxon  :=  the child of self.taxon that
        #     contains newTaxon ]
        childTaxon = self.taxon.childContaining(newTaxon)

Next we make sure that self.childMap has a child node for this child.

catweb
        #-- 3
        # [ if self._childMap has a key for childTaxon.txKey ->
        #     childNode  :=  the corresponding value
        #   else ->
        #     self._childMap [ childTaxon.txKey ]  :=  a new
        #         TaxonPhotoSet for childTaxon
        #     childNode  :=  that same TaxonPhotoSet ]
        try:
            childNode = self._childMap [ childTaxon.txKey ]
        except KeyError:
            childNode = TaxonPhotoSet(childTaxon)
            self._childMap [ childTaxon.txKey ] = childNode

A recursive call to the child node's .addArchImage() method will add the new photo to the correct subtree. See Section 11.2, “TaxonPhotoSet.addArchImage(): Add one photo”.

catweb
        #-- 4
        # [ childNode  :=  childNode with archImage added under
        #                  birdId ]
        childNode.addArchImage(birdId, archImage)