Next / Previous / Contents / Shipman's homepage

13.3. BaseSpeciesSet.engRe(): Search English names

This method searches through all the English names in the instance, using Python's re regular expression module. For example, given a BaseSpeciesSet instance named bss, to search for names containing “unid” followed later on by “hum”, you might use this sequence:

    import re
    unHum = re.compile(r'unid.*hum')
    for  sp in bss.engRe(unHum):
        print unHum.eng

Here's the interface:
# - - -   B a s e S p e c i e s S e t . e n g R e   - - -

    def engRe(self, re):
        '''Search English names for a compiled regular expression.

In order to generate the names in ascending order, first we extract a list of all the keys in the self.engMap, then we sort them.
        #-- 1 --
        # [ engList  :=  a list of all keys in self.engMap in
        #                ascending order ]
        engList = self.engMap.keys()

Then we iterate over those names. For any that match, we pull the Species object out of self.engMap. Note that we use the .search() method, which finds the regular expression anywhere in the string, instead of .match() which finds only matches at the start of the string.
        #-- 2 --
        # [ generate the values from self.engMap for keys in
        #   engList that match re ]
        for  eng in engList:
            #-- 2 body --
            # [ if eng matches re ->
            #     yield self.engMap[eng]
            #   else -> I ]
            m =
            if  m:
                yield self.engMap[eng]

At this point we could raise StopIteration, but falling out of the bottom of a generator has the same effect.