<p dir="ltr">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.</p>
<div class="gmail_quote">On Dec 6, 2014 8:58 PM, "iynaix" <<a href="mailto:iynaix@gmail.com">iynaix@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>My solution: <a href="http://pastebin.com/nbGprTtV" target="_blank">http://pastebin.com/nbGprTtV</a></div><div><br></div><div>Quick explanation:</div><div><br></div><div>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 (<a href="https://docs.python.org/2/library/ast.html" target="_blank">https://docs.python.org/2/library/ast.html</a>).</div><div><br></div><div>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.</div><div><br></div><div>This was pretty fun! Always wanted to play with the python ast module, but never had a problem to apply it on.</div></div><div class="gmail_extra"><br clear="all"><div><div>Cheers,<br>Xianyi</div></div>
<br><div class="gmail_quote">On Sun, Dec 7, 2014 at 4:34 AM,  <span dir="ltr"><<a href="mailto:jep200404@columbus.rr.com" target="_blank">jep200404@columbus.rr.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span>On Sat, 6 Dec 2014 10:59:21 -0500, Eric Floehr <<a href="mailto:eric@intellovations.com" target="_blank">eric@intellovations.com</a>> wrote:<br>
<br>
> How would you solve this problem?<br>
<br>
</span>Release early, release often: <a href="http://colug.net/python/dojo/20141206/" target="_blank">http://colug.net/python/dojo/20141206/</a><br>
<br>
I would normally use grep, awk, sed, awk, tr, and friends,<br>
but here's some very sloppy Python code to start things.<br>
<br>
What program, ala indent or cb, do folks like for cleaning up<br>
Python code?<br>
<div><div>_______________________________________________<br>
CentralOH mailing list<br>
<a href="mailto:CentralOH@python.org" target="_blank">CentralOH@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/centraloh" target="_blank">https://mail.python.org/mailman/listinfo/centraloh</a><br>
</div></div></blockquote></div><br></div>
<br>_______________________________________________<br>
CentralOH mailing list<br>
<a href="mailto:CentralOH@python.org">CentralOH@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/centraloh" target="_blank">https://mail.python.org/mailman/listinfo/centraloh</a><br>
<br></blockquote></div>