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="next" href="stream-reader-objects.html" /> | |
13 | <link rel="prev" href="codec-objects.html" /> | |
14 | <link rel="parent" href="node130.html" /> | |
15 | <link rel="next" href="stream-reader-objects.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>4.9.1.2 StreamWriter Objects </title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="4.9.1.1 Codec Objects" | |
25 | href="codec-objects.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="4.9.1 Codec Base Classes" | |
28 | href="node130.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="4.9.1.3 StreamReader Objects" | |
31 | href="stream-reader-objects.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Library Reference</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
38 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="codec-objects.html">4.9.1.1 Codec Objects</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="node130.html">4.9.1 Codec Base Classes</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="stream-reader-objects.html">4.9.1.3 StreamReader Objects</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H3><A NAME="SECTION006912000000000000000"></A><A NAME="stream-writer-objects"></A> | |
56 | <BR> | |
57 | 4.9.1.2 StreamWriter Objects | |
58 | </H3> | |
59 | ||
60 | <P> | |
61 | The <tt class="class">StreamWriter</tt> class is a subclass of <tt class="class">Codec</tt> and | |
62 | defines the following methods which every stream writer must define in | |
63 | order to be compatible to the Python codec registry. | |
64 | ||
65 | <P> | |
66 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
67 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-1007' xml:id='l2h-1007' class="class">StreamWriter</tt></b>(</nobr></td> | |
68 | <td><var>stream</var><big>[</big><var>, errors</var><big>]</big><var></var>)</td></tr></table></dt> | |
69 | <dd> | |
70 | Constructor for a <tt class="class">StreamWriter</tt> instance. | |
71 | ||
72 | <P> | |
73 | All stream writers must provide this constructor interface. They are | |
74 | free to add additional keyword arguments, but only the ones defined | |
75 | here are used by the Python codec registry. | |
76 | ||
77 | <P> | |
78 | <var>stream</var> must be a file-like object open for writing (binary) | |
79 | data. | |
80 | ||
81 | <P> | |
82 | The <tt class="class">StreamWriter</tt> may implement different error handling | |
83 | schemes by providing the <var>errors</var> keyword argument. These | |
84 | parameters are predefined: | |
85 | ||
86 | <P> | |
87 | ||
88 | <UL> | |
89 | <LI><code>'strict'</code> Raise <tt class="exception">ValueError</tt> (or a subclass); | |
90 | this is the default. | |
91 | </LI> | |
92 | <LI><code>'ignore'</code> Ignore the character and continue with the next. | |
93 | </LI> | |
94 | <LI><code>'replace'</code> Replace with a suitable replacement character | |
95 | </LI> | |
96 | <LI><code>'xmlcharrefreplace'</code> Replace with the appropriate XML | |
97 | character reference | |
98 | </LI> | |
99 | <LI><code>'backslashreplace'</code> Replace with backslashed escape sequences. | |
100 | ||
101 | </LI> | |
102 | </UL> | |
103 | ||
104 | <P> | |
105 | The <var>errors</var> argument will be assigned to an attribute of the | |
106 | same name. Assigning to this attribute makes it possible to switch | |
107 | between different error handling strategies during the lifetime | |
108 | of the <tt class="class">StreamWriter</tt> object. | |
109 | ||
110 | <P> | |
111 | The set of allowed values for the <var>errors</var> argument can | |
112 | be extended with <tt class="function">register_error()</tt>. | |
113 | </dl> | |
114 | ||
115 | <P> | |
116 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
117 | <td><nobr><b><tt id='l2h-1008' xml:id='l2h-1008' class="method">write</tt></b>(</nobr></td> | |
118 | <td><var>object</var>)</td></tr></table></dt> | |
119 | <dd> | |
120 | Writes the object's contents encoded to the stream. | |
121 | </dl> | |
122 | ||
123 | <P> | |
124 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
125 | <td><nobr><b><tt id='l2h-1009' xml:id='l2h-1009' class="method">writelines</tt></b>(</nobr></td> | |
126 | <td><var>list</var>)</td></tr></table></dt> | |
127 | <dd> | |
128 | Writes the concatenated list of strings to the stream (possibly by | |
129 | reusing the <tt class="method">write()</tt> method). | |
130 | </dl> | |
131 | ||
132 | <P> | |
133 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
134 | <td><nobr><b><tt id='l2h-1010' xml:id='l2h-1010' class="method">reset</tt></b>(</nobr></td> | |
135 | <td><var></var>)</td></tr></table></dt> | |
136 | <dd> | |
137 | Flushes and resets the codec buffers used for keeping state. | |
138 | ||
139 | <P> | |
140 | Calling this method should ensure that the data on the output is put | |
141 | into a clean state, that allows appending of new fresh data without | |
142 | having to rescan the whole stream to recover state. | |
143 | </dl> | |
144 | ||
145 | <P> | |
146 | In addition to the above methods, the <tt class="class">StreamWriter</tt> must also | |
147 | inherit all other methods and attribute from the underlying stream. | |
148 | ||
149 | <P> | |
150 | ||
151 | <DIV CLASS="navigation"> | |
152 | <div class='online-navigation'> | |
153 | <p></p><hr /> | |
154 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
155 | <tr> | |
156 | <td class='online-navigation'><a rel="prev" title="4.9.1.1 Codec Objects" | |
157 | href="codec-objects.html"><img src='../icons/previous.png' | |
158 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
159 | <td class='online-navigation'><a rel="parent" title="4.9.1 Codec Base Classes" | |
160 | href="node130.html"><img src='../icons/up.png' | |
161 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
162 | <td class='online-navigation'><a rel="next" title="4.9.1.3 StreamReader Objects" | |
163 | href="stream-reader-objects.html"><img src='../icons/next.png' | |
164 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
165 | <td align="center" width="100%">Python Library Reference</td> | |
166 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
167 | href="contents.html"><img src='../icons/contents.png' | |
168 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
169 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
170 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
171 | <td class='online-navigation'><a rel="index" title="Index" | |
172 | href="genindex.html"><img src='../icons/index.png' | |
173 | border='0' height='32' alt='Index' width='32' /></A></td> | |
174 | </tr></table> | |
175 | <div class='online-navigation'> | |
176 | <b class="navlabel">Previous:</b> | |
177 | <a class="sectref" rel="prev" href="codec-objects.html">4.9.1.1 Codec Objects</A> | |
178 | <b class="navlabel">Up:</b> | |
179 | <a class="sectref" rel="parent" href="node130.html">4.9.1 Codec Base Classes</A> | |
180 | <b class="navlabel">Next:</b> | |
181 | <a class="sectref" rel="next" href="stream-reader-objects.html">4.9.1.3 StreamReader Objects</A> | |
182 | </div> | |
183 | </div> | |
184 | <hr /> | |
185 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
186 | </DIV> | |
187 | <!--End of Navigation Panel--> | |
188 | <ADDRESS> | |
189 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
190 | </ADDRESS> | |
191 | </BODY> | |
192 | </HTML> |