-1

I've written a basic storage class that holds data in a list of lists writes it to a file and then allows it to be read back from the file for persistence. It was working on the system it was originally written (windows 7) I moved it to a second system (windows 8) and now it seems to be malfunctioning.

The error I'm receiving is:

Traceback (most recent call last):
File "D:\Code Vault\pydb.py", line 69, in <module>
print Users.ReadData()
File "D:\Code Vault\pydb.py", line 23, in ReadData
self.data = cPickle.load(self.Inputstream)
EOFError

The full code for the class is as follows

class table():

    #TODO Insert Key Feild to facilitate ID Search 

    def __init__(self,*args):
        self.tablename = args[0]
        self.data = []
        self.colnames = {}
        self.filename = str(self.tablename)+ ".p"

        for e in args[1:]:
            self.colnames.update({e:len(self.colnames)})

        self.Filestorage = open(self.filename, "wb" )
        self.Filestorage.close()

    def ReadData(self):

        self.Inputstream = open(self.filename, "rb")
        self.Inputstream.seek(0)
        self.data = cPickle.load(self.Inputstream)
        self.Inputstream.close()
        return self.data

    def AddData (self, *args):
        self.tempdata = []

        for e in args:
            self.tempdata.append(e)
        if len(args) == len(self.colnames):
            self.data.append(self.tempdata)
        else:
            return "Incorrect argument quantities"
        return self.tempdata


    def Commit(self):
        self.Outputstream = open(self.filename, "ab" )
        cPickle.dump(self.data, self.Outputstream)
        self.Outputstream.close()
        return self.ReadData()

    def Search(self,query):
        for e in self.ReadData():
            if query in e:
                return self.ReadData().index(e)
        else:
            return "Value " + str(query) + " does not exist"

    def Update(self,query, edit):
        for e in self.data:
            if query in e:
                e[e.index(query)] = edit
        return self.data

    def DumpTable(self):
        self.data = []
        self.Outputstream = open(self.filename+".p", "wb" )
        cPickle.dump(self.data, self.Outputstream)
        self.Outputstream.close()
        self.ReadData()

Users = table("Users","Username","Password")
Users.AddData("bob","123456")
# Users.Commit()
# print Users.data
# print Users.filename
# print Users.data
print Users.ReadData()

It's probably a case of code blindness as it was working previously. Any Ideas would be appreciated

Lobsterbob
  • 21
  • 4
  • also it should be noted that with the Users.commit() function uncommented even when the data has allready been commited and is in the file, the ReadData function outputs correctly when being returned from the commit function – Lobsterbob Jul 12 '15 at 13:15
  • Further inspection shows that when The ReadData() function is called it's erasing the contents of the pickle file... – Lobsterbob Jul 12 '15 at 13:28
  • I figured it out, it was not due to a lack of research as the down-votes might lead you to believe. At the top of the class I had initialised the file to be created every time the module was run thus it was erasing the contents of the file and producing the error removing the file creation and making it conditional solved the problem. @MihaiHangiu I was right in assuming it was code blindness as well as code stupidity not lack of research! – Lobsterbob Jul 12 '15 at 23:01

2 Answers2

1

The amended code that answers the question.

import pickle

class table():

    #TODO Insert Key Feild to facilitate ID Search 

    def __init__(self,*args):
        self.tablename = args[0]
        self.data = []
        self.colnames = {}
        self.filename = str(self.tablename)+ ".p"

        for e in args[1:]:
            self.colnames.update({e:len(self.colnames)})

        try:                                      #creating this conditional prevented the contents being erased upon instantiating the class
            f = open(self.filename,"rb")
        except IOError:
            self.createfile = open(self.filename, "wb" )
            self.createfile.close()

    def ReadData(self):
        self.Inputstream = open(self.filename, "rb")
        self.data = pickle.load(self.Inputstream)
        self.Inputstream.close()
        return self.data

    def AddData (self, *args):
        self.tempdata = []

        for e in args:
            self.tempdata.append(e)
        if len(args) == len(self.colnames):
            self.data.append(self.tempdata)
        else:
            return "Incorrect argument quantities"
        return self.tempdata

    def Commit(self):
        self.Outputstream = open(self.filename, "wb" )
        pickle.dump(self.data, self.Outputstream)
        self.Outputstream.close()

    def createfile(self):
        self.createfile = open(self.filename, "wb" )
        self.createfile.close()

    def Search(self,query):
        for e in self.ReadData():
            if query in e:
                return self.ReadData().index(e)
        else:
            return "Value " + str(query) + " does not exist"

    def Update(self,query, edit):
        for e in self.data:
            if query in e:
                e[e.index(query)] = edit
        return self.data

    def DumpTable(self):
        self.data = []
        self.Outputstream = open(self.filename+".p", "wb" )
        pickle.dump(self.data, self.Outputstream)
        self.Outputstream.close()
        self.ReadData()
Lobsterbob
  • 21
  • 4
0

The solution seems to be here pickle.load() raising EOFError in Windows

i.e. file needs to be open for read on binary mode with "rb" flag.

Community
  • 1
  • 1
Mihai Hangiu
  • 588
  • 4
  • 13