Commit | Line | Data |
---|---|---|
920dae64 AT |
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="pickle-sub.html" /> | |
13 | <link rel="parent" href="module-pickle.html" /> | |
14 | <link rel="next" href="module-cPickle.html" /> | |
15 | <meta name='aesop' content='information' /> | |
16 | <title>3.14.7 Example </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.6 Subclassing Unpicklers" | |
24 | href="pickle-sub.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 pickle " | |
27 | href="module-pickle.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.15 cPickle " | |
30 | href="module-cPickle.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="pickle-sub.html">3.14.6 Subclassing Unpicklers</A> | |
45 | <b class="navlabel">Up:</b> | |
46 | <a class="sectref" rel="parent" href="module-pickle.html">3.14 pickle </A> | |
47 | <b class="navlabel">Next:</b> | |
48 | <a class="sectref" rel="next" href="module-cPickle.html">3.15 cPickle </A> | |
49 | </div> | |
50 | <hr /></div> | |
51 | </DIV> | |
52 | <!--End of Navigation Panel--> | |
53 | ||
54 | <H2><A NAME="SECTION0051470000000000000000"></A><A NAME="pickle-example"></A> | |
55 | <BR> | |
56 | 3.14.7 Example | |
57 | </H2> | |
58 | ||
59 | <P> | |
60 | Here's a simple example of how to modify pickling behavior for a | |
61 | class. The <tt class="class">TextReader</tt> class opens a text file, and returns | |
62 | the line number and line contents each time its <tt class="method">readline()</tt> | |
63 | method is called. If a <tt class="class">TextReader</tt> instance is pickled, all | |
64 | attributes <em>except</em> the file object member are saved. When the | |
65 | instance is unpickled, the file is reopened, and reading resumes from | |
66 | the last location. The <tt class="method">__setstate__()</tt> and | |
67 | <tt class="method">__getstate__()</tt> methods are used to implement this behavior. | |
68 | ||
69 | <P> | |
70 | <div class="verbatim"><pre> | |
71 | class TextReader: | |
72 | """Print and number lines in a text file.""" | |
73 | def __init__(self, file): | |
74 | self.file = file | |
75 | self.fh = open(file) | |
76 | self.lineno = 0 | |
77 | ||
78 | def readline(self): | |
79 | self.lineno = self.lineno + 1 | |
80 | line = self.fh.readline() | |
81 | if not line: | |
82 | return None | |
83 | if line.endswith("\n"): | |
84 | line = line[:-1] | |
85 | return "%d: %s" % (self.lineno, line) | |
86 | ||
87 | def __getstate__(self): | |
88 | odict = self.__dict__.copy() # copy the dict since we change it | |
89 | del odict['fh'] # remove filehandle entry | |
90 | return odict | |
91 | ||
92 | def __setstate__(self,dict): | |
93 | fh = open(dict['file']) # reopen file | |
94 | count = dict['lineno'] # read from file... | |
95 | while count: # until line count is restored | |
96 | fh.readline() | |
97 | count = count - 1 | |
98 | self.__dict__.update(dict) # update attributes | |
99 | self.fh = fh # save the file object | |
100 | </pre></div> | |
101 | ||
102 | <P> | |
103 | A sample usage might be something like this: | |
104 | ||
105 | <P> | |
106 | <div class="verbatim"><pre> | |
107 | >>> import TextReader | |
108 | >>> obj = TextReader.TextReader("TextReader.py") | |
109 | >>> obj.readline() | |
110 | '1: #!/usr/local/bin/python' | |
111 | >>> # (more invocations of obj.readline() here) | |
112 | ... obj.readline() | |
113 | '7: class TextReader:' | |
114 | >>> import pickle | |
115 | >>> pickle.dump(obj,open('save.p','w')) | |
116 | </pre></div> | |
117 | ||
118 | <P> | |
119 | If you want to see that <tt class="module"><a href="module-pickle.html">pickle</a></tt> works across Python | |
120 | processes, start another Python session, before continuing. What | |
121 | follows can happen from either the same process or a new process. | |
122 | ||
123 | <P> | |
124 | <div class="verbatim"><pre> | |
125 | >>> import pickle | |
126 | >>> reader = pickle.load(open('save.p')) | |
127 | >>> reader.readline() | |
128 | '8: "Print and number lines in a text file."' | |
129 | </pre></div> | |
130 | ||
131 | <P> | |
132 | <div class="seealso"> | |
133 | <p class="heading">See Also:</p> | |
134 | ||
135 | <dl compact="compact" class="seemodule"> | |
136 | <dt>Module <b><tt class="module"><a href="module-copyreg.html">copy_reg</a></tt>:</b> | |
137 | <dd>Pickle interface constructor | |
138 | registration for extension types. | |
139 | </dl> | |
140 | ||
141 | <P> | |
142 | <dl compact="compact" class="seemodule"> | |
143 | <dt>Module <b><tt class="module"><a href="module-shelve.html">shelve</a></tt>:</b> | |
144 | <dd>Indexed databases of objects; uses <tt class="module">pickle</tt>. | |
145 | </dl> | |
146 | ||
147 | <P> | |
148 | <dl compact="compact" class="seemodule"> | |
149 | <dt>Module <b><tt class="module"><a href="module-copy.html">copy</a></tt>:</b> | |
150 | <dd>Shallow and deep object copying. | |
151 | </dl> | |
152 | ||
153 | <P> | |
154 | <dl compact="compact" class="seemodule"> | |
155 | <dt>Module <b><tt class="module"><a href="module-marshal.html">marshal</a></tt>:</b> | |
156 | <dd>High-performance serialization of built-in types. | |
157 | </dl> | |
158 | </div> | |
159 | ||
160 | <P> | |
161 | ||
162 | <DIV CLASS="navigation"> | |
163 | <div class='online-navigation'> | |
164 | <p></p><hr /> | |
165 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
166 | <tr> | |
167 | <td class='online-navigation'><a rel="prev" title="3.14.6 Subclassing Unpicklers" | |
168 | href="pickle-sub.html"><img src='../icons/previous.png' | |
169 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
170 | <td class='online-navigation'><a rel="parent" title="3.14 pickle " | |
171 | href="module-pickle.html"><img src='../icons/up.png' | |
172 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
173 | <td class='online-navigation'><a rel="next" title="3.15 cPickle " | |
174 | href="module-cPickle.html"><img src='../icons/next.png' | |
175 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
176 | <td align="center" width="100%">Python Library Reference</td> | |
177 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
178 | href="contents.html"><img src='../icons/contents.png' | |
179 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
180 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
181 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
182 | <td class='online-navigation'><a rel="index" title="Index" | |
183 | href="genindex.html"><img src='../icons/index.png' | |
184 | border='0' height='32' alt='Index' width='32' /></A></td> | |
185 | </tr></table> | |
186 | <div class='online-navigation'> | |
187 | <b class="navlabel">Previous:</b> | |
188 | <a class="sectref" rel="prev" href="pickle-sub.html">3.14.6 Subclassing Unpicklers</A> | |
189 | <b class="navlabel">Up:</b> | |
190 | <a class="sectref" rel="parent" href="module-pickle.html">3.14 pickle </A> | |
191 | <b class="navlabel">Next:</b> | |
192 | <a class="sectref" rel="next" href="module-cPickle.html">3.15 cPickle </A> | |
193 | </div> | |
194 | </div> | |
195 | <hr /> | |
196 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
197 | </DIV> | |
198 | <!--End of Navigation Panel--> | |
199 | <ADDRESS> | |
200 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
201 | </ADDRESS> | |
202 | </BODY> | |
203 | </HTML> |