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}
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?
+
+
byte objects, byte arrays, and None.
+If this isn’t enough for you, the pickle module is also extensible, as you’ll see later in this chapter.
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.
-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.
+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.
pickle module takes a Python data structure and saves it to a file.
-pickle protocol.”
-pickle protocol is Python-specific; there is no guarantee of cross-language compatibility. You probably couldn’t take the entry.pickle file you just created and do anything useful with it in Perl, PHP, Java, or any other language.
-pickle module. The pickle protocol has changed several times as new data types have been added to the Python language, but there are still limitations.
+entry.pickle file you just created and do anything useful with it in Perl, PHP, Java, or any other language.
+pickle module. The pickle protocol has changed several times as new data types have been added to the Python language, but there are still limitations.
pickle module will use the latest version of the pickle protocol. This ensures that you have maximum flexibility in the types of data you can serialize, but it also means that the resulting file will not be readable by older versions of Python that do not support the latest version of the pickle protocol.
-pickle protocol is a binary protocol. Be sure to open your pickle files in binary mode, or the data will get corrupted during writing.
+pickle module will use the latest version of the pickle protocol. This ensures that you have maximum flexibility in the types of data you can serialize, but it also means that the resulting file will not be readable by older versions of Python that do not support the latest version of the pickle protocol.
+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