March 22, 2011 4 Comments
First Hermit Hello, are you a hermit by any chance?
Second Hermit Yes that’s right. Are you a hermit?
First Hermit Yes, I certainly am.
Second Hermit Well I never. What are you getting away from?
First Hermit Oh you know, the usual – people, chat, gossip, you know.
Second Hermit Oh I certainly do – it was the same with me. I mean there comes a time when you realize there’s no good frittering your life away in idleness and trivial chit-chat.
If you remember back a few tutorials ago, we learnt how to pickle objects so that we could get them back later. To pickle the object triviaQuestions (which was a list object) we first imported the pickle module, then opened a file and dumped the object into the file, then close()d the file:
->code snippet removed to note  because I don't want you typing it in<-
Note here that when we opened the file with ‘w’ this meant we were ‘w’riting to it. If the file existed, then Python wiped it ready for us to write something new to it. You need to be careful when reading from a file that you have ‘r’ (for ‘r’ead) in the open() command, otherwise, instead of reading the data, you’ll wipe it instead!!!!
Let’s load the triviaQuestions list:
>>> import cPickle >>> filename = "p4kTriviaQuestions.txt" >>> fileObject = open(filename,'r') # note the 'r' for 'read' >>> triviaQuestions = cPickle.load(fileObject) >>> len(triviaQuestions) 2 >>> triviaQuestions ['Who expects the Spanish Inquisition?', 'Nobody', 'Eric the Hallibut', 'An unladen swallow', 'Brian', 'Me!'] >>> triviaQuestions ['What is the air-speed velocity of an unladen swallow?', 'What do you mean? African or European swallow?', '10 m/s', '14.4 m/s', '23.6 m/s']
Wow! We have preserved the list between tutorials. So, rather than continually typing out all the questions every time, we only have to add new questions.
In this code we’ve imported cPickle. We mentioned cPickle earlier. It does (for our purposes) the same thing as pickle, only it does it faster. cPickle and pickle behave little differently in some special circumstances, but that’s not relevant for us. We also used ‘r’ rather than ‘w’ when we opened the file. Also, cPickle doesn’t care about the name the object had when it was dump()ed. We could have loaded the pickle into an object with a different name:
>>> fileObject.close() >>> fileObject = open(filename,'r') >>> aList = cPickle.load(fileObject) >>> fileObject.close() >>> aList == triviaQuestions True
We had to first close() the fileObject so that cPickle read from the start of the file. If there are multiple objects pickled to a file it loads them out in the order they were dumped in. So once it load()s an object cPickle has moves to the end of the object in the file in order to read the next object. If there is only one object, then cPickle will be at the end of the file and won’t be able to read any more.
As we can see, we’ve load()ed the same object under a different name (aList). You should also note here that when an object is load()ed, it doesn’t change the file (unlike when an object is dump()ed). We could load multiple copies of the object for ever if we wanted to.
Let’s add another question and then dump the list to the file:
sampleQuestion =  # this clears the earlier entries # if we append without doing this # we'll have multiple questions in the wrong list # first the question sampleQuestion.append("Is this the right room for an argument?") # then the correct answer sampleQuestion.append("I've told you once.") # now one or more incorrect answers sampleQuestion.append("No") sampleQuestion.append("Down the hall, first on the left") sampleQuestion.append("Yes")
now we append() this question to the end of the list:
Finally, we dump the object back into the file. Be careful – when we open the file to write (‘w’) we lose what’s saved in it. That’s no problem if we fill it back up with data, so be sure to dump the question list!
>>> fileObject = open(filename,'w') # data that was in the file is now gone! >>> cPickle.dump(triviaQuestions,fileObject) # ok, put new data in >>> fileObject.close()
If you were worried about losing your data you would first dump() the object to a file with a temporary name, then when that was successful, rename the existing file as ‘p4kTriviaQuestions.bak’ and the newly created file to ‘p4kTriviaQuestions.txt’. Then if anything went wrong with saving the data you would still have the data backed up in the “.bak” file.
- add some more questions to your triviaQuestions list by load()ing the list from your file, adding questions, then dump()ing the list back to the file. Make sure you use ‘r’ when you open a file you are going to load() from and ‘w’ when you open a file to dump() to.
>>> import pickle >>> # This is just a flashback don't retype this code because it will wipe your data!!!! >>> filename = "p4kTriviaQuestions.txt" >>> fileObject = open(filename,'w') >>> pickle.dump(triviaQuestions,fileObject) >>> fileObject.close()