diff --git a/serializing.html b/serializing.html index 049fb77..602a0cc 100644 --- a/serializing.html +++ b/serializing.html @@ -38,7 +38,20 @@ body{counter-reset:h1 13}

Serializing Simple Python Objects

-

FIXME - introduction to pickle module, concepts, what datatypes can be pickled w/o additional work +

The concept of serialization is simple. You have a data structure in memory that you want to save, reuse, or send to someone else. How would you do that? Well, that depends on how you want to save it, how you want to reuse it, and to whom you want to send it. Many games allow you to save your progress when you quit the game and pick up where you left off when you relaunch the game. (Actually, many non-gaming applications do this as well.) In this case, a data structure that captures “your progress so far” needs to be stored on disk when you quit, then loaded from disk when you relaunch. The data is only meant to be used by the same program that created it, never sent over a network, and never read by anything other than the program that created it. Therefore, the interoperability issues are limited to ensuring that later versions of the program can read data written by earlier versions. + +

For cases like this, the pickle module is ideal. It’s part of the Python standard library, so it’s always available. It’s fast; the bulk of it is written in C, like the Python interpreter itself. It can store arbitrarily complex Python data structures. + +

What can the pickle module store? + +

+ +

If this isn’t enough for you, the pickle module is also extensible, as you’ll see later in this chapter.

Saving to a File

@@ -76,19 +89,19 @@ body{counter-reset:h1 13}
  1. This is still in Python Shell #1.
  2. Use the open() function to open a file. Set the file mode to 'wb' to open the file for writing in binary mode. Wrap it in a with statement to ensure the file is closed automatically when you’re done with it. -
  3. The dump() function in the pickle module takes a serializable Python data structure, serializes it into a binary, Python-specific format using the latest version of the pickle protocol, and saves it to an open file. +
  4. The dump() function in the pickle module takes a serializable Python data structure, serializes it into a binary, Python-specific format using the latest version of the pickle protocol, and saves it to an open file.

That last sentence was pretty important.

Loading from a File

@@ -167,6 +180,7 @@ NameError: name 'entry' is not defined

Bytes and Strings Rear Their Ugly Heads (Again!)

+

The pickle protocol has been around for many years, and it has matured as Python itself has matured.

FIXME - discussion of pickle protocol versions, backward incompatibility of protocol version 3 due to bytes/strings separation in Python 3, link to http://docs.python.org/3.1/library/pickle.html#data-stream-format

Debugging Pickle Files