Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9b28fe2

Browse files
committed
Logical markup.
1 parent e14dde2 commit 9b28fe2

2 files changed

Lines changed: 132 additions & 122 deletions

File tree

Doc/lib/libpickle.tex

Lines changed: 66 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,33 @@ \section{Standard Module \sectcode{pickle}}
88
\indexii{flattening}{objects}
99
\indexii{pickling}{objects}
1010

11-
\setindexsubitem{(in module pickle)}
1211

13-
The \code{pickle} module implements a basic but powerful algorithm for
12+
The \module{pickle} module implements a basic but powerful algorithm for
1413
``pickling'' (a.k.a.\ serializing, marshalling or flattening) nearly
1514
arbitrary Python objects. This is the act of converting objects to a
1615
stream of bytes (and back: ``unpickling'').
1716
This is a more primitive notion than
18-
persistency --- although \code{pickle} reads and writes file objects,
17+
persistency --- although \module{pickle} reads and writes file objects,
1918
it does not handle the issue of naming persistent objects, nor the
2019
(even more complicated) area of concurrent access to persistent
21-
objects. The \code{pickle} module can transform a complex object into
20+
objects. The \module{pickle} module can transform a complex object into
2221
a byte stream and it can transform the byte stream into an object with
2322
the same internal structure. The most obvious thing to do with these
2423
byte streams is to write them onto a file, but it is also conceivable
2524
to send them across a network or store them in a database. The module
26-
\code{shelve} provides a simple interface to pickle and unpickle
25+
\module{shelve} provides a simple interface to pickle and unpickle
2726
objects on ``dbm''-style database files.
2827
\refstmodindex{shelve}
2928

30-
\strong{Note:} The \code{pickle} module is rather slow. A
31-
reimplementation of the same algorithm in C, which is up to 1000 times
32-
faster, is available as the \code{cPickle}\refbimodindex{cPickle}
29+
\strong{Note:} The \module{pickle} module is rather slow. A
30+
reimplementation of the same algorithm in \C{}, which is up to 1000 times
31+
faster, is available as the \module{cPickle}\refbimodindex{cPickle}
3332
module. This has the same interface except that \code{Pickler} and
3433
\code{Unpickler} are factory functions, not classes (so they cannot be
35-
used as a base class for inheritance).
34+
used as base classes for inheritance).
3635

37-
Unlike the built-in module \code{marshal}, \code{pickle} handles the
38-
following correctly:
36+
Unlike the built-in module \module{marshal}, \module{pickle} handles
37+
the following correctly:
3938
\refbimodindex{marshal}
4039

4140
\begin{itemize}
@@ -48,7 +47,7 @@ \section{Standard Module \sectcode{pickle}}
4847

4948
\end{itemize}
5049

51-
The data format used by \code{pickle} is Python-specific. This has
50+
The data format used by \module{pickle} is Python-specific. This has
5251
the advantage that there are no restrictions imposed by external
5352
standards such as XDR%
5453
\index{XDR}
@@ -57,36 +56,36 @@ \section{Standard Module \sectcode{pickle}}
5756
it means that non-Python programs may not be able to reconstruct
5857
pickled Python objects.
5958

60-
By default, the \code{pickle} data format uses a printable \ASCII{}
59+
By default, the \module{pickle} data format uses a printable \ASCII{}
6160
representation. This is slightly more voluminous than a binary
6261
representation. The big advantage of using printable \ASCII{} (and of
63-
some other characteristics of \code{pickle}'s representation) is that
62+
some other characteristics of \module{pickle}'s representation) is that
6463
for debugging or recovery purposes it is possible for a human to read
6564
the pickled file with a standard text editor.
6665

6766
A binary format, which is slightly more efficient, can be chosen by
6867
specifying a nonzero (true) value for the \var{bin} argument to the
69-
\code{Pickler} constructor or the \code{dump()} and \code{dumps()}
68+
\class{Pickler} constructor or the \function{dump()} and \function{dumps()}
7069
functions. The binary format is not the default because of backwards
7170
compatibility with the Python 1.4 pickle module. In a future version,
7271
the default may change to binary.
7372

74-
The \code{pickle} module doesn't handle code objects, which the
75-
\code{marshal} module does. I suppose \code{pickle} could, and maybe
73+
The \module{pickle} module doesn't handle code objects, which the
74+
\module{marshal} module does. I suppose \module{pickle} could, and maybe
7675
it should, but there's probably no great need for it right now (as
77-
long as \code{marshal} continues to be used for reading and writing
76+
long as \module{marshal} continues to be used for reading and writing
7877
code objects), and at least this avoids the possibility of smuggling
7978
Trojan horses into a program.
8079
\refbimodindex{marshal}
8180

