Problem with pickle and restarting a program

Peace abhishek1899 at gmail.com
Wed Mar 19 23:05:31 CET 2014


In my GUI, the user enters two values: cellNumber(Integer) and SerialNumber(String)

I have the following piece of code in my view/controller:

####################The following function is called after the user enters the cellnumber and serial number##############
def ListenForDetails(status):
	cellNumber = status[0]
	SerialNumber = status[1]
	ModelInfo().CellList[cellNumber].receiveSerialNumber(SerialNumber)

------------------In my controller thread-----------------
class Controller(Thread):
	def __init__(self):
		Thread.__init__(self)
		self.start()
	
	def run(self):
		global SynchronizationVariable
		while SynchronizationVariable != 1:
			time.sleep(0.01)
		while 1:
			StateChanged = False
			for cells in ModelInfo().CellList:
				rc = cells.PollCells()
				if rc == 0:
					StateChanged = True
			if StateChanged:
				ModelInfo().save()
			time.sleep(1)

-------------------------------In my model.py script-------------------------------------

class CellInfo(): #This class encapsulates all the information present in a cell
	def __init__(self,cellNo):
		self.SerialNo = ""
		self.cellNo = cellNo
	
	def receiveSerialNumber(self, SerialNumber):
		self.SerialNo = SerialNumber

        def PollCells(self):
		RC = self.StateMachine[self.CurrentState](self)
		return RC

        ...all state machine functions here...


"""Model Info below"""
class ModelInfo(): #This class creates a list of cells.
	def __init__(self):
		print("Setting up a list of cells to iterate on")
		self.CellList = [] #List of all the cell Objects
		for index in xrange(0,24):
			print(self.CellList)
			self.CellList.append(CellInfo(index))
		
	def save(self):
		with open("RestoreInfo.p", "wb") as f:
			pickle.dump(self,f)

Model = ModelInfo() #Constructs an empty model

"""IN MAIN (In the controller script)"""
if __name__ == '__main__':
    if os.path.isfile( "RestoreInfo.p" ):
        with open("RestoreInfo.p", "rb") as f:
            model = pickle.load( f );
    else:
        model = model.ModelInfo();


The serial number field always remains empty even though I enter from the GUI and the receiveSerialNumber function is called and I explicitly initialize it to the variable in the model.
I'm trying to save the state of the program so that next time I open the application it resumes from where it left off. The problem is when I restart the application, the serial number is empty. What am I doing wrong here? Whenever I get a change in state (the RC value), I pickle and save to a file (restoreinfo.p). When I restart the application, the serial number is no longer there even though I enter it. Could you please let me know what is going wrong? Thank you.



More information about the Python-list mailing list