[CentralOH] Python Parsing Puzzle
erik.n.welch at gmail.com
Sun Dec 7 21:34:40 CET 2014
Thanks for posting the puzzle, Eric. It's great to see the different
approaches people are taking.
Here's my solution using regular expressions and pandas (which I'm sure
could be cleaned up and simplified):
On Sat, Dec 6, 2014 at 11:37 PM, Thomas Winningham <winningham at gmail.com>
> 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.
>> 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>
>>> > 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
>> CentralOH mailing list
>> CentralOH at python.org
> CentralOH mailing list
> CentralOH at python.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the CentralOH