[Python-checkins] r50903 - python/trunk/Doc/lib/libpickle.tex

andrew.kuchling python-checkins at python.org
Fri Jul 28 14:33:19 CEST 2006


Author: andrew.kuchling
Date: Fri Jul 28 14:33:19 2006
New Revision: 50903

Modified:
   python/trunk/Doc/lib/libpickle.tex
Log:
Add example

Modified: python/trunk/Doc/lib/libpickle.tex
==============================================================================
--- python/trunk/Doc/lib/libpickle.tex	(original)
+++ python/trunk/Doc/lib/libpickle.tex	Fri Jul 28 14:33:19 2006
@@ -725,7 +725,50 @@
 
 \subsection{Example \label{pickle-example}}
 
-Here's a simple example of how to modify pickling behavior for a
+For the simplest code, use the \function{dump()} and \function{load()}
+functions.  Note that a self-referencing list is pickled and restored
+correctly.
+
+\begin{verbatim}
+import pickle
+
+data1 = {'a': [1, 2.0, 3, 4+6j],
+         'b': ('string', u'Unicode string'),
+         'c': None}
+
+selfref_list = [1, 2, 3]
+selfref_list.append(selfref_list)
+
+output = open('data.pkl', 'wb')
+
+# Pickle dictionary using protocol 0.
+pickle.dump(data1, output)
+
+# Pickle the list using the highest protocol available.
+pickle.dump(selfref_list, output, -1)
+
+output.close()
+\end{verbatim}
+
+The following example reads the resulting pickled data.  When reading
+a pickle-containing file, you should open the file in binary mode
+because you can't be sure if the ASCII or binary format was used.
+
+\begin{verbatim}
+import pprint, pickle
+
+pkl_file = open('data.pkl', 'rb')
+
+data1 = pickle.load(pkl_file)
+pprint.pprint(data1)
+
+data2 = pickle.load(pkl_file)
+pprint.pprint(data2)
+
+pkl_file.close()
+\end{verbatim}
+
+Here's a larger example that shows how to modify pickling behavior for a
 class.  The \class{TextReader} class opens a text file, and returns
 the line number and line contents each time its \method{readline()}
 method is called. If a \class{TextReader} instance is pickled, all


More information about the Python-checkins mailing list