[Chicago] Passing an expression into Python then executing it

D Mahoney dan at streemit.net
Mon Dec 3 21:33:41 CET 2012


I'm working on a log file parser and have a bunch of regular expressions 
I want to watch for and for each regex I have an expression that tells 
me what re output result I want to return for analysis. For example, one 
of my instruction lines might look like:
'(Connection closed by ([a-zA-Z0-9.-]+)', 'groups()[0]'
What that would tell me is that after I do a re.search of the compiled 
regex against my log file line, if the input line matches the regex I 
want to return the contents of groups()[0] to the calling code.

I am at a loss as to how to do this.

I've been experimenting with the "compile" and "exec" commands, but 
haven't been able to figure out how to do what I'm attempting. I've also 
tried using backticks, but so far all I've been able to do is return the 
string "groups()[0]".

Is there a reasonable way to do what I'm attempting, or should I look 
for a different approach?


More information about the Chicago mailing list