Pickling Objects Into Files - Page 13
June 7, 2001
Pickling is a major benefit in Python. Use this ability!
Write an arbitrary Python object with cPickle.dump.
|
|
Python can write any data structure into a file, and read that
data structure back out of a file and re-create it, with just a
few commands. This is an unusual ability, but one that is highly
useful. It can save the programmer many pages of code which do
nothing but dump the state of a program into a file (and can save
a similar amount of code which does nothing but read that state
back in).
Python provides this ability via the cPickle module.
cPickle is actually a C language rewrite of the
original pickle module. We are using it in our
examples here as it is a thousand times faster than the
pickle module. Pickling is very powerful but very
simple to use. For example, assume that the entire state of a
program is held in three variables, a, b, and
c. We can save this state to a file called "state"
as follows:
import cPickle
.
.
.
file = open("state", 'w')
cPickle. dump(a, file)
cPickle. dump(b, file)
cPickle. dump(c, file)
file. close()
It doesn't matter what was stored in a, b, and
c. It might be as simple as numbers, or as complex
as a list of dictionaries containing instances of user-defined
classes. cPickle.dump will save everything.
Now, to read that data back in on a later run of the program,
just say
import cPickle
file = open("state", 'r')
a = cPickle.load(file)
b = cPickle.load(file)
c = cPickle.load(file)
file. close()
Retrieve an arbitrary Python object with
cPickle.load.
Any data that was previously in the variables a, b,
or c will have been restored to them by
cPickle.load.
cPickle can handle just about any Python
object.
|
|
The cPickle module can store almost anything in this
manner. The cPickle module can handle lists, tuples,
numbers, strings, dictionaries, and just about anything made up
of these types of objects, which includes all class instances. It
also handles shared objects, cyclic references, and other complex
memory structures correctly, storing shared objects only once,
and restoring them as shared objects, not as identical copies.
However, code objects (what Python stores byte compiled code in)
and system resources like files or sockets can not be pickled.
A convenient way of using cPickle is to save your
state variables into a dictionary, and then cPickle
the dictionary.
|
|
More often than not, you won't want to save your entire program
state with cPickle. For example, most applications can have
multiple documents open at one time. If you saved the entire
state of the program, you would effectively save all open
documents in one file. An easy and effective way of saving and
restoring only data of interest is to write a save function which
stores all data you wish to save into a dictionary, and then uses
cPickle to save the dictionary. Then, a
complementary restore function can be used to read the dictionary
back in (again using cPickle), and to assign the
values in the dictionary to the appropriate program variables.
This also has the advantage that there is no possibility of
reading values back in an incorrect order, that is, an order
different from the order in which they were stored. Using this
approach with the above example, we would get code looking
something like this:
import cPickle
.
.
.
def saveData():
global a, b, c
file = open(" state", 'w')
data = {'a' : a, 'b' : b, 'c' : c}
cPickle.dump( data, file)
file.close()
def restoreData():
global a, b, c
file = open(" state", 'r')
data = cPickle.load( file)
file.close()
a = data[ 'a']
b = data[ 'b']
c = data[ 'c']
.
.
Now this is a somewhat contrived example. You probably won't be
saving the state of the top-level variables of your interactive
mode very often.
Output and Redirection - Page 12
The Quick Python Book
Pickling the Cache - Page 14
|