[CentralOH] Python Parsing Puzzle

iynaix iynaix at gmail.com
Sun Dec 7 02:57:43 CET 2014


My solution: http://pastebin.com/nbGprTtV

Quick explanation:

As the formulas are basically valid python, minus undefined identifiers, I
sanitize the formulas a little, then feed the formulas into the python ast
module (https://docs.python.org/2/library/ast.html).

Some quick walking of the parse tree yields the needed numbers and
coefficients, which is then placed into a dict. Parsing an ast does not
trigger an eval, so it should be safe, if that is a concern. After that
it's just python data structure manipulation, pretty straightforward.

This was pretty fun! Always wanted to play with the python ast module, but
never had a problem to apply it on.

Cheers,
Xianyi

On Sun, Dec 7, 2014 at 4:34 AM, <jep200404 at columbus.rr.com> wrote:

> On Sat, 6 Dec 2014 10:59:21 -0500, Eric Floehr <eric at intellovations.com>
> wrote:
>
> > How would you solve this problem?
>
> Release early, release often: http://colug.net/python/dojo/20141206/
>
> I would normally use grep, awk, sed, awk, tr, and friends,
> but here's some very sloppy Python code to start things.
>
> What program, ala indent or cb, do folks like for cleaning up
> Python code?
> _______________________________________________
> CentralOH mailing list
> CentralOH at python.org
> https://mail.python.org/mailman/listinfo/centraloh
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/centraloh/attachments/20141207/f60f04be/attachment.html>


More information about the CentralOH mailing list