File: pickling.rst

package info (click to toggle)
python-persistent 4.0.8-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 680 kB
  • ctags: 1,140
  • sloc: python: 4,156; ansic: 2,727; xml: 845; makefile: 112
file content (168 lines) | stat: -rw-r--r-- 3,808 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
Pickling Persistent Objects
===========================

Persistent objects are designed to make the standard Python pickling
machinery happy:

.. doctest::

   >>> import pickle
   >>> from persistent.tests.cucumbers import Simple
   >>> from persistent.tests.cucumbers import print_dict

   >>> x = Simple('x', aaa=1, bbb='foo')

   >>> print_dict(x.__getstate__())
   {'__name__': 'x', 'aaa': 1, 'bbb': 'foo'}

   >>> f, (c,), state = x.__reduce__()
   >>> f.__name__
   '__newobj__'
   >>> f.__module__
   'copy_reg'
   >>> c.__name__
   'Simple'

   >>> print_dict(state)
   {'__name__': 'x', 'aaa': 1, 'bbb': 'foo'}

   >>> import pickle
   >>> pickle.loads(pickle.dumps(x)) == x
   True
   >>> pickle.loads(pickle.dumps(x, 0)) == x
   True
   >>> pickle.loads(pickle.dumps(x, 1)) == x
   True

   >>> pickle.loads(pickle.dumps(x, 2)) == x
   True

   >>> x.__setstate__({'z': 1})
   >>> x.__dict__
   {'z': 1}

This support even works well for derived classes which customize pickling
by overriding :meth:`__getnewargs__`, :meth:`__getstate__` and
:meth:`__setstate__`.

.. doctest::

   >>> from persistent.tests.cucumbers import Custom

   >>> x = Custom('x', 'y')
   >>> x.__getnewargs__()
   ('x', 'y')
   >>> x.a = 99

   >>> (f, (c, ax, ay), a) = x.__reduce__()
   >>> f.__name__
   '__newobj__'
   >>> f.__module__
   'copy_reg'
   >>> c.__name__
   'Custom'
   >>> ax, ay, a
   ('x', 'y', 99)

   >>> pickle.loads(pickle.dumps(x)) == x
   True
   >>> pickle.loads(pickle.dumps(x, 0)) == x
   True
   >>> pickle.loads(pickle.dumps(x, 1)) == x
   True
   >>> pickle.loads(pickle.dumps(x, 2)) == x
   True

The support works for derived classes which define :attr:`__slots__`.  It
ignores any slots which map onto the "persistent" namespace (prefixed with
``_p_``) or the "volatile" namespace (prefixed with ``_v_``):

.. doctest::

   >>> import copy_reg
   >>> from persistent.tests.cucumbers import SubSlotted
   >>> x = SubSlotted('x', 'y', 'z')

Note that we haven't yet assiged a value to the ``s4`` attribute:

.. doctest::

   >>> d, s = x.__getstate__()
   >>> d
   >>> print_dict(s)
   {'s1': 'x', 's2': 'y', 's3': 'z'}

   >>> import pickle
   >>> pickle.loads(pickle.dumps(x)) == x
   True
   >>> pickle.loads(pickle.dumps(x, 0)) == x
   True
   >>> pickle.loads(pickle.dumps(x, 1)) == x
   True
   >>> pickle.loads(pickle.dumps(x, 2)) == x
   True


After assigning it:

.. doctest::

   >>> x.s4 = 'spam'

   >>> d, s = x.__getstate__()
   >>> d
   >>> print_dict(s)
   {'s1': 'x', 's2': 'y', 's3': 'z', 's4': 'spam'}

   >>> pickle.loads(pickle.dumps(x)) == x
   True
   >>> pickle.loads(pickle.dumps(x, 0)) == x
   True
   >>> pickle.loads(pickle.dumps(x, 1)) == x
   True
   >>> pickle.loads(pickle.dumps(x, 2)) == x
   True

:class:`persistent.Persistent` supports derived classes which have base
classes defining :attr:`__slots`, but which do not define attr:`__slots__`
themselves:

.. doctest::

   >>> from persistent.tests.cucumbers import SubSubSlotted
   >>> x = SubSubSlotted('x', 'y', 'z')

   >>> d, s = x.__getstate__()
   >>> print_dict(d)
   {}
   >>> print_dict(s)
   {'s1': 'x', 's2': 'y', 's3': 'z'}

   >>> import pickle
   >>> pickle.loads(pickle.dumps(x)) == x
   True
   >>> pickle.loads(pickle.dumps(x, 0)) == x
   True
   >>> pickle.loads(pickle.dumps(x, 1)) == x
   True
   >>> pickle.loads(pickle.dumps(x, 2)) == x
   True

   >>> x.s4 = 'spam'
   >>> x.foo = 'bar'
   >>> x.baz = 'bam'

   >>> d, s = x.__getstate__()
   >>> print_dict(d)
   {'baz': 'bam', 'foo': 'bar'}
   >>> print_dict(s)
   {'s1': 'x', 's2': 'y', 's3': 'z', 's4': 'spam'}

   >>> pickle.loads(pickle.dumps(x)) == x
   True
   >>> pickle.loads(pickle.dumps(x, 0)) == x
   True
   >>> pickle.loads(pickle.dumps(x, 1)) == x
   True
   >>> pickle.loads(pickle.dumps(x, 2)) == x
   True