<HTML><BODY style="word-wrap: break-word; -khtml-nbsp-mode: space; -khtml-line-break: after-white-space; "><BR><DIV><BLOCKQUOTE type="cite"><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Date: Thu, 10 Aug 2006 08:51:12 -0400</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">From: Brendon Towle <<A href="mailto:btowle@carnegielearning.com">btowle@carnegielearning.com</A>></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Subject: Re: Eval (was Re: Question about using python as a scripting</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN>language)</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><BR></DIV> <BLOCKQUOTE type="cite"><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Date: 9 Aug 2006 14:12:01 -0700</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">From: "Simon Forman" <<A href="mailto:rogue_pedro@yahoo.com">rogue_pedro@yahoo.com</A>></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Subject: Re: Eval (was Re: Question about using python as a scripting</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><SPAN class="Apple-tab-span" style="white-space:pre"> </SPAN>language)</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">To: <A href="mailto:python-list@python.org">python-list@python.org</A></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Message-ID: <<A href="mailto:1155157921.662196.11210@75g2000cwc.googlegroups.com">1155157921.662196.11210@75g2000cwc.googlegroups.com</A>></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Content-Type: text/plain; charset="iso-8859-1"</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><BR></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">Fredrik Lundh posted a great piece of code to parse a subset of python</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">safely:</DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><BR></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><A href="http://groups.google.ca/group/comp.lang.python/browse_frm/thread/">http://groups.google.ca/group/comp.lang.python/browse_frm/thread/</A><SPAN class="Apple-converted-space"> </SPAN></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">8e427c5e6da35c/a34397ba74892b4e</DIV> </BLOCKQUOTE><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; min-height: 14px; "><BR></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">This, as it turns out, was the most helpful pointer of them all -- <SPAN class="Apple-converted-space"> </SPAN></DIV><DIV style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">thanks!</DIV></BLOCKQUOTE><BR></DIV><DIV>Actually, I spoke too soon. (I should have known better -- always test first.) But:</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>>>>import SafeEval as se</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>>>>se.safeEval('[["AAPL", 35.5, 0.45],["YHOO", 75.68, 0.01]]')</DIV><DIV>[['AAPL', 35.5, 0.45000000000000001], ['YHOO', 75.680000000000007, 0.01]]</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>>>>se.safeEval('[["AAPL", 35.5, 0.45],["YHOO", 75.68, -0.01]]')</DIV><DIV>SyntaxError: malformed expression (-)</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>Seems that parsing negative numbers is outside of the scope of this routine. Here's the source (which is Frederik's source with one minor renaming; I take no credit here); anyone have any ideas?</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>B.</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>==== start source ====</DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>import cStringIO, tokenize </DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>def sequence(next, token, end): </DIV><DIV> out = [] </DIV><DIV> token = next() </DIV><DIV> while token[1] != end: </DIV><DIV> out.append(atom(next, token)) </DIV><DIV> token = next() </DIV><DIV> if token[1] == "," or token[1] == ":": </DIV><DIV> token = next() </DIV><DIV> return out </DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>def atom(next, token): </DIV><DIV> if token[1] == "(": </DIV><DIV> return tuple(sequence(next, token, ")")) </DIV><DIV> elif token[1] == "[": </DIV><DIV> return sequence(next, token, "]") </DIV><DIV> elif token[1] == "{": </DIV><DIV> seq = sequence(next, token, "}") </DIV><DIV> res = {} </DIV><DIV> for i in range(0, len(seq), 2): </DIV><DIV> res[seq[i]] = seq[i+1] </DIV><DIV> return res </DIV><DIV> elif token[0] in (tokenize.STRING, tokenize.NUMBER): </DIV><DIV> return eval(token[1]) # safe use of eval! </DIV><DIV> raise SyntaxError("malformed expression (%s)" % token[1]) </DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>def safeEval(source): </DIV><DIV> src = cStringIO.StringIO(source).readline </DIV><DIV> src = tokenize.generate_tokens(src) </DIV><DIV> src = (token for token in src if token[0] is not tokenize.NL) </DIV><DIV> res = atom(src.next, src.next()) </DIV><DIV> if src.next()[0] is not tokenize.ENDMARKER: </DIV><DIV> raise SyntaxError("bogus data after expression") </DIV><DIV> return res </DIV><DIV><BR class="khtml-block-placeholder"></DIV><DIV>==== end source ====</DIV><DIV><BR class="khtml-block-placeholder"></DIV><BR><DIV> <SPAN class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-align: auto; -khtml-text-decorations-in-effect: none; text-indent: 0px; -apple-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><SPAN class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-align: auto; -khtml-text-decorations-in-effect: none; text-indent: 0px; -apple-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><SPAN class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px 0px; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; text-align: auto; -khtml-text-decorations-in-effect: none; text-indent: 0px; -apple-text-size-adjust: auto; text-transform: none; orphans: 2; white-space: normal; widows: 2; word-spacing: 0px; "><DIV><SPAN style=""><FONT class="Apple-style-span" face="Verdana"><SPAN class="Apple-style-span" style="font-family: Verdana; "><SPAN class="Apple-style-span" style="font-family: Verdana; "><SPAN class="Apple-style-span" style="font-family: Verdana; ">-- </SPAN></SPAN></SPAN><BR style="font-family: Verdana; "></FONT></SPAN><SPAN style=""><FONT class="Apple-style-span" face="Arial"><SPAN class="Apple-style-span" style="font-family: Arial; "><SPAN class="Apple-style-span" style="font-family: Arial; "><SPAN class="Apple-style-span" style="font-family: Arial; ">Brendon Towle, PhD</SPAN></SPAN></SPAN><BR style="font-family: Arial; "></FONT><FONT class="Apple-style-span" face="Arial"><SPAN class="Apple-style-span" style="font-family: Arial; "><SPAN class="Apple-style-span" style="font-family: Arial; "><SPAN class="Apple-style-span" style="font-family: Arial; ">Cognitive Scientist</SPAN></SPAN></SPAN><BR style="font-family: Arial; "></FONT><FONT class="Apple-style-span" face="Arial"><SPAN class="Apple-style-span" style="font-family: Arial; "><SPAN class="Apple-style-span" style="font-family: Arial; "><SPAN class="Apple-style-span" style="font-family: Arial; ">+1-412-690-2442x127</SPAN></SPAN></SPAN><BR style="font-family: Arial; "></FONT><FONT class="Apple-style-span" face="Arial"><SPAN class="Apple-style-span" style="font-family: Arial; "><SPAN class="Apple-style-span" style="font-family: Arial; "><SPAN class="Apple-style-span" style="font-family: Arial; ">Carnegie Learning, Inc.</SPAN></SPAN></SPAN><BR style="font-family: Arial; "></FONT><FONT class="Apple-style-span" face="Arial"><I style="font-family: Arial; font-style: italic; "><SPAN class="Apple-style-span" style="font-family: Arial; font-style: italic; "><SPAN class="Apple-style-span" style="font-family: Arial; font-style: italic; "><SPAN class="Apple-style-span" style="font-family: Arial; font-style: italic; ">The Cognitive Tutor Company ®</SPAN></SPAN></SPAN><BR style="font-family: Arial; font-style: italic; "></I></FONT><FONT class="Apple-style-span" face="Arial"><I style="font-family: Arial; font-style: italic; "><SPAN class="Apple-style-span" style="font-family: Arial; font-style: italic; "><SPAN class="Apple-style-span" style="font-family: Arial; font-style: italic; "><SPAN class="Apple-style-span" style="font-family: Arial; font-style: italic; ">Helping over 375,000 students in 1000 school districts succeed in math.</SPAN></SPAN></SPAN></I></FONT></SPAN></DIV><BR class="Apple-interchange-newline"></SPAN></SPAN></SPAN> </DIV><BR></BODY></HTML>