eval() woes
rdrink
rdrink at gmail.com
Tue Aug 29 00:13:46 EDT 2006
Ok, maybe now I can make some more sense of this, with an example of
real code (sorry if it's a bit dense):
This is the basic function...
def equate(parts,new_eq):
oL = int(parts[0])
iL = int(parts[1])
iR = int(parts[2])
oR = int(parts[3])
oLoL = int(str(oL)+str(oL))
oLiL = int(str(oL)+str(iL))
oLiR = int(str(oL)+str(iR))
oLoR = int(str(oL)+str(oR))
iLoL = int(str(iL)+str(oL))
iLiL = int(str(iL)+str(iL))
iLiR = int(str(iL)+str(iR))
iLoR = int(str(iL)+str(oR))
iRoL = int(str(iR)+str(oL))
iRiL = int(str(iR)+str(iL))
iRiR = int(str(iR)+str(iR))
iRoR = int(str(iR)+str(oR))
oRoL = int(str(oR)+str(oL))
oRiL = int(str(oR)+str(iL))
oRiR = int(str(oR)+str(iR))
oRoR = int(str(oR)+str(oR))
new_seed = eval(new_eq)
return new_seed
... into which is passed two items:
- 'parts' , which is a list e.g ['12','34','56','78'] (of strings)
- and 'new_eq',which is also a string read from a text file, e.g.
"pow(oLiL,2)"
And so...for the first 9 entries (of 480) in the text file where...
pow(oLiL,2)
pow(oLiL,2) - oL
pow(oLiL,2) - iL
pow(oLiL,2) - iR
pow(oLiL,2) - oR
pow(oLiL,2) + oL
pow(oLiL,2) + iL
pow(oLiL,2) + iR
pow(oLiL,2) + oR
pow(oLiL,2)
pow(oL - iL,2)
... eval() works fine.
But on the 10th...
pow(oL - iL,2) - oL
... it bombs with the error:
pow(oL - iL,2) - oL
Traceback (most recent call last):
File "the_farmer2.py", line 264, in ?
seed = equate(parts,equation)
File "the_farmer2.py", line 112, in equate
iL = int(parts[1])
ValueError: invalid literal for int(): -
And what is interseting/odd is:
- For the third, '- iL' evaluates fine...
- as well as the 9th, [where it is nested, oL - iL, inside pow()] ...
- but in the 10th, where a subtraction is called twice, it doesn't.
Which leads me to believe that the problem is either inside eval() it's
self, or in the way these variables are being cast... but I can't tell
which.
(BTW, as a footnote: For each of the above 'equations' the function
equate() was called 500 times... in some cases with the list 'parts'
equaling things like ['0',2','3','0'], so I have no reason to believe
that the problem is with the way the list is being passed in... but I
could be wrong)
Can anyone see something I can't?
Robb
More information about the Python-list
mailing list