Problem with pickle and restarting a program

P

Peace

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 acell
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.
 
D

dieter

Peace said:
...
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.

You may want to use debugging to determine what goes on in detail.

There are commercial debuggers with a graphical user interface
(maybe even free ones). Python comes with a simple command line
debugger ("pdb").
 
P

peace

You may want to use debugging to determine what goes on in detail.



There are commercial debuggers with a graphical user interface

(maybe even free ones). Python comes with a simple command line

debugger ("pdb").

I tried doing that. I still could not figure out what was wrong. Thank you.
 
D

dieter

peace said:
...
I tried doing that. I still could not figure out what was wrong. Thank you.

Debugging is often not easy. An essential strategy is "divide and conquer":
i.e. you split the complete scenario into segments and analyse each
segment to find out where the bad thing happens.

Related to pickle, there is one point to lock at closely: where
you "dump" the data. Verify, that you are dumping the correct (expected) data.
If the data is not as you expect at that point, you must analyse
the first part (from where the data was produced up to the dumping point);
on the other hand, if the dumped data is correct, you would verify
that "load" restores this same data (this will be very likely the case)
and if it does why it became wrong afterward.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top