[CentralOH] Python Parsing Puzzle

Thomas Winningham winningham at gmail.com
Sun Dec 7 06:37:28 CET 2014

I was curious about ast as well. I played with it once one time. So it is
true that parsing then shouldn't evaluate the code? I'm trying to think if
there are there any other possible "gotchas" ? I guess I should just read
more. I always figured things like Hy and PyPy make extensive use of
concepts in this general area. I played once on time with parser generators
in Java until I found a different solution for whatever I was doing. I
guess had I ever taken a compiler class I may know better how to Google
these things, or even post about them, heh.
On Dec 6, 2014 8:58 PM, "iynaix" <iynaix at gmail.com> wrote:

> 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
> _______________________________________________
> 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/0ef24de3/attachment.html>

More information about the CentralOH mailing list