packing things back to regular expression

Tim Chase python.list at tim.thechases.com
Wed Feb 20 14:26:33 EST 2008


> mytable = {"a" : "myname"}
>>> re.SomeNewFunc(compilexp, mytable)
> "myname"

how does SomeNewFunc know to pull "a" as opposed to any other key?

>>> mytable = {"a" : "1"}
>>> re.SomeNewFunc(compileexp, mytable)
> ERROR

You could do something like one of the following 3 functions:

   import re
   ERROR = 'ERROR'
   def some_new_func(table, regex):
     "Return processed results for values matching regex"
     result = {}
     for k,v in table.iteritems():
       m = regex.match(v)
       if m:
         result[k] = m.group(1)
       else:
         result[k] = ERROR
     return result

   def some_new_func2(table, regex, key):
     "Get value (if matches regex) or ERROR based on key"
     m = regex.match(table[key])
     if m: return m.group(0)
     return ERROR

   def some_new_func3(table, regex):
     "Sniff the desired key from the regexp (inefficient)"
     for k,v in table.iteritems():
       m = regex.match(v)
       if m:
         groupname, match = m.groupdict().iteritems().next()
         if groupname == k:
           return match
     return ERROR

   if __name__ == "__main__":
     NAME = 'name1'
     mytable = {
       'a': 'myname',
       'b': '1',
       NAME: 'foo',
       }
     regexp = '(?P<%s>[a-z]+)' % NAME
     print 'Using regex:'
     print regexp
     print '='*10

     r = re.compile(regexp)
     results = some_new_func(mytable, r)
     print 'a: ', results['a']
     print 'b: ', results['b']
     print '='*10
     print 'a: ', some_new_func2(mytable, r, 'a')
     print 'b: ', some_new_func2(mytable, r, 'b')
     print '='*10
     print '%s: %s' % (NAME, some_new_func3(mytable, r))

Function#2 is the optimal solution, for single hits, whereas 
Function#1 is best if you plan to repeatedly extract keys from 
one set of processed results (the function only gets called 
once).  Function#3 is just ugly, and generally indicates that you 
need to change your tactic ;)

-tkc






More information about the Python-list mailing list