comparison doc/streams.txt @ 853:f33ecf3c319e trunk

Convert a bunch of print statements to py3k compatible syntax.
author cmlenz
date Tue, 10 Nov 2009 21:22:51 +0000
parents f459f22f7ad2
children 129e54866a98
comparison
equal deleted inserted replaced
852:07f4339fecb0 853:f33ecf3c319e
44 event “comes from”. 44 event “comes from”.
45 45
46 .. code-block:: pycon 46 .. code-block:: pycon
47 47
48 >>> for kind, data, pos in stream: 48 >>> for kind, data, pos in stream:
49 ... print kind, `data`, pos 49 ... print('%s %r %r' % (kind, data, pos))
50 ... 50 ...
51 START (QName(u'p'), Attrs([(QName(u'class'), u'intro')])) (None, 1, 0) 51 START (QName(u'p'), Attrs([(QName(u'class'), u'intro')])) (None, 1, 0)
52 TEXT u'Some text and ' (None, 1, 17) 52 TEXT u'Some text and ' (None, 1, 17)
53 START (QName(u'a'), Attrs([(QName(u'href'), u'http://example.org/')])) (None, 1, 31) 53 START (QName(u'a'), Attrs([(QName(u'href'), u'http://example.org/')])) (None, 1, 31)
54 TEXT u'a link' (None, 1, 61) 54 TEXT u'a link' (None, 1, 61)
141 Here's the output from ``serialize()``: 141 Here's the output from ``serialize()``:
142 142
143 .. code-block:: pycon 143 .. code-block:: pycon
144 144
145 >>> for output in stream.serialize(): 145 >>> for output in stream.serialize():
146 ... print `output` 146 ... print(repr(output))
147 ... 147 ...
148 <Markup u'<p class="intro">'> 148 <Markup u'<p class="intro">'>
149 <Markup u'Some text and '> 149 <Markup u'Some text and '>
150 <Markup u'<a href="http://example.org/">'> 150 <Markup u'<a href="http://example.org/">'>
151 <Markup u'a link'> 151 <Markup u'a link'>
156 156
157 And here's the output from ``render()``: 157 And here's the output from ``render()``:
158 158
159 .. code-block:: pycon 159 .. code-block:: pycon
160 160
161 >>> print stream.render() 161 >>> print(stream.render())
162 <p class="intro">Some text and <a href="http://example.org/">a link</a>.<br/></p> 162 <p class="intro">Some text and <a href="http://example.org/">a link</a>.<br/></p>
163 163
164 Both methods can be passed a ``method`` parameter that determines how exactly 164 Both methods can be passed a ``method`` parameter that determines how exactly
165 the events are serialized to text. This parameter can be either a string or a 165 the events are serialized to text. This parameter can be either a string or a
166 custom serializer class: 166 custom serializer class:
167 167
168 .. code-block:: pycon 168 .. code-block:: pycon
169 169
170 >>> print stream.render('html') 170 >>> print(stream.render('html'))
171 <p class="intro">Some text and <a href="http://example.org/">a link</a>.<br></p> 171 <p class="intro">Some text and <a href="http://example.org/">a link</a>.<br></p>
172 172
173 Note how the `<br>` element isn't closed, which is the right thing to do for 173 Note how the `<br>` element isn't closed, which is the right thing to do for
174 HTML. See `serialization methods`_ for more details. 174 HTML. See `serialization methods`_ for more details.
175 175
181 181
182 .. code-block:: pycon 182 .. code-block:: pycon
183 183
184 >>> from genshi.filters import HTMLSanitizer 184 >>> from genshi.filters import HTMLSanitizer
185 >>> from genshi.output import TextSerializer 185 >>> from genshi.output import TextSerializer
186 >>> print ''.join(TextSerializer()(HTMLSanitizer()(stream))) 186 >>> print(''.join(TextSerializer()(HTMLSanitizer()(stream))))
187 Some text and a link. 187 Some text and a link.
188 188
189 The pipe operator allows a nicer syntax: 189 The pipe operator allows a nicer syntax:
190 190
191 .. code-block:: pycon 191 .. code-block:: pycon
192 192
193 >>> print stream | HTMLSanitizer() | TextSerializer() 193 >>> print(stream | HTMLSanitizer() | TextSerializer())
194 Some text and a link. 194 Some text and a link.
195 195
196 196
197 .. _`serialization methods`: 197 .. _`serialization methods`:
198 198
325 .. code-block:: pycon 325 .. code-block:: pycon
326 326
327 >>> substream = stream.select('a') 327 >>> substream = stream.select('a')
328 >>> substream 328 >>> substream
329 <genshi.core.Stream object at ...> 329 <genshi.core.Stream object at ...>
330 >>> print substream 330 >>> print(substream)
331 <a href="http://example.org/">a link</a> 331 <a href="http://example.org/">a link</a>
332 332
333 Often, streams cannot be reused: in the above example, the sub-stream is based 333 Often, streams cannot be reused: in the above example, the sub-stream is based
334 on a generator. Once it has been serialized, it will have been fully consumed, 334 on a generator. Once it has been serialized, it will have been fully consumed,
335 and cannot be rendered again. To work around this, you can wrap such a stream 335 and cannot be rendered again. To work around this, you can wrap such a stream
339 339
340 >>> from genshi import Stream 340 >>> from genshi import Stream
341 >>> substream = Stream(list(stream.select('a'))) 341 >>> substream = Stream(list(stream.select('a')))
342 >>> substream 342 >>> substream
343 <genshi.core.Stream object at ...> 343 <genshi.core.Stream object at ...>
344 >>> print substream 344 >>> print(substream)
345 <a href="http://example.org/">a link</a> 345 <a href="http://example.org/">a link</a>
346 >>> print substream.select('@href') 346 >>> print(substream.select('@href'))
347 http://example.org/ 347 http://example.org/
348 >>> print substream.select('text()') 348 >>> print(substream.select('text()'))
349 a link 349 a link
350 350
351 See `Using XPath in Genshi`_ for more information about the XPath support in 351 See `Using XPath in Genshi`_ for more information about the XPath support in
352 Genshi. 352 Genshi.
353 353
Copyright (C) 2012-2017 Edgewall Software