1998-08-10 19:42:37 +00:00
|
|
|
\section{\module{pickle} ---
|
1999-04-22 21:23:22 +00:00
|
|
|
Python object serialization}
|
1998-07-23 17:59:49 +00:00
|
|
|
|
1999-04-22 21:23:22 +00:00
|
|
|
\declaremodule{standard}{pickle}
|
1998-07-23 17:59:49 +00:00
|
|
|
\modulesynopsis{Convert Python objects to streams of bytes and back.}
|
2000-04-03 20:13:55 +00:00
|
|
|
% Substantial improvements by Jim Kerr <jbkerr@sr.hp.com>.
|
1998-07-23 17:59:49 +00:00
|
|
|
|
2000-07-16 19:01:10 +00:00
|
|
|
\index{persistence}
|
1995-02-15 15:53:08 +00:00
|
|
|
\indexii{persistent}{objects}
|
|
|
|
\indexii{serializing}{objects}
|
|
|
|
\indexii{marshalling}{objects}
|
|
|
|
\indexii{flattening}{objects}
|
|
|
|
\indexii{pickling}{objects}
|
|
|
|
|
1995-03-17 16:07:09 +00:00
|
|
|
|
1999-07-02 14:25:37 +00:00
|
|
|
The \module{pickle} module implements a basic but powerful algorithm
|
|
|
|
for ``pickling'' (a.k.a.\ serializing, marshalling or flattening)
|
|
|
|
nearly arbitrary Python objects. This is the act of converting
|
|
|
|
objects to a stream of bytes (and back: ``unpickling''). This is a
|
2000-07-16 19:01:10 +00:00
|
|
|
more primitive notion than persistence --- although \module{pickle}
|
1999-07-02 14:25:37 +00:00
|
|
|
reads and writes file objects, it does not handle the issue of naming
|
|
|
|
persistent objects, nor the (even more complicated) area of concurrent
|
|
|
|
access to persistent objects. The \module{pickle} module can
|
|
|
|
transform a complex object into a byte stream and it can transform the
|
|
|
|
byte stream into an object with the same internal structure. The most
|
|
|
|
obvious thing to do with these byte streams is to write them onto a
|
|
|
|
file, but it is also conceivable to send them across a network or
|
|
|
|
store them in a database. The module
|
1999-04-22 21:23:22 +00:00
|
|
|
\refmodule{shelve}\refstmodindex{shelve} provides a simple interface
|
|
|
|
to pickle and unpickle objects on DBM-style database files.
|
|
|
|
|
1995-02-15 15:53:08 +00:00
|
|
|
|
1998-04-04 06:20:28 +00:00
|
|
|
\strong{Note:} The \module{pickle} module is rather slow. A
|
1999-04-22 21:23:22 +00:00
|
|
|
reimplementation of the same algorithm in C, which is up to 1000 times
|
1999-07-02 14:25:37 +00:00
|
|
|
faster, is available as the
|
|
|
|
\refmodule{cPickle}\refbimodindex{cPickle} module. This has the same
|
|
|
|
interface except that \class{Pickler} and \class{Unpickler} are
|
|
|
|
factory functions, not classes (so they cannot be used as base classes
|
|
|
|
for inheritance).
|
1997-12-09 20:45:08 +00:00
|
|
|
|
2000-04-03 20:13:55 +00:00
|
|
|
Although the \module{pickle} module can use the built-in module
|
|
|
|
\refmodule{marshal}\refbimodindex{marshal} internally, it differs from
|
|
|
|
\refmodule{marshal} in the way it handles certain kinds of data:
|
1995-02-15 15:53:08 +00:00
|
|
|
|
|
|
|
\begin{itemize}
|
|
|
|
|
2000-04-03 20:13:55 +00:00
|
|
|
\item Recursive objects (objects containing references to themselves):
|
|
|
|
\module{pickle} keeps track of the objects it has already
|
|
|
|
serialized, so later references to the same object won't be
|
|
|
|
serialized again. (The \refmodule{marshal} module breaks for
|
|
|
|
this.)
|
1995-02-15 15:53:08 +00:00
|
|
|
|
2000-04-03 20:13:55 +00:00
|
|
|
\item Object sharing (references to the same object in different
|
|
|
|
places): This is similar to self-referencing objects;
|
|
|
|
\module{pickle} stores the object once, and ensures that all
|
|
|
|
other references point to the master copy. Shared objects
|
|
|
|
remain shared, which can be very important for mutable objects.
|
1995-02-15 15:53:08 +00:00
|
|
|
|
2000-04-03 20:13:55 +00:00
|
|
|
\item User-defined classes and their instances: \refmodule{marshal}
|
|
|
|
does not support these at all, but \module{pickle} can save
|
|
|
|
and restore class instances transparently. The class definition
|
|
|
|
must be importable and live in the same module as when the
|
|
|
|
object was stored.
|
1995-02-15 15:53:08 +00:00
|
|
|
|
|
|
|
\end{itemize}
|
|
|
|
|
1998-04-04 06:20:28 +00:00
|
|
|
The data format used by \module{pickle} is Python-specific. This has
|
1995-02-15 15:53:08 +00:00
|
|
|
the advantage that there are no restrictions imposed by external
|
1999-04-22 21:23:22 +00:00
|
|
|
standards such as
|
|
|
|
XDR\index{XDR}\index{External Data Representation} (which can't
|
|
|
|
represent pointer sharing); however it means that non-Python programs
|
|
|
|
may not be able to reconstruct pickled Python objects.
|
1995-02-15 15:53:08 +00:00
|
|
|
|
1998-04-04 06:20:28 +00:00
|
|
|
By default, the \module{pickle} data format uses a printable \ASCII{}
|
1997-12-09 20:45:08 +00:00
|
|
|
representation. This is slightly more voluminous than a binary
|
|
|
|
representation. The big advantage of using printable \ASCII{} (and of
|
1998-04-04 06:20:28 +00:00
|
|
|
some other characteristics of \module{pickle}'s representation) is that
|
1997-12-09 20:45:08 +00:00
|
|
|
for debugging or recovery purposes it is possible for a human to read
|
|
|
|
the pickled file with a standard text editor.
|
|
|
|
|
|
|
|
A binary format, which is slightly more efficient, can be chosen by
|
|
|
|
specifying a nonzero (true) value for the \var{bin} argument to the
|
1998-04-04 06:20:28 +00:00
|
|
|
\class{Pickler} constructor or the \function{dump()} and \function{dumps()}
|
1997-12-09 20:45:08 +00:00
|
|
|
functions. The binary format is not the default because of backwards
|
|
|
|
compatibility with the Python 1.4 pickle module. In a future version,
|
|
|
|
the default may change to binary.
|
1995-02-15 15:53:08 +00:00
|
|
|
|
1998-04-04 06:20:28 +00:00
|
|
|
The \module{pickle} module doesn't handle code objects, which the
|
1999-07-02 14:25:37 +00:00
|
|
|
\refmodule{marshal}\refbimodindex{marshal} module does. I suppose
|
|
|
|
\module{pickle} could, and maybe it should, but there's probably no
|
|
|
|
great need for it right now (as long as \refmodule{marshal} continues
|
|
|
|
to be used for reading and writing code objects), and at least this
|
|
|
|
avoids the possibility of smuggling Trojan horses into a program.
|
1995-02-15 15:53:08 +00:00
|
|
|
|
2000-07-16 19:01:10 +00:00
|
|
|
For the benefit of persistence modules written using \module{pickle}, it
|
1995-02-15 15:53:08 +00:00
|
|
|
supports the notion of a reference to an object outside the pickled
|
|
|
|
data stream. Such objects are referenced by a name, which is an
|
1995-03-17 16:07:09 +00:00
|
|
|
arbitrary string of printable \ASCII{} characters. The resolution of
|
1998-04-04 06:20:28 +00:00
|
|
|
such names is not defined by the \module{pickle} module --- the
|
1995-02-15 15:53:08 +00:00
|
|
|
persistent object module will have to implement a method
|
1998-04-04 06:20:28 +00:00
|
|
|
\method{persistent_load()}. To write references to persistent objects,
|
|
|
|
the persistent module must define a method \method{persistent_id()} which
|
1995-02-15 15:53:08 +00:00
|
|
|
returns either \code{None} or the persistent ID of the object.
|
|
|
|
|
|
|
|
There are some restrictions on the pickling of class instances.
|
|
|
|
|
|
|
|
First of all, the class must be defined at the top level in a module.
|
1997-12-09 20:45:08 +00:00
|
|
|
Furthermore, all its instance variables must be picklable.
|
1995-02-15 15:53:08 +00:00
|
|
|
|
1998-02-13 06:58:54 +00:00
|
|
|
\setindexsubitem{(pickle protocol)}
|
1995-03-17 16:07:09 +00:00
|
|
|
|
1998-04-04 06:20:28 +00:00
|
|
|
When a pickled class instance is unpickled, its \method{__init__()} method
|
1997-12-09 20:45:08 +00:00
|
|
|
is normally \emph{not} invoked. \strong{Note:} This is a deviation
|
|
|
|
from previous versions of this module; the change was introduced in
|
|
|
|
Python 1.5b2. The reason for the change is that in many cases it is
|
|
|
|
desirable to have a constructor that requires arguments; it is a
|
1998-04-04 06:20:28 +00:00
|
|
|
(minor) nuisance to have to provide a \method{__getinitargs__()} method.
|
1997-12-09 20:45:08 +00:00
|
|
|
|
1998-04-04 06:20:28 +00:00
|
|
|
If it is desirable that the \method{__init__()} method be called on
|
|
|
|
unpickling, a class can define a method \method{__getinitargs__()},
|
1998-01-09 22:36:51 +00:00
|
|
|
which should return a \emph{tuple} containing the arguments to be
|
1998-04-04 06:20:28 +00:00
|
|
|
passed to the class constructor (\method{__init__()}). This method is
|
1997-12-30 17:44:48 +00:00
|
|
|
called at pickle time; the tuple it returns is incorporated in the
|
|
|
|
pickle for the instance.
|
1999-07-02 14:25:37 +00:00
|
|
|
\withsubitem{(copy protocol)}{\ttindex{__getinitargs__()}}
|
|
|
|
\withsubitem{(instance constructor)}{\ttindex{__init__()}}
|
|
|
|
|
|
|
|
Classes can further influence how their instances are pickled --- if
|
|
|
|
the class
|
|
|
|
\withsubitem{(copy protocol)}{
|
|
|
|
\ttindex{__getstate__()}\ttindex{__setstate__()}}
|
|
|
|
\withsubitem{(instance attribute)}{
|
|
|
|
\ttindex{__dict__}}
|
1998-04-04 06:20:28 +00:00
|
|
|
defines the method \method{__getstate__()}, it is called and the return
|
1995-02-15 15:53:08 +00:00
|
|
|
state is pickled as the contents for the instance, and if the class
|
1998-04-04 06:20:28 +00:00
|
|
|
defines the method \method{__setstate__()}, it is called with the
|
1995-02-15 15:53:08 +00:00
|
|
|
unpickled state. (Note that these methods can also be used to
|
|
|
|
implement copying class instances.) If there is no
|
1998-04-04 06:20:28 +00:00
|
|
|
\method{__getstate__()} method, the instance's \member{__dict__} is
|
|
|
|
pickled. If there is no \method{__setstate__()} method, the pickled
|
1995-02-15 15:53:08 +00:00
|
|
|
object must be a dictionary and its items are assigned to the new
|
1998-04-04 06:20:28 +00:00
|
|
|
instance's dictionary. (If a class defines both \method{__getstate__()}
|
|
|
|
and \method{__setstate__()}, the state object needn't be a dictionary
|
1995-02-15 15:53:08 +00:00
|
|
|
--- these methods can do what they want.) This protocol is also used
|
1999-04-22 21:23:22 +00:00
|
|
|
by the shallow and deep copying operations defined in the
|
|
|
|
\refmodule{copy}\refstmodindex{copy} module.
|
1995-02-15 15:53:08 +00:00
|
|
|
|
|
|
|
Note that when class instances are pickled, their class's code and
|
1995-03-13 10:03:32 +00:00
|
|
|
data are not pickled along with them. Only the instance data are
|
1995-02-15 15:53:08 +00:00
|
|
|
pickled. This is done on purpose, so you can fix bugs in a class or
|
|
|
|
add methods and still load objects that were created with an earlier
|
|
|
|
version of the class. If you plan to have long-lived objects that
|
1995-03-13 10:03:32 +00:00
|
|
|
will see many versions of a class, it may be worthwhile to put a version
|
1995-02-15 15:53:08 +00:00
|
|
|
number in the objects so that suitable conversions can be made by the
|
1998-04-04 06:20:28 +00:00
|
|
|
class's \method{__setstate__()} method.
|
1995-02-15 15:53:08 +00:00
|
|
|
|
1995-03-17 16:07:09 +00:00
|
|
|
When a class itself is pickled, only its name is pickled --- the class
|
|
|
|
definition is not pickled, but re-imported by the unpickling process.
|
|
|
|
Therefore, the restriction that the class must be defined at the top
|
|
|
|
level in a module applies to pickled classes as well.
|
|
|
|
|
1998-02-13 06:58:54 +00:00
|
|
|
\setindexsubitem{(in module pickle)}
|
1995-03-17 16:07:09 +00:00
|
|
|
|
1995-02-15 15:53:08 +00:00
|
|
|
The interface can be summarized as follows.
|
|
|
|
|
|
|
|
To pickle an object \code{x} onto a file \code{f}, open for writing:
|
|
|
|
|
1998-02-13 06:58:54 +00:00
|
|
|
\begin{verbatim}
|
1995-02-15 15:53:08 +00:00
|
|
|
p = pickle.Pickler(f)
|
|
|
|
p.dump(x)
|
1998-02-13 06:58:54 +00:00
|
|
|
\end{verbatim}
|
1998-04-04 06:20:28 +00:00
|
|
|
|
1995-03-17 16:07:09 +00:00
|
|
|
A shorthand for this is:
|
|
|
|
|
1998-02-13 06:58:54 +00:00
|
|
|
\begin{verbatim}
|
1995-03-17 16:07:09 +00:00
|
|
|
pickle.dump(x, f)
|
1998-02-13 06:58:54 +00:00
|
|
|
\end{verbatim}
|
1998-04-04 06:20:28 +00:00
|
|
|
|
1995-02-15 15:53:08 +00:00
|
|
|
To unpickle an object \code{x} from a file \code{f}, open for reading:
|
|
|
|
|
1998-02-13 06:58:54 +00:00
|
|
|
\begin{verbatim}
|
1995-02-15 15:53:08 +00:00
|
|
|
u = pickle.Unpickler(f)
|
1995-04-10 11:34:00 +00:00
|
|
|
x = u.load()
|
1998-02-13 06:58:54 +00:00
|
|
|
\end{verbatim}
|
1998-04-04 06:20:28 +00:00
|
|
|
|
1995-03-17 16:07:09 +00:00
|
|
|
A shorthand is:
|
|
|
|
|
1998-02-13 06:58:54 +00:00
|
|
|
\begin{verbatim}
|
1995-03-17 16:07:09 +00:00
|
|
|
x = pickle.load(f)
|
1998-02-13 06:58:54 +00:00
|
|
|
\end{verbatim}
|
1998-04-04 06:20:28 +00:00
|
|
|
|
|
|
|
The \class{Pickler} class only calls the method \code{f.write()} with a
|
2000-04-03 20:13:55 +00:00
|
|
|
\withsubitem{(class in pickle)}{\ttindex{Unpickler}\ttindex{Pickler}}
|
1998-04-04 06:20:28 +00:00
|
|
|
string argument. The \class{Unpickler} calls the methods \code{f.read()}
|
1998-01-09 22:36:51 +00:00
|
|
|
(with an integer argument) and \code{f.readline()} (without argument),
|
1995-02-15 15:53:08 +00:00
|
|
|
both returning a string. It is explicitly allowed to pass non-file
|
|
|
|
objects here, as long as they have the right methods.
|
|
|
|
|
1998-04-04 06:20:28 +00:00
|
|
|
The constructor for the \class{Pickler} class has an optional second
|
2000-04-03 20:13:55 +00:00
|
|
|
argument, \var{bin}. If this is present and true, the binary
|
|
|
|
pickle format is used; if it is absent or false, the (less efficient,
|
1997-12-09 20:45:08 +00:00
|
|
|
but backwards compatible) text pickle format is used. The
|
1998-04-04 06:20:28 +00:00
|
|
|
\class{Unpickler} class does not have an argument to distinguish
|
1997-12-09 20:45:08 +00:00
|
|
|
between binary and text pickle formats; it accepts either format.
|
|
|
|
|
1995-02-15 15:53:08 +00:00
|
|
|
The following types can be pickled:
|
1999-07-02 14:25:37 +00:00
|
|
|
|
1995-02-15 15:53:08 +00:00
|
|
|
\begin{itemize}
|
|
|
|
|
|
|
|
\item \code{None}
|
|
|
|
|
|
|
|
\item integers, long integers, floating point numbers
|
|
|
|
|
2000-04-06 15:04:30 +00:00
|
|
|
\item normal and Unicode strings
|
1995-02-15 15:53:08 +00:00
|
|
|
|
|
|
|
\item tuples, lists and dictionaries containing only picklable objects
|
|
|
|
|
2000-04-03 20:13:55 +00:00
|
|
|
\item functions defined at the top level of a module (by name
|
|
|
|
reference, not storage of the implementation)
|
|
|
|
|
|
|
|
\item built-in functions
|
|
|
|
|
1995-03-17 16:07:09 +00:00
|
|
|
\item classes that are defined at the top level in a module
|
|
|
|
|
1998-04-04 06:20:28 +00:00
|
|
|
\item instances of such classes whose \member{__dict__} or
|
|
|
|
\method{__setstate__()} is picklable
|
1995-02-15 15:53:08 +00:00
|
|
|
|
|
|
|
\end{itemize}
|
|
|
|
|
1995-03-17 16:07:09 +00:00
|
|
|
Attempts to pickle unpicklable objects will raise the
|
1998-04-04 06:20:28 +00:00
|
|
|
\exception{PicklingError} exception; when this happens, an unspecified
|
1995-03-17 16:07:09 +00:00
|
|
|
number of bytes may have been written to the file.
|
|
|
|
|
1998-04-04 06:20:28 +00:00
|
|
|
It is possible to make multiple calls to the \method{dump()} method of
|
|
|
|
the same \class{Pickler} instance. These must then be matched to the
|
|
|
|
same number of calls to the \method{load()} method of the
|
|
|
|
corresponding \class{Unpickler} instance. If the same object is
|
|
|
|
pickled by multiple \method{dump()} calls, the \method{load()} will all
|
1998-01-09 22:36:51 +00:00
|
|
|
yield references to the same object. \emph{Warning}: this is intended
|
1995-03-17 16:07:09 +00:00
|
|
|
for pickling multiple objects without intervening modifications to the
|
|
|
|
objects or their parts. If you modify an object and then pickle it
|
1998-04-04 06:20:28 +00:00
|
|
|
again using the same \class{Pickler} instance, the object is not
|
1995-03-17 16:07:09 +00:00
|
|
|
pickled again --- a reference to it is pickled and the
|
1998-04-04 06:20:28 +00:00
|
|
|
\class{Unpickler} will return the old value, not the modified one.
|
1995-03-17 16:07:09 +00:00
|
|
|
(There are two problems here: (a) detecting changes, and (b)
|
|
|
|
marshalling a minimal set of changes. I have no answers. Garbage
|
|
|
|
Collection may also become a problem here.)
|
|
|
|
|
1998-04-04 06:20:28 +00:00
|
|
|
Apart from the \class{Pickler} and \class{Unpickler} classes, the
|
1995-03-17 16:07:09 +00:00
|
|
|
module defines the following functions, and an exception:
|
|
|
|
|
1998-03-17 06:33:25 +00:00
|
|
|
\begin{funcdesc}{dump}{object, file\optional{, bin}}
|
2000-07-16 19:01:10 +00:00
|
|
|
Write a pickled representation of \var{object} to the open file object
|
1997-12-09 20:45:08 +00:00
|
|
|
\var{file}. This is equivalent to
|
1998-04-04 06:20:28 +00:00
|
|
|
\samp{Pickler(\var{file}, \var{bin}).dump(\var{object})}.
|
1997-12-09 20:45:08 +00:00
|
|
|
If the optional \var{bin} argument is present and nonzero, the binary
|
|
|
|
pickle format is used; if it is zero or absent, the (less efficient)
|
|
|
|
text pickle format is used.
|
1995-03-17 16:07:09 +00:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{load}{file}
|
|
|
|
Read a pickled object from the open file object \var{file}. This is
|
1998-04-04 06:20:28 +00:00
|
|
|
equivalent to \samp{Unpickler(\var{file}).load()}.
|
1995-03-17 16:07:09 +00:00
|
|
|
\end{funcdesc}
|
|
|
|
|
1997-12-09 20:45:08 +00:00
|
|
|
\begin{funcdesc}{dumps}{object\optional{, bin}}
|
1995-03-17 16:07:09 +00:00
|
|
|
Return the pickled representation of the object as a string, instead
|
1997-12-09 20:45:08 +00:00
|
|
|
of writing it to a file. If the optional \var{bin} argument is
|
|
|
|
present and nonzero, the binary pickle format is used; if it is zero
|
|
|
|
or absent, the (less efficient) text pickle format is used.
|
1995-03-17 16:07:09 +00:00
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{funcdesc}{loads}{string}
|
|
|
|
Read a pickled object from a string instead of a file. Characters in
|
|
|
|
the string past the pickled object's representation are ignored.
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
\begin{excdesc}{PicklingError}
|
|
|
|
This exception is raised when an unpicklable object is passed to
|
1999-07-02 14:25:37 +00:00
|
|
|
\method{Pickler.dump()}.
|
1995-03-17 16:07:09 +00:00
|
|
|
\end{excdesc}
|
1998-03-06 21:27:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
\begin{seealso}
|
1999-04-22 21:23:22 +00:00
|
|
|
\seemodule[copyreg]{copy_reg}{pickle interface constructor
|
|
|
|
registration}
|
1998-04-04 06:20:28 +00:00
|
|
|
|
1999-04-22 21:23:22 +00:00
|
|
|
\seemodule{shelve}{indexed databases of objects; uses \module{pickle}}
|
1998-04-11 20:43:51 +00:00
|
|
|
|
1999-04-22 21:23:22 +00:00
|
|
|
\seemodule{copy}{shallow and deep object copying}
|
1998-04-11 20:43:51 +00:00
|
|
|
|
1999-04-22 21:23:22 +00:00
|
|
|
\seemodule{marshal}{high-performance serialization of built-in types}
|
1998-03-06 21:27:14 +00:00
|
|
|
\end{seealso}
|
1998-04-11 20:05:43 +00:00
|
|
|
|
|
|
|
|
2000-04-03 20:13:55 +00:00
|
|
|
\subsection{Example \label{pickle-example}}
|
|
|
|
|
|
|
|
Here's a simple example of 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
|
|
|
|
attributes \emph{except} the file object member are saved. When the
|
|
|
|
instance is unpickled, the file is reopened, and reading resumes from
|
|
|
|
the last location. The \method{__setstate__()} and
|
|
|
|
\method{__getstate__()} methods are used to implement this behavior.
|
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
# illustrate __setstate__ and __getstate__ methods
|
|
|
|
# used in pickling.
|
|
|
|
|
|
|
|
class TextReader:
|
|
|
|
"Print and number lines in a text file."
|
|
|
|
def __init__(self,file):
|
|
|
|
self.file = file
|
|
|
|
self.fh = open(file,'r')
|
|
|
|
self.lineno = 0
|
|
|
|
|
|
|
|
def readline(self):
|
|
|
|
self.lineno = self.lineno + 1
|
|
|
|
line = self.fh.readline()
|
|
|
|
if not line:
|
|
|
|
return None
|
|
|
|
return "%d: %s" % (self.lineno,line[:-1])
|
|
|
|
|
|
|
|
# return data representation for pickled object
|
|
|
|
def __getstate__(self):
|
|
|
|
odict = self.__dict__ # get attribute dictionary
|
|
|
|
del odict['fh'] # remove filehandle entry
|
|
|
|
return odict
|
|
|
|
|
|
|
|
# restore object state from data representation generated
|
|
|
|
# by __getstate__
|
|
|
|
def __setstate__(self,dict):
|
|
|
|
fh = open(dict['file']) # reopen file
|
|
|
|
count = dict['lineno'] # read from file...
|
|
|
|
while count: # until line count is restored
|
|
|
|
fh.readline()
|
|
|
|
count = count - 1
|
|
|
|
dict['fh'] = fh # create filehandle entry
|
|
|
|
self.__dict__ = dict # make dict our attribute dictionary
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
A sample usage might be something like this:
|
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
>>> import TextReader
|
|
|
|
>>> obj = TextReader.TextReader("TextReader.py")
|
|
|
|
>>> obj.readline()
|
|
|
|
'1: #!/usr/local/bin/python'
|
|
|
|
>>> # (more invocations of obj.readline() here)
|
|
|
|
... obj.readline()
|
|
|
|
'7: class TextReader:'
|
|
|
|
>>> import pickle
|
|
|
|
>>> pickle.dump(obj,open('save.p','w'))
|
|
|
|
|
|
|
|
(start another Python session)
|
|
|
|
|
|
|
|
>>> import pickle
|
|
|
|
>>> reader = pickle.load(open('save.p'))
|
|
|
|
>>> reader.readline()
|
|
|
|
'8: "Print and number lines in a text file."'
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
|
1998-08-10 19:42:37 +00:00
|
|
|
\section{\module{cPickle} ---
|
1999-04-22 21:23:22 +00:00
|
|
|
Alternate implementation of \module{pickle}}
|
1998-07-23 17:59:49 +00:00
|
|
|
|
1999-04-22 21:23:22 +00:00
|
|
|
\declaremodule{builtin}{cPickle}
|
2000-04-03 20:13:55 +00:00
|
|
|
\modulesynopsis{Faster version of \refmodule{pickle}, but not subclassable.}
|
1999-04-22 21:23:22 +00:00
|
|
|
\moduleauthor{Jim Fulton}{jfulton@digicool.com}
|
|
|
|
\sectionauthor{Fred L. Drake, Jr.}{fdrake@acm.org}
|
1998-07-23 17:59:49 +00:00
|
|
|
|
1998-04-11 20:05:43 +00:00
|
|
|
|
|
|
|
The \module{cPickle} module provides a similar interface and identical
|
1999-07-02 14:25:37 +00:00
|
|
|
functionality as the \refmodule{pickle}\refstmodindex{pickle} module,
|
|
|
|
but can be up to 1000 times faster since it is implemented in C. The
|
|
|
|
only other important difference to note is that \function{Pickler()}
|
|
|
|
and \function{Unpickler()} are functions and not classes, and so
|
|
|
|
cannot be subclassed. This should not be an issue in most cases.
|
1998-04-11 20:05:43 +00:00
|
|
|
|
|
|
|
The format of the pickle data is identical to that produced using the
|
1999-04-22 21:23:22 +00:00
|
|
|
\refmodule{pickle} module, so it is possible to use \refmodule{pickle} and
|
2000-07-16 19:01:10 +00:00
|
|
|
\module{cPickle} interchangeably with existing pickles.
|
1999-01-06 23:34:39 +00:00
|
|
|
|
|
|
|
(Since the pickle data format is actually a tiny stack-oriented
|
|
|
|
programming language, and there are some freedoms in the encodings of
|
|
|
|
certain objects, it's possible that the two modules produce different
|
|
|
|
pickled data for the same input objects; however they will always be
|
2000-07-01 17:47:38 +00:00
|
|
|
able to read each other's pickles back in.)
|