What is wrong with my python movie database code? - python

I was looking for some help with a program I am writing for personal use. It is supposed to export a movie's name and rating (stored in a dictionary) to an external file (file.txt) and then load it at the start of every run. It should also export a movie's name and review (also a dictionary) to another external file (review.txt). I get an error on the line for reading the file.txt and putting it in base. Any clues?
base = {}
#Open and write info to base
with open('file.txt','r') as f:
# Error here :(
base = eval(f.read())
error message:
Traceback (most recent call last):
File "/Users/Will/Documents/movie_database.py", line 18, in <module>
base = eval(f.read())
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing

Your file is empty, so eval() throws an exception because no Python expression was found:
>>> eval('') # empty string
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 0
^
SyntaxError: unexpected EOF while parsing
You shouldn't be using eval() in the first place however, the security implications are terrifying here. Someone that can fool you into loading an arbitrary file can now take over your Python process, and turn your machine into a spam bot zombie.
json or shelve (or directly using pickle, although that has security implications as well) would offer you better and more robust serialisation options. You could also look into using sqlite3 for a SQL database option.

Related

Unexpected EOF while parsing using Python tool cif2cell

I have an error when trying to use a Python tool cif2cell. What cif2cell does it take a .cif file in and return a .cell file.
Intro can be found elsewhere about what it is, but its basically a materials modelling tool, and what I'm trying to do is input a .cif file that represents this 'tile' of atoms, and return a .cell file that is a structure of many of these tiles - The supercell.
In my case I'm going for a 5x5x1 supercell, as can be seen.
Here's the terminal command;
$ ./cif2cell -p castep -f 9000046.cif -o structure1.cell --supercell = [5 5 1]
Which is yielding the following error;
Traceback (most recent call last):
File "./cif2cell", line 354, in <module>
supercellmap = safe_matheval(options.supercellmap)
File "/Users/ 'my name' /Desktop/castep-8.0-macosx-intel/utils.py", line 525, in safe_matheval
return eval(sexpr,{"__builtins__":None},safe_dict)
File "<string>", line 1
=
^
SyntaxError: unexpected EOF while parsing
I have seen that the error has been come across by many writing simple programs, but I have not found any resolutions/examples where a tool has been queried.

PyPDF2 TypeError when trying to run example from lib

I've got PyPDF2 lib from here:
https://github.com/mstamy2/PyPDF2/tree/Python3-3
When trying to run script "Example 1:" from from there see it:
PyPDF2 python versions (2.5 - 3.3) compatibility branch
Traceback (most recent call last):
File "1.py", line 6, in <module>
input1 = PdfFileReader(open("document1.pdf", "rb"))
File "C:\Python33\lib\site-packages\PyPDF2\pdf.py", line 595, in __init__
self.read(stream)
File "C:\Python33\lib\site-packages\PyPDF2\pdf.py", line 1097, in read
streamData = StringIO(xrefstream.getData())
TypeError: initial_value must be str or None, not bytes
What is wrong?
It was a problem related to the compatibility within PyPDF2 and Python 3.
In my case, I have solved it by replacing pdf.py and utils.py with the ones you will find here, where they basically control if you are running Python 3 and, in case you are, receive data as bytes instead of strings.

What does <py3fix> mean?

