Eval (was Re: Question about the use of python as a scripting language)
Brendon Towle
btowle at carnegielearning.com
Thu Aug 10 10:02:59 EDT 2006
Oops -- I missed the subject line on my last post.
On 10 Aug 2006, at 9:41 AM, skip at pobox.com wrote:
> Brendon> Seems that parsing negative numbers is outside of the
> scope of
> Brendon> this routine. Here's the source (which is Frederik's
> source
> Brendon> with one minor renaming; I take no credit here);
> anyone have
> Brendon> any ideas?
>
> Negative numbers are actually tokenized as a MINUS followed by a
> NUMBER:
>
> % python
> Python 2.5b2 (trunk:50921, Jul 28 2006, 20:21:50)
> [GCC 4.0.0 (Apple Computer, Inc. build 5026)] on darwin
> Type "help", "copyright", "credits" or "license" for more
> information.
>>>> - 47
> -47
>
> Try changing your atom function to detect a minus sign, remember
> that fact,
> then require the next token to be a number.
While I appreciate the suggestion, this is exactly the route I don't
want to take -- it essentially involves rewriting the Python parser/
tokenizer, which has already been written and tested by people much
more qualified than I. In fact, the code I was using (Frederik's
code) has already been written and tested by people much more
qualified than I, and it _still_ doesn't work.
A shortcut occurs to me; maybe someone can tell me what's wrong with
my reasoning here. It seems that any string that is unsafe to pass to
eval() must involve a function call, and thus must contain an opening
paren. Given that I know that the data I expect contains no parens,
would people expect this code to be safe:
START_MARKER = 'var table_body = '
END_MARKER = '];'
def extractStockData(data):
pos1 = data.find(START_MARKER)
pos2 = data.find(END_MARKER, pos1)
parenPos = data.find('(')
if parenPos >= 0:
raise "Data format changed -- found a paren"
else:
return eval(data[pos1+len(START_MARKER):pos2+1])
B.
--
Brendon Towle, PhD
Cognitive Scientist
+1-412-690-2442x127
Carnegie Learning, Inc.
The Cognitive Tutor Company ®
Helping over 375,000 students in 1000 school districts succeed in math.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20060810/ab8635f5/attachment.html>
More information about the Python-list
mailing list