Next / Previous / Contents / Shipman's homepage

12. test3: A test driver

Here is a small script that exercises some of the main functions of the module. On the command line, one provides as arguments a sequence of one or more bird codes.

test3 abbr ...

A report is printed about each one (assuming it is valid) showing: the standard taxon to which it is assigned; the chain of inheritance from that taxon to the root; and the English name from which the code was derived, if that is different from the taxon's English name.

test3
#!/usr/bin/env python
#================================================================
# test3: Test driver for xnomo3.py.
#
# Do not edit this file.  It is extracted automatically from the
# documentation here:
#   http://www.nmt.edu/~shipman/xnomo3/ims3/
#----------------------------------------------------------------

# - - - - -   I m p o r t s

import sys
import xnomo3
import abbr as abbrMod
from etbuilder import E, et

# - - - - -   M a n i f e s t   c o n s t a n t s

FORMAT = "%-50s %s"

# - - - - -   m a i n

def main():
    '''Main program.

      [ (xnomo3's XML input file is available locally) and
        (command line contains one or more bird codes) ->
          sys.stdout  +:=  report on each code's status with respect
                           to that XML input file ]          
    '''
    #-- 1 --
    # [ xnomo3's XML input file is available locally ->
    #     txny  :=  an xnomo3.Txny instance representing that file
    #   else ->
    #     sys.stderr  +:=  message(s)
    #     stop execution ]
    try:
        txny = xnomo3.Txny()
    except IOError, x:
        fatal("Can't open the taxonomy file: %s" % x)

    #-- 2 --
    # [ if command line has any arguments ->
    #     abbrList  :=  those arguments
    #   else ->
    #     sys.stderr  +:=  error message
    #     stop execution ]
    abbrList = sys.argv[1:]
    if len(abbrList) == 0:
        fatal("*** Usage: %s ABBR ..." % sys.argv[0])

    #-- 3 --
    # [ sys.stdout  +:=  reports on bird codes in abbrList ]
    for abbr in abbrList:
        report(txny, abbr)

# - - -   r e p o r t

def report(txny, abbr):
    '''Show status of abbr in txny.
    '''
    print "\n==== Code '%s' ====" % abbrMod.fixAbbr(abbr)
    try:
        abbrEng = txny.abbrToEng(abbr)
        abbrHtml1 = txny.abbrToHtml(abbr)
        abbrHtmlComma1 = txny.abbrToHtmlComma(abbr)
        abbrHtml2 = txny.abbrToHtml(abbr, cssClass='sci')
        abbrHtmlComma2 = txny.abbrToHtmlComma(abbr, cssClass='sci')
        abbrTex = txny.abbrToTeX(abbr)
        abbrTexComma = txny.abbrToTeXComma(abbr)

        taxon = txny.lookupAbbr(abbr)
    except KeyError:
        print "No such code."
        return

    if abbrEng != taxon.eng:
        print "--- English name is not the standard one."
        print FORMAT % (abbrEng, ".abbrToEng()")
        print FORMAT % (abbrHtml1, ".abbrToHtml")
        print FORMAT % (abbrHtmlComma1, "...inverted")
        print FORMAT % (abbrHtml2, "abbrToHTML+CSS")
        print FORMAT % (abbrHtmlComma2, "...inv")
        print FORMAT % (abbrTex, ".abbrToTex")
        print FORMAT % (abbrTexComma, "...inv")
        abbrNodeTests(txny, abbr)
        print "---"

    ancestorReport(taxon)

    print FORMAT % (taxon.eng, "taxon.eng")
    print FORMAT % (taxon.engComma, "...inv")
    print FORMAT % (taxon.engHtml(), ".engHtml")
    print FORMAT % (taxon.engHtmlComma(), "...inv")
    print FORMAT % (taxon.engHtml(cssClass='sci'), ".engHtml+CSS")
    print FORMAT % (taxon.engHtmlComma(cssClass='sci'),
                    "...inv")
    print FORMAT % (taxon.tex, "TeX")
    print FORMAT % (taxon.texComma, "...inv")

    taxonNodeTests(taxon)

# - - -   a b b r N o d e T e s t s

def abbrNodeTests(txny, abbr):
    '''Test txny methods in -htmlSubelt family.
    '''
    td = E.td()
    txny.abbrHtmlSubelt(abbr, td)
    print FORMAT % (et.tostring(td), "txny-subelt")

    td = E.td()
    txny.abbrHtmlSubeltComma(abbr, td)
    print FORMAT % (et.tostring(td), "...inv")

    td = E.td()
    txny.abbrHtmlSubelt(abbr, td, cssClass='sci')
    print FORMAT % (et.tostring(td), "txny-subelt")

    td = E.td()
    txny.abbrHtmlSubeltComma(abbr, td, cssClass='sci')
    print FORMAT % (et.tostring(td), "...inv")


# - - -   a n c e s t o r R e p o r t

def ancestorReport(taxon):
    '''Show the ancestor taxa.
    '''
    t = taxon
    while t is not None:
        print ("    %02d %s: %s" %
               (t.rank.depth, t.sci, t.eng))
        t = t.parent

# - - -   t a x o n N o d e T e s t s

def taxonNodeTests(taxon):
    '''Test the 'taxon.engHtmlSubelt' family.
    '''
    td = E.td()
    taxon.engHtmlSubelt(td)
    print FORMAT % (et.tostring(td), "taxon.engHtmlSubelt")

    td = E.td()
    taxon.engHtmlSubeltComma(td)
    print FORMAT % (et.tostring(td), "...inv")

    td = E.td()
    taxon.engHtmlSubelt(td, cssClass='sci')
    print FORMAT % (et.tostring(td), "+CSS")

    td = E.td()
    taxon.engHtmlSubeltComma(td, cssClass='sci')
    print FORMAT % (et.tostring(td), "...inv")
   

# - - -   f a t a l

def fatal(*L):
    '''Print a message and terminate.

      [ L is a list of strings ->
          sys.stderr  +:=  concatenated elements of L
          stop execution ]
    '''
    print >>sys.stderr, ''.join(L)
    sys.exit(1)

# - - - - -   E p i l o g u e

if __name__ == '__main__':
    main()