Next / Previous / Contents / Shipman's homepage

5. Imported modules

This section imports various modules. First, the sys module that gives us access to the standard I/O streams and the command line arguments.

nomcompile3
#================================================================
# Imports
#----------------------------------------------------------------
import sys                      # Standard command lines and streams
import re                       # Standard regular expression package

The standard Python optparse module is used for checking and storing command line options. We import OptionParser, the class that parses option, and also OptionError, the exception that is raised when the options are not correct.

nomcompile3
from optparse import OptionParser, OptionError

From the Python interface defined in the specification, we import the abbr module. We use several items from this module: the abbreviate() function that applies the standard rules for forming bird codes, the engDeComma() function that normalizes bird names; ABBR_L, the length of a bird code; and RE_ABBR, a regular expression for matching a bird code. Because “abbr” is used in several places as a local variable, we'll rename the module as abbrMod.

nomcompile3
import abbr as abbrMod

The XML product file will be written using the etbuilder module described in Python XML processing with lxml. In particular, the E instance is a factory method that builds element, and et is the general XML interface, compatible with Python's standard elementTree module.

nomcompile3
from etbuilder import E, et

File rnc_txny.py is a module that defines manifest constants for all the XML element and attribute names. It is built from the txny.rng schema using the program described in pyrang: A single-sourcing tool for Python-XML applications.

Node (element) names in this file carry the suffix _N, and attribute names end in _A. Word breaks (including CamelCase lowercase-uppercase transitions) are converted to underbars. Examples of element names:

ElementPython name
codeCODE_N
collisionSetCOLLISION_SET_N

Examples of attribute names:

AttributePython name
statusSTATUS_A
stdAbbrSTD_ABBR_A

We will import this module as rnc so that, for example, a reference to the name of a code node would read “rnc.CODE_N”.

nomcompile3
import rnc_txny as rnc

From the author's personal Python library, we'll be using the Singleton, Log, and Scan classes. See Scanning objects for Python: Scan and Log.

nomcompile3
from logscan import Log, Scan
from singleton import Singleton