I am reading the bottle source code and see:
eval(compile('def _raise(*a): raise a[0], a[1], a[2]', '<py3fix>', 'exec'))
I read the documentation about compile and it only tells me that <string> is commonly used. I also googled and searched on stackoverflow but can not find the related info.
So can anyone tell me how <py3fix> affects compilation? And is there any other filename, where can I find the related documentation?
Thanks in advance.
It doesn't affect it at all. It's just a name that it's used to identify where the compiled code is coming from, so you can use string you want.
Like the docs say:
compile(source, filename, mode[, flags[, dont_inherit]])
The filename argument should give the file from which the code was
read; pass some recognizable value if it wasn’t read from a file
('< string>' is commonly used).
in the case where the source is not being read from a file (like here) they suggest that you use <string> so that you know that this code is compiled from a written string.
The person who comited the code, did it when fixing some Bottle Python 2/3 bugs. So I'm guessing that he used <py3fix> as a way to identify the assertion was raised from the def _raise he compiles when the user is running 2.x:
>>> eval(compile('def _raise(*a): raise a[0], a[1], a[2]', '<py3fix>', 'exec'))
>>> _raise(Exception, "error message", None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<py3fix>", line 1, in _raise
Exception: error message
>>> eval(compile('def _raise(*a): raise a[0], a[1], a[2]', '<my_source_file>', 'exec'))
>>> _raise(Exception, "error message", None)Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<my_source_file>", line 1, in _raise
Exception: error message

Import error when using XPath from PyXML-0.8.4

I'm learning Xpath and XSLT using Python 2.6.6-64bit on my unix workstation. For Xpath, I'm using the xpath module provided in PyXml-0.8.4.
However, the import line itself throws the following error:
Traceback (most recent call last):
File "xp.py", line 7, in <module>
from xml.xpath import Evaluate
File "/usr/local/python-2.6.6-64bit/lib/python2.6/site-packages/_xmlplus/xpath/__init__.py", line 112, in <module>
from pyxpath import ExprParserFactory
File "/usr/local/python-2.6.6-64bit/lib/python2.6/site-packages/_xmlplus/xpath/pyxpath.py", line 59, in <module>
from xml.xpath.ParsedAbbreviatedRelativeLocationPath import ParsedAbbreviatedRelativeLocationPath
File "/usr/local/python-2.6.6-64bit/lib/python2.6/site-packages/_xmlplus/xpath/ParsedAbbreviatedRelativeLocationPath.py", line 31
as = ParsedAxisSpecifier.ParsedAxisSpecifier('descendant-or-self')
^
SyntaxError: invalid syntax
Looking it up led me to understand that this is because 'as' is a reserved name in python-2.6 and PyXML-0.8.4 tries to redefine it. However, there are no directions on how to resolve this issue.
Please advise.
PyXML is obsolete and should not be used (see here). That's why there are no updates for it. Many features that were included in it were added to Python's standard library. There are also fully-supported third-party packages for Python XML processing like lxml.

where is the call to encode the string or force the string to need to be encoded in this file?

I know this may seem rude or mean or unpolite, but I need some help to try to figure out why I cant call window.loadPvmFile("f:\games#DD.ATC3.Root\common\models\a300\amu\dummy.pvm") exactly like that as a string. Instead of doing that, it gives me a traceback error:
Traceback (most recent call last):
File "F:\Python Apps\pvmViewer_v1_1.py", line 415, in <module>
window.loadPvmFile("f:\games\#DD.ATC3.Root\common\models\a300\amu\dummy.pvm")
File "F:\Python Apps\pvmViewer_v1_1.py", line 392, in loadPvmFile
file1 = open(path, "rb")
IOError: [Errno 22] invalid mode ('rb') or filename:
'f:\\games\\#DD.ATC3.Root\\common\\models\x07300\x07mu\\dummy.pvm'
Also notice, that in the traceback error, the file path is different. When I try a path that has no letters in it except for the drive letter and filename, it throws this error:
Traceback (most recent call last):
File "F:\Python Apps\pvmViewer_v1_1.py", line 416, in <module>
loadPvmFile('f:\0\0\dummy.pvm')
File "F:\Python Apps\pvmViewer_v1_1.py", line 393, in loadPvmFile
file1 = open(path, "r")
TypeError: file() argument 1 must be encoded string without NULL bytes, not str
I have searched for the place that the encode function is called or where the argument is encoded and cant find it. Flat out, I am out of ideas, frustrated and I have nowhere else to go. The source code can be found here: PVM VIEWER
Also note that you will not be able to run this code and load a pvm file and that I am using portable python 2.7.3! Thanks for everyone's time and effort!
\a and \0 are escape sequences. Use r'' (or R'') around the string to mark it as a raw string.
window.loadPvmFile(r"f:\games#DD.ATC3.Root\common\models\a300\amu\dummy.pvm")

Categories

Resources