André a écrit :
(snip)
you don't need to use pattern.items()...

Here is something I use (straight cut-and-paste):

    def parse_single_line(self, line):
        '''Parses a given line to see if it match a known pattern'''
        for name in self.patterns:
            result = self.patterns[name].match(line)

FWIW, this is more expansive than iterating over (key, value) tuples using dict.items(), since you have one extra call to dict.__getitem__ per entry.

            if result is not None:
                return name, result.groups()
        return None, line


where self.patterns is something like
self.patterns={
'pattern1': re.compile(...),
'pattern2': re.compile(...)
}

The one potential problem with the method as I wrote it is that
sometimes a more generic pattern gets matched first whereas a more
specific pattern may be desired.

As usual when order matters, the solution is to use a list of (name, whatever) tuples instead of a dict. You can still build a dict from this list when needed (the dict initializer accepts a list of (name, object) as argument).

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to