Next / Previous / Contents / Shipman's homepage

9. class Taxon

Each taxon in the tree is represented by one Taxon instance.

xnomo3.py
# - - - - -   c l a s s   T a x o n   - - - - -

class Taxon:
    """Represents one taxonomic grouping of organisms.

      Exports:
        Taxon ( txny, parentTaxon, taxonNode ):
          [ (txny is the containing Txny instance) and
            (parentTaxon is the parent Taxon instance, or None for
            root) and
            (taxonNode is a TAXON_N Element) ->
              return a new Taxon instance pointing back to txny, with
              parent=parentTaxon, representing taxonNode and its
              descendants ]
        .txny:           [ as passed to constructor, read-only ]
        .parent:         [ parentTaxon passed to constructor, read-only ]
        .abbr:
          [ if self has a standard abbreviation ->
              that abbrevation
            else -> None ]
        
        .rank:           [ self's taxonomic rank as a Rank instance ]
        .sci:            [ self's scientific name ]
        .txKey:
          [ self's taxonomic key, a string of digits that orders
            taxa in phylogenetic order ]
        .contains ( other ):
          [ other is a Taxon instance in self.txny ->
              if self is an ancestor of other ->
                return 1
              else -> return 0 ]
        .nearestAncestor ( other ):
          [ other is a Taxon ->
              return the Taxon representing the nearest ancestor
              of self and other ]
        .__cmp__ ( self, other ):
          [ other is a Taxon ->
              if self precedes other ->
                return a negative number
              else if self is the same as other ->
                return 0
              else -> return a positive number ]
        .__getitem__ ( n ):
          [ n is an integer ->
              if 0 <= n < (number of self's children) ->
                return self's nth child, counting from 0
              else -> raise KeyError ]
        .__iter__ ( self ):
          [ return an iterator that generates self's children
            in taxonomic order ]
        .__len__ ( self ):
          [ return number of self's child taxa ]
        .__str__ ( self ):
          [ return a string representation of self ]
        .eng:            [ self's English name, normalized ]
        .engComma:       [ self's English name, inverted ]
        .tex:            [ self's English name with TeX markup ]
        .texComma:       [ same as .tex, but inverted ]
        .engHtml(cssClass=None):
          [ if cssClass is None ->
              return self's English name as HTML with italics marked up
              using the deprecated 'i' element
            else ->
              return self's English name as HTML with italics marked up
              using a span element with class=cssClass ]
        .engHtmlComma(cssClass=None):
          [ like .engHtml but "Generic[, Specific]" ]
        .engHtmlSubelt(node, cssClass=None):
          [ (node is an et.Element) ->
              if cssClass is None ->
                node  :=  node with self's English name added, italics
                    marked up using the deprecated 'i' element
              if cssClass is a string ->
                node  :=  node with self's English name added, italics
                    marked up using a span element with class=cssClass ]
        .engHtmlSubeltComma(node, cssClass=None):
          [ same as .engHtmlSubelt but with the name inverted ]

The internal state includes a list of self's child taxa, if any.

xnomo3.py
      State/Invariant:
        .__rawEng:
          [ English name from file, inverted, with underbars to
            indicate italics ]
        .__childList:
          [ a list of the direct child taxa of self, in phylogenetic
            order, if any, as Taxon instances ]
    """

The private .__childList attribute will hold pointers to self's children, if any.

9.1. Taxon.contains()

The argument is another Taxon. The method returns True if and only if the other taxon is contained in self, False otherwise.

The logic is straightforward: if the nearest common ancestor of self and the other taxon is self, then self contains the other taxon.

xnomo3.py
# - - -   T a x o n . c o n t a i n s   - - -

    def contains ( self, other ):
        """Does self contain other?
        """

        #-- 1 --
        # [ ancestor  :=  nearest ancestor of self and other ]
        ancestor = self.nearestAncestor(other)

        #-- 2 --
        return (self is ancestor)