Short circuting

Mauro Caceres mauro.caceres at gmail.com
Mon Jan 24 10:53:37 EST 2011


Another option could be something like this:

You can add ids to your regexp, so you can retrive them latter using
groupdict.
Once you have the ids in place, you can join in a new regexp with the "|"
operator which is not greedy, it will stop after the first match.

pattern = (?P<section>re_section)|?P<name>re_section|...

Then you can have another structure with the previous id and the actions you
want to perform on them.

actions = {'section': lambda r: r[18:-5],
                'name': lambda r: r[6:-1]),
               ...}


Finally you just need to iterate over the result. In this case the
dictionary will have only one pair.

result = pattern.search(line)
if result:
   for key,val in result.groupdict().iteritems():
       actions[key](val)

....

-- 
Mauro Cáceres
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110124/2e0b2c8b/attachment-0001.html>


More information about the Python-list mailing list