82-
For the benefit of persistency modules written using \code{pickle}, it
81+
For the benefit of persistency modules written using \module{pickle}, it
8382
supports the notion of a reference to an object outside the pickled
8483
data stream. Such objects are referenced by a name, which is an
8584
arbitrary string of printable \ASCII{} characters. The resolution of
86-
such names is not defined by the \code{pickle} module --- the
85+
such names is not defined by the \module{pickle} module --- the
8786
persistent object module will have to implement a method
88-
\code{persistent_load()}. To write references to persistent objects,
89-
the persistent module must define a method \code{persistent_id()} which
87+
\method{persistent_load()}. To write references to persistent objects,
88+
the persistent module must define a method \method{persistent_id()} which
9089
returns either \code{None} or the persistent ID of the object.
9190

9291
There are some restrictions on the pickling of class instances.
@@ -96,38 +95,38 @@ \section{Standard Module \sectcode{pickle}}
9695

9796
\setindexsubitem{(pickle protocol)}
9897

99-
When a pickled class instance is unpickled, its \code{__init__()} method
98+
When a pickled class instance is unpickled, its \method{__init__()} method
10099
is normally \emph{not} invoked. \strong{Note:} This is a deviation
101100
from previous versions of this module; the change was introduced in
102101
Python 1.5b2. The reason for the change is that in many cases it is
103102
desirable to have a constructor that requires arguments; it is a
104-
(minor) nuisance to have to provide a \code{__getinitargs__()} method.
103+
(minor) nuisance to have to provide a \method{__getinitargs__()} method.
105104

106-
If it is desirable that the \code{__init__()} method be called on
107-
unpickling, a class can define a method \code{__getinitargs__()},
105+
If it is desirable that the \method{__init__()} method be called on
106+
unpickling, a class can define a method \method{__getinitargs__()},
108107
which should return a \emph{tuple} containing the arguments to be
109-
passed to the class constructor (\code{__init__()}). This method is
108+
passed to the class constructor (\method{__init__()}). This method is
110109
called at pickle time; the tuple it returns is incorporated in the
111110
pickle for the instance.
112-
\ttindex{__getinitargs__}
113-
\ttindex{__init__}
111+
\ttindex{__getinitargs__()}
112+
\ttindex{__init__()}
114113

115114
Classes can further influence how their instances are pickled --- if the class
116-
defines the method \code{__getstate__()}, it is called and the return
115+
defines the method \method{__getstate__()}, it is called and the return
117116
state is pickled as the contents for the instance, and if the class
118-
defines the method \code{__setstate__()}, it is called with the
117+
defines the method \method{__setstate__()}, it is called with the
119118
unpickled state. (Note that these methods can also be used to
120119
implement copying class instances.) If there is no
121-
\code{__getstate__()} method, the instance's \code{__dict__} is
122-
pickled. If there is no \code{__setstate__()} method, the pickled
120+
\method{__getstate__()} method, the instance's \member{__dict__} is
121+
pickled. If there is no \method{__setstate__()} method, the pickled
123122
object must be a dictionary and its items are assigned to the new
124-
instance's dictionary. (If a class defines both \code{__getstate__()}
125-
and \code{__setstate__()}, the state object needn't be a dictionary
123+
instance's dictionary. (If a class defines both \method{__getstate__()}
124+
and \method{__setstate__()}, the state object needn't be a dictionary
126125
--- these methods can do what they want.) This protocol is also used
127-
by the shallow and deep copying operations defined in the \code{copy}
128-
module.
129-
\ttindex{__getstate__}
130-
\ttindex{__setstate__}
126+
by the shallow and deep copying operations defined in the \module{copy}
127+
module.\refstmodindex{copy}
128+
\ttindex{__getstate__()}
129+
\ttindex{__setstate__()}
131130
\ttindex{__dict__}
132131

133132
Note that when class instances are pickled, their class's code and
@@ -137,7 +136,7 @@ \section{Standard Module \sectcode{pickle}}
137136
version of the class. If you plan to have long-lived objects that
138137
will see many versions of a class, it may be worthwhile to put a version
139138
number in the objects so that suitable conversions can be made by the
140-
class's \code{__setstate__()} method.
139+
class's \method{__setstate__()} method.
141140

142141
When a class itself is pickled, only its name is pickled --- the class
143142
definition is not pickled, but re-imported by the unpickling process.
@@ -154,39 +153,39 @@ \section{Standard Module \sectcode{pickle}}
154153
p = pickle.Pickler(f)
155154
p.dump(x)
156155
\end{verbatim}
157-
%
156+
158157
A shorthand for this is:
159158

160159
\begin{verbatim}
161160
pickle.dump(x, f)
162161
\end{verbatim}
163-
%
162+
164163
To unpickle an object \code{x} from a file \code{f}, open for reading:
165164

166165
\begin{verbatim}
167166
u = pickle.Unpickler(f)
168167
x = u.load()
169168
\end{verbatim}
170-
%
169+
171170
A shorthand is:
172171

173172
\begin{verbatim}
174173
x = pickle.load(f)
175174
\end{verbatim}
176-
%
177-
The \code{Pickler} class only calls the method \code{f.write()} with a
178-
string argument. The \code{Unpickler} calls the methods \code{f.read()}
175+
176+
The \class{Pickler} class only calls the method \code{f.write()} with a
177+
string argument. The \class{Unpickler} calls the methods \code{f.read()}
179178
(with an integer argument) and \code{f.readline()} (without argument),
180179
both returning a string. It is explicitly allowed to pass non-file
181180
objects here, as long as they have the right methods.
182181
\ttindex{Unpickler}
183182
\ttindex{Pickler}
184183

185-
The constructor for the \code{Pickler} class has an optional second
184+
The constructor for the \class{Pickler} class has an optional second
186185
argument, \var{bin}. If this is present and nonzero, the binary
187186
pickle format is used; if it is zero or absent, the (less efficient,
188187
but backwards compatible) text pickle format is used. The
189-
\code{Unpickler} class does not have an argument to distinguish
188+
\class{Unpickler} class does not have an argument to distinguish
190189
between binary and text pickle formats; it accepts either format.
191190

192191
The following types can be pickled:
@@ -202,45 +201,45 @@ \section{Standard Module \sectcode{pickle}}
202201

203202
\item classes that are defined at the top level in a module
204203

205-
\item instances of such classes whose \code{__dict__} or
206-
\code{__setstate__()} is picklable
204+
\item instances of such classes whose \member{__dict__} or
205+
\method{__setstate__()} is picklable
207206

208207
\end{itemize}
209208

210209
Attempts to pickle unpicklable objects will raise the
211-
\code{PicklingError} exception; when this happens, an unspecified
210+
\exception{PicklingError} exception; when this happens, an unspecified
212211
number of bytes may have been written to the file.
213212

214-
It is possible to make multiple calls to the \code{dump()} method of
215-
the same \code{Pickler} instance. These must then be matched to the
216-
same number of calls to the \code{load()} instance of the
217-
corresponding \code{Unpickler} instance. If the same object is
218-
pickled by multiple \code{dump()} calls, the \code{load()} will all
213+
It is possible to make multiple calls to the \method{dump()} method of
214+
the same \class{Pickler} instance. These must then be matched to the
215+
same number of calls to the \method{load()} method of the
216+
corresponding \class{Unpickler} instance. If the same object is
217+
pickled by multiple \method{dump()} calls, the \method{load()} will all
219218
yield references to the same object. \emph{Warning}: this is intended
220219
for pickling multiple objects without intervening modifications to the
221220
objects or their parts. If you modify an object and then pickle it
222-
again using the same \code{Pickler} instance, the object is not
221+
again using the same \class{Pickler} instance, the object is not
223222
pickled again --- a reference to it is pickled and the
224-
\code{Unpickler} will return the old value, not the modified one.
223+
\class{Unpickler} will return the old value, not the modified one.
225224
(There are two problems here: (a) detecting changes, and (b)
226225
marshalling a minimal set of changes. I have no answers. Garbage
227226
Collection may also become a problem here.)
228227

229-
Apart from the \code{Pickler} and \code{Unpickler} classes, the
228+
Apart from the \class{Pickler} and \class{Unpickler} classes, the
230229
module defines the following functions, and an exception:
231230

232231
\begin{funcdesc}{dump}{object, file\optional{, bin}}
233232
Write a pickled representation of \var{obect} to the open file object
234233
\var{file}. This is equivalent to
235-
\code{Pickler(\var{file}, \var{bin}).dump(\var{object})}.
234+
\samp{Pickler(\var{file}, \var{bin}).dump(\var{object})}.
236235
If the optional \var{bin} argument is present and nonzero, the binary
237236
pickle format is used; if it is zero or absent, the (less efficient)
238237
text pickle format is used.
239238
\end{funcdesc}
240239

241240
\begin{funcdesc}{load}{file}
242241
Read a pickled object from the open file object \var{file}. This is
243-
equivalent to \code{Unpickler(\var{file}).load()}.
242+
equivalent to \samp{Unpickler(\var{file}).load()}.
244243
\end{funcdesc}
245244

246245
\begin{funcdesc}{dumps}{object\optional{, bin}}
@@ -262,6 +261,12 @@ \section{Standard Module \sectcode{pickle}}
262261

263262

264263
\begin{seealso}
264+
\seemodule{copy}{shallow and deep object copying}
265+
265266
\seemodule[copyreg]{copy_reg}{pickle interface constructor
266267
registration}
268+
269+
\seemodule{marshal}{high-performance serialization of built-in types}
270+
271+
\seemodule{shelve}{indexed databases of objects; uses \module{pickle}}
267272
\end{seealso}

0 commit comments

Comments
 (0)