Next / Previous / Contents / Shipman's homepage

17.6. SpeciesSearcher.runEng()

The .engRe() method in the BaseSpeciesSet class does everything we need except writing the output.

baseclasses.py
# - - -   S p e c i e s S e a r c h e r . r u n E n g   - - -

    def runEng(self):
        '''Search English names treating target as a regular expression.
        '''

First we must compile the regular expression. Passing the re.IGNORECASE flag to the re.compile() method forces the matching process to ignore whether letters are uppercase or lowercase, so that for example pattern "a" matches either "a" or "A".

baseclasses.py
        #-- 1 --
        # [ if self.target is a valid regular expression ->
        #     pat  :=  a compiled regular expression that ignores
        #              case
        #   else ->
        #     sys.stdout  +:=  error message(s)
        #     stop execution ]
        try:
            pat = re.compile(self.target, re.IGNORECASE)
        except Exception, detail:
            print("*** Invalid regular expression '%s': %s" %
                    (self.target, detail))
            sys.exit(1)

The rest is straightforward. The self.speciesSet.engRe() method generates a sequence of Species object, one for each match. We simply write those objects as if they were strings, and the Species.__str__() method takes care of formatting them. See Section 13.3, “BaseSpeciesSet.engRe(): Search English names”.

baseclasses.py
        #-- 2 --
        # [ sys.stdout  +:=  report showing English names in
        #       self.speciesSet that match pat ]
        for  species in self.speciesSet.engRe(pat):
            print "  %s" % species