| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="prev" href="node69.html" /> |
| 13 | <link rel="parent" href="pickle-protocol.html" /> |
| 14 | <link rel="next" href="pickle-sub.html" /> |
| 15 | <meta name='aesop' content='information' /> |
| 16 | <title>3.14.5.3 Pickling and unpickling external objects</title> |
| 17 | </head> |
| 18 | <body> |
| 19 | <DIV CLASS="navigation"> |
| 20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 22 | <tr> |
| 23 | <td class='online-navigation'><a rel="prev" title="3.14.5.2 Pickling and unpickling" |
| 24 | href="node69.html"><img src='../icons/previous.png' |
| 25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 26 | <td class='online-navigation'><a rel="parent" title="3.14.5 The pickle protocol" |
| 27 | href="pickle-protocol.html"><img src='../icons/up.png' |
| 28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 29 | <td class='online-navigation'><a rel="next" title="3.14.6 Subclassing Unpicklers" |
| 30 | href="pickle-sub.html"><img src='../icons/next.png' |
| 31 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 32 | <td align="center" width="100%">Python Library Reference</td> |
| 33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 34 | href="contents.html"><img src='../icons/contents.png' |
| 35 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 36 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 37 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 38 | <td class='online-navigation'><a rel="index" title="Index" |
| 39 | href="genindex.html"><img src='../icons/index.png' |
| 40 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 41 | </tr></table> |
| 42 | <div class='online-navigation'> |
| 43 | <b class="navlabel">Previous:</b> |
| 44 | <a class="sectref" rel="prev" href="node69.html">3.14.5.2 Pickling and unpickling</A> |
| 45 | <b class="navlabel">Up:</b> |
| 46 | <a class="sectref" rel="parent" href="pickle-protocol.html">3.14.5 The pickle protocol</A> |
| 47 | <b class="navlabel">Next:</b> |
| 48 | <a class="sectref" rel="next" href="pickle-sub.html">3.14.6 Subclassing Unpicklers</A> |
| 49 | </div> |
| 50 | <hr /></div> |
| 51 | </DIV> |
| 52 | <!--End of Navigation Panel--> |
| 53 | |
| 54 | <H3><A NAME="SECTION0051453000000000000000"> |
| 55 | 3.14.5.3 Pickling and unpickling external objects</A> |
| 56 | </H3> |
| 57 | |
| 58 | <P> |
| 59 | For the benefit of object persistence, the <tt class="module">pickle</tt> module |
| 60 | supports the notion of a reference to an object outside the pickled |
| 61 | data stream. Such objects are referenced by a ``persistent id'', |
| 62 | which is just an arbitrary string of printable ASCII characters. |
| 63 | The resolution of such names is not defined by the <tt class="module">pickle</tt> |
| 64 | module; it will delegate this resolution to user defined functions on |
| 65 | the pickler and unpickler.<A NAME="tex2html20" |
| 66 | HREF="#foot8985"><SUP>3.8</SUP></A> |
| 67 | <P> |
| 68 | To define external persistent id resolution, you need to set the |
| 69 | <tt class="member">persistent_id</tt> attribute of the pickler object and the |
| 70 | <tt class="member">persistent_load</tt> attribute of the unpickler object. |
| 71 | |
| 72 | <P> |
| 73 | To pickle objects that have an external persistent id, the pickler |
| 74 | must have a custom <tt class="function">persistent_id()</tt> method that takes an |
| 75 | object as an argument and returns either <code>None</code> or the persistent |
| 76 | id for that object. When <code>None</code> is returned, the pickler simply |
| 77 | pickles the object as normal. When a persistent id string is |
| 78 | returned, the pickler will pickle that string, along with a marker |
| 79 | so that the unpickler will recognize the string as a persistent id. |
| 80 | |
| 81 | <P> |
| 82 | To unpickle external objects, the unpickler must have a custom |
| 83 | <tt class="function">persistent_load()</tt> function that takes a persistent id |
| 84 | string and returns the referenced object. |
| 85 | |
| 86 | <P> |
| 87 | Here's a silly example that <em>might</em> shed more light: |
| 88 | |
| 89 | <P> |
| 90 | <div class="verbatim"><pre> |
| 91 | import pickle |
| 92 | from cStringIO import StringIO |
| 93 | |
| 94 | src = StringIO() |
| 95 | p = pickle.Pickler(src) |
| 96 | |
| 97 | def persistent_id(obj): |
| 98 | if hasattr(obj, 'x'): |
| 99 | return 'the value %d' % obj.x |
| 100 | else: |
| 101 | return None |
| 102 | |
| 103 | p.persistent_id = persistent_id |
| 104 | |
| 105 | class Integer: |
| 106 | def __init__(self, x): |
| 107 | self.x = x |
| 108 | def __str__(self): |
| 109 | return 'My name is integer %d' % self.x |
| 110 | |
| 111 | i = Integer(7) |
| 112 | print i |
| 113 | p.dump(i) |
| 114 | |
| 115 | datastream = src.getvalue() |
| 116 | print repr(datastream) |
| 117 | dst = StringIO(datastream) |
| 118 | |
| 119 | up = pickle.Unpickler(dst) |
| 120 | |
| 121 | class FancyInteger(Integer): |
| 122 | def __str__(self): |
| 123 | return 'I am the integer %d' % self.x |
| 124 | |
| 125 | def persistent_load(persid): |
| 126 | if persid.startswith('the value '): |
| 127 | value = int(persid.split()[2]) |
| 128 | return FancyInteger(value) |
| 129 | else: |
| 130 | raise pickle.UnpicklingError, 'Invalid persistent id' |
| 131 | |
| 132 | up.persistent_load = persistent_load |
| 133 | |
| 134 | j = up.load() |
| 135 | print j |
| 136 | </pre></div> |
| 137 | |
| 138 | <P> |
| 139 | In the <tt class="module">cPickle</tt> module, the unpickler's |
| 140 | <tt class="member">persistent_load</tt> attribute can also be set to a Python |
| 141 | list, in which case, when the unpickler reaches a persistent id, the |
| 142 | persistent id string will simply be appended to this list. This |
| 143 | functionality exists so that a pickle data stream can be ``sniffed'' |
| 144 | for object references without actually instantiating all the objects |
| 145 | in a pickle.<A NAME="tex2html21" |
| 146 | HREF="#foot8892"><SUP>3.9</SUP></A> Setting |
| 147 | <tt class="member">persistent_load</tt> to a list is usually used in conjunction with |
| 148 | the <tt class="method">noload()</tt> method on the Unpickler. |
| 149 | |
| 150 | <P> |
| 151 | <BR><HR><H4>Footnotes</H4> |
| 152 | <DL> |
| 153 | <DT><A NAME="foot8985">... unpickler.</A><A |
| 154 | HREF="node70.html#tex2html20"><SUP>3.8</SUP></A></DT> |
| 155 | <DD>The actual mechanism for |
| 156 | associating these user defined functions is slightly different for |
| 157 | <tt class="module">pickle</tt> and <tt class="module">cPickle</tt>. The description given here |
| 158 | works the same for both implementations. Users of the <tt class="module">pickle</tt> |
| 159 | module could also use subclassing to effect the same results, |
| 160 | overriding the <tt class="method">persistent_id()</tt> and <tt class="method">persistent_load()</tt> |
| 161 | methods in the derived classes. |
| 162 | |
| 163 | </DD> |
| 164 | <DT><A NAME="foot8892">... pickle.</A><A |
| 165 | HREF="node70.html#tex2html21"><SUP>3.9</SUP></A></DT> |
| 166 | <DD>We'll leave you with the image of Guido and Jim |
| 167 | sitting around sniffing pickles in their living rooms. |
| 168 | |
| 169 | </DD> |
| 170 | </DL> |
| 171 | <DIV CLASS="navigation"> |
| 172 | <div class='online-navigation'> |
| 173 | <p></p><hr /> |
| 174 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 175 | <tr> |
| 176 | <td class='online-navigation'><a rel="prev" title="3.14.5.2 Pickling and unpickling" |
| 177 | href="node69.html"><img src='../icons/previous.png' |
| 178 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 179 | <td class='online-navigation'><a rel="parent" title="3.14.5 The pickle protocol" |
| 180 | href="pickle-protocol.html"><img src='../icons/up.png' |
| 181 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 182 | <td class='online-navigation'><a rel="next" title="3.14.6 Subclassing Unpicklers" |
| 183 | href="pickle-sub.html"><img src='../icons/next.png' |
| 184 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 185 | <td align="center" width="100%">Python Library Reference</td> |
| 186 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 187 | href="contents.html"><img src='../icons/contents.png' |
| 188 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 189 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 190 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 191 | <td class='online-navigation'><a rel="index" title="Index" |
| 192 | href="genindex.html"><img src='../icons/index.png' |
| 193 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 194 | </tr></table> |
| 195 | <div class='online-navigation'> |
| 196 | <b class="navlabel">Previous:</b> |
| 197 | <a class="sectref" rel="prev" href="node69.html">3.14.5.2 Pickling and unpickling</A> |
| 198 | <b class="navlabel">Up:</b> |
| 199 | <a class="sectref" rel="parent" href="pickle-protocol.html">3.14.5 The pickle protocol</A> |
| 200 | <b class="navlabel">Next:</b> |
| 201 | <a class="sectref" rel="next" href="pickle-sub.html">3.14.6 Subclassing Unpicklers</A> |
| 202 | </div> |
| 203 | </div> |
| 204 | <hr /> |
| 205 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 206 | </DIV> |
| 207 | <!--End of Navigation Panel--> |
| 208 | <ADDRESS> |
| 209 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 210 | </ADDRESS> |
| 211 | </BODY> |
| 212 | </HTML> |