Because the spec2004, spec2002, and spec1998 scripts are almost
identical, we embody the logic of these scripts in a class
called SpeciesSearcher
Here is the interface to the class:
# - - - - - c l a s s S p e c i e s S e a r c h e r - - - - -
class SpeciesSearcher:
'''Logic to search a species authority file.
Exports:
SpeciesSearcher(speciesSet, target):
[ (speciesSet is a BaseSpeciesSet object) and
(target is a string) ->
return a new SpeciesSearcher object with those
values ]
.run():
[ if self.target contains exactly 4 letters ->
sys.stdout +:= report on self.target matching a
4-letter species code in self.speciesSet, if any
else if target contains exactly 6 letters ->
sys.stdout +:= report on self.target matching a
6-letter species code in self.speciesSet, if any
else if self.target is not a valid regular expression->
sys.stdout +:= error message
else ->
sys.stdout +:= report on any matches of English
names in self.speciesSet, treating self.target as a
Python regular expression ]
State/Invariants:
.speciesSet: [ as passed to constructor, read-only ]
.target: [ as passed to constructor, read-only ]
'''
To check whether a given target contains four or six letters, as class variables, we have precompiled regular expressions matching those patterns.
#================================================================
# Manifest constants
#----------------------------------------------------------------
FOUR_LETTER_PAT = re.compile (
r'[a-zA-Z]{4}' # Matches exactly four letters
r'$') # Insure all characters match
SIX_LETTER_PAT = re.compile (
r'[a-zA-Z]{6}' # Matches exactly six letters
r'$') # Insure all characters match