473,805 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

eval raising SyntaxError (was No Subject)


On Tue, 2007-12-11 at 16:55 -0800, katie smith wrote:
I tried your suggestions and all that came up was the error
Traceback (most recent call last):
File "C:\Python25\em pire\Empire Strategy.pyw", line 1788, in
<module>
NewMap1= eval (NewMap1, {}, {})
File "<string>", line 1
Tropical Islands
^
SyntaxError: invalid syntax

And what is this about trusting your source? The string is taken
exactly from another part in the program so I know for sure it is
correct if that is what that means.
Katie,

First, please provide a useful subject heading when posting to the list.
It makes everyone's life easier when searching the archives.

Second, the example you provided in your last post was converting a
string of the form "[1,2,3,4,5]" to a list of integers. What is
happening here, is you are trying to convert a string of the form
"Tropical Islands" to... what?

I assume you are not getting the string you thought you would get. Eval
is acting as advertised. It is taking the line "Tropical Islands" and
trying to parse it as python code. If that's not what you wanted to do,
the question you should be asking is "why did I get the string "Tropical
Islands" instead of "[1,2,3,4,5]". You should not be asking "what is
eval doing wrong."

Before you were advised not to use eval unless you were sure of the
source of your inputs.

If the source of your input is "me," or "trusted operators" then you are
not sure of your inputs, because you might type something in thinking
that the focus was on a different window, or you might mistype
something.

If the source of your inputs is a line you are extracting from another
python file, you are not sure of your inputs, because there could be an
error in your python file, or someone could add a line, so while
pulling, e.g., line 15 used to give you "[1,2,3,4,5]", that's now line
16, and line 15 gives you "Tropical Islands" instead, because somebody
thought there should be an extra blank line before the start of the code
or something.

If the source of your inputs is the output of another function, why is
it passing you a repr of a list instead of the list itself.

Almost NEVER use eval. If you find yourself using eval, make your next
task figuring out how not to use eval.

Cheers,
Cliff
Dec 12 '07 #1
0 1163

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3517
by: Chris Connett | last post by:
I have an interesting problem with eval(). ---Background Info--- The program I'm working on launches a separate process with a popen to do some highly specialized processing of input, then this process leaves resultant data structured in files, in python syntax, at a known absolute path, which is then read in, eval()'ed, and processed further. It works fine when there are no errors in processing by this external program, but when there...
15
3673
by: manstey | last post by:
Hi, I have a text file called a.txt: # comments I read it using this:
1
2220
by: =?ISO-8859-1?Q?Tor_Erik_S=F8nvisen?= | last post by:
Hi, A while ago I asked a question on the list about a simple eval function, capable of eval'ing simple python constructs (tuples, dicts, lists, strings, numbers etc) in a secure manner: http://groups.google.com/group/comp.lang.python/browse_thread/thread/58a01273441d445f/ also pointed to a simple eval function by Fredrik Lundh: http://effbot.org/zone/simple-iterator-parser.htm. His solution, using module tokenize, was short and...
10
1229
by: TheSaint | last post by:
Hi, It seems to be strange that give me syntax error inside an eval statement. I'm looking at it carefully but I can't see any flaw. Here it's part of the code: for nn in stn_items: value= eval('cp.%s' %nn) if value and (nn in 'log, trash, multithread, verbose, download'):
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10604
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10356
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10361
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10103
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6874
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5676
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.