[Chicago] Passing an expression into Python then executing it
D Mahoney
dan at streemit.net
Tue Dec 4 19:11:17 CET 2012
Thanks for the wonderful advice, everybody! I love reading this mailing
list - I learn a lot here.
It does indeed look like callbacks are the way to go. That would allow
me to easily resolve a couple other small issues I've been seeing with
my code. I'll make the changes to use callbacks this afternoon.
It would be wonderful if I could just always grab groups()[0], but in a
few instances I need to grab other stuff. Kernel messages, for example,
where the string looks like "kernel: [<ffffffff8000b>]
vfs_read+0xcb/0x171". In this instance I want to capture the "vfs_read"
part but don't care (for summary purposes" what memory address is
associated with it. I could take care of this with a more elaborate
regex, but given the audience that is going to be using this tool a
simpler regex that makes use of callbacks ought be a better fit.
Again, thanks for all the input. I'm going to study all of your
suggestions, and move my code to the callback solution right away.
On 12/03/2012 04:20 PM, Brantley Harris wrote:
> Essentially you have instructions that are a pair (regex, result).
> How complex do you want ``result`` to be? Kumar is right that you
> can be infinitely complex by passing it in as a pure function. This
> is likely you're best bet.
>
> However, if you need it to be a string, because of easy configuration
> or some other reason, then you have to pass something meaningful into it.
>
> You have a few options after that, one is to just have a {0, 1, 2, 3,
> ..., n} and return the m.groups()[n]. But something tells me you want
> something a bit more complex.
>
>
> Another is completely unsafe, security-wise, but unquestionably easy
> and powerful:
>
> def get_result(m, code):
> return eval(code, {}, {'m': m})
>
> Then you can do:
> >>> regex, result = 'Connection closed by ([a-zA-Z0-9.-]+)',
> 'm.groups()[0]'
> >>> m = re.match(regex, 'Connection closed by TheCloser')
> >>> print get_result(m, result)
> TheCloser
>
> This is, as I said, completely insecure. If anyone is allowed to
> specify the "result", they could do whatever they wanted to your system.
>
>
> Another is to run it as a template. Safe and secure, and allows
> rather high complexities:
>
> from jinja2 import Template
> def get_result(m, code):
> return Template(code).render(m=m)
>
> Then you can do:
> >>> regex, result = 'Connection closed by ([a-zA-Z0-9.-]+)', '{{
> m.groups()[0] }}'
> >>> m = re.match(regex, 'Connection closed by TheCloser')
> >>> print get_result(m, result)
> TheCloser
>
> Along with any of the other complexities available in the Jinja2
> Template Library.
>
>
> On Mon, Dec 3, 2012 at 2:33 PM, D Mahoney <dan at streemit.net
> <mailto:dan at streemit.net>> wrote:
>
> For
>
>
>
>
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> http://mail.python.org/mailman/listinfo/chicago
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/chicago/attachments/20121204/faffd69d/attachment.html>
More information about the Chicago
mailing list