test/pylit_test.py: new test for modification time settings
[pylit.git] / rstdocs / examples / iterqueue_test.py.html
blobfc8cb21cdd61317d44629dfac160e2cb6e54c561
1 <?xml version="1.0" encoding="iso-8859-1" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
6 <meta name="generator" content="Docutils 0.4.1: http://docutils.sourceforge.net/" />
7 <title>Test the iterator wrappers from iterqueue.py</title>
8 <style type="text/css">
11 :Author: David Goodger
12 :Contact: goodger@users.sourceforge.net
13 :Date: $Date: 2005-12-18 01:56:14 +0100 (Sun, 18 Dec 2005) $
14 :Revision: $Revision: 4224 $
15 :Copyright: This stylesheet has been placed in the public domain.
17 Default cascading style sheet for the HTML output of Docutils.
19 See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
20 customize this style sheet.
23 /* used to remove borders from tables and images */
24 .borderless, table.borderless td, table.borderless th {
25 border: 0 }
27 table.borderless td, table.borderless th {
28 /* Override padding for "table.docutils td" with "! important".
29 The right padding separates the table cells. */
30 padding: 0 0.5em 0 0 ! important }
32 .first {
33 /* Override more specific margin styles with "! important". */
34 margin-top: 0 ! important }
36 .last, .with-subtitle {
37 margin-bottom: 0 ! important }
39 .hidden {
40 display: none }
42 a.toc-backref {
43 text-decoration: none ;
44 color: black }
46 blockquote.epigraph {
47 margin: 2em 5em ; }
49 dl.docutils dd {
50 margin-bottom: 0.5em }
52 /* Uncomment (and remove this text!) to get bold-faced definition list terms
53 dl.docutils dt {
54 font-weight: bold }
57 div.abstract {
58 margin: 2em 5em }
60 div.abstract p.topic-title {
61 font-weight: bold ;
62 text-align: center }
64 div.admonition, div.attention, div.caution, div.danger, div.error,
65 div.hint, div.important, div.note, div.tip, div.warning {
66 margin: 2em ;
67 border: medium outset ;
68 padding: 1em }
70 div.admonition p.admonition-title, div.hint p.admonition-title,
71 div.important p.admonition-title, div.note p.admonition-title,
72 div.tip p.admonition-title {
73 font-weight: bold ;
74 font-family: sans-serif }
76 div.attention p.admonition-title, div.caution p.admonition-title,
77 div.danger p.admonition-title, div.error p.admonition-title,
78 div.warning p.admonition-title {
79 color: red ;
80 font-weight: bold ;
81 font-family: sans-serif }
83 /* Uncomment (and remove this text!) to get reduced vertical space in
84 compound paragraphs.
85 div.compound .compound-first, div.compound .compound-middle {
86 margin-bottom: 0.5em }
88 div.compound .compound-last, div.compound .compound-middle {
89 margin-top: 0.5em }
92 div.dedication {
93 margin: 2em 5em ;
94 text-align: center ;
95 font-style: italic }
97 div.dedication p.topic-title {
98 font-weight: bold ;
99 font-style: normal }
101 div.figure {
102 margin-left: 2em ;
103 margin-right: 2em }
105 div.footer, div.header {
106 clear: both;
107 font-size: smaller }
109 div.line-block {
110 display: block ;
111 margin-top: 1em ;
112 margin-bottom: 1em }
114 div.line-block div.line-block {
115 margin-top: 0 ;
116 margin-bottom: 0 ;
117 margin-left: 1.5em }
119 div.sidebar {
120 margin-left: 1em ;
121 border: medium outset ;
122 padding: 1em ;
123 background-color: #ffffee ;
124 width: 40% ;
125 float: right ;
126 clear: right }
128 div.sidebar p.rubric {
129 font-family: sans-serif ;
130 font-size: medium }
132 div.system-messages {
133 margin: 5em }
135 div.system-messages h1 {
136 color: red }
138 div.system-message {
139 border: medium outset ;
140 padding: 1em }
142 div.system-message p.system-message-title {
143 color: red ;
144 font-weight: bold }
146 div.topic {
147 margin: 2em }
149 h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
150 h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
151 margin-top: 0.4em }
153 h1.title {
154 text-align: center }
156 h2.subtitle {
157 text-align: center }
159 hr.docutils {
160 width: 75% }
162 img.align-left {
163 clear: left }
165 img.align-right {
166 clear: right }
168 ol.simple, ul.simple {
169 margin-bottom: 1em }
171 ol.arabic {
172 list-style: decimal }
174 ol.loweralpha {
175 list-style: lower-alpha }
177 ol.upperalpha {
178 list-style: upper-alpha }
180 ol.lowerroman {
181 list-style: lower-roman }
183 ol.upperroman {
184 list-style: upper-roman }
186 p.attribution {
187 text-align: right ;
188 margin-left: 50% }
190 p.caption {
191 font-style: italic }
193 p.credits {
194 font-style: italic ;
195 font-size: smaller }
197 p.label {
198 white-space: nowrap }
200 p.rubric {
201 font-weight: bold ;
202 font-size: larger ;
203 color: maroon ;
204 text-align: center }
206 p.sidebar-title {
207 font-family: sans-serif ;
208 font-weight: bold ;
209 font-size: larger }
211 p.sidebar-subtitle {
212 font-family: sans-serif ;
213 font-weight: bold }
215 p.topic-title {
216 font-weight: bold }
218 pre.address {
219 margin-bottom: 0 ;
220 margin-top: 0 ;
221 font-family: serif ;
222 font-size: 100% }
224 pre.literal-block, pre.doctest-block {
225 margin-left: 2em ;
226 margin-right: 2em ;
227 background-color: #eeeeee }
229 span.classifier {
230 font-family: sans-serif ;
231 font-style: oblique }
233 span.classifier-delimiter {
234 font-family: sans-serif ;
235 font-weight: bold }
237 span.interpreted {
238 font-family: sans-serif }
240 span.option {
241 white-space: nowrap }
243 span.pre {
244 white-space: pre }
246 span.problematic {
247 color: red }
249 span.section-subtitle {
250 /* font-size relative to parent (h1..h6 element) */
251 font-size: 80% }
253 table.citation {
254 border-left: solid 1px gray;
255 margin-left: 1px }
257 table.docinfo {
258 margin: 2em 4em }
260 table.docutils {
261 margin-top: 0.5em ;
262 margin-bottom: 0.5em }
264 table.footnote {
265 border-left: solid 1px black;
266 margin-left: 1px }
268 table.docutils td, table.docutils th,
269 table.docinfo td, table.docinfo th {
270 padding-left: 0.5em ;
271 padding-right: 0.5em ;
272 vertical-align: top }
274 table.docutils th.field-name, table.docinfo th.docinfo-name {
275 font-weight: bold ;
276 text-align: left ;
277 white-space: nowrap ;
278 padding-left: 0 }
280 h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
281 h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
282 font-size: 100% }
284 tt.docutils {
285 background-color: #eeeeee }
287 ul.auto-toc {
288 list-style-type: none }
290 </style>
291 </head>
292 <body>
293 <div class="document" id="test-the-iterator-wrappers-from-iterqueue-py">
294 <h1 class="title">Test the iterator wrappers from iterqueue.py</h1>
295 <!-- #!/usr/bin/env python
296 # -*- coding: iso-8859-1 -*- -->
297 <div class="contents topic">
298 <p class="topic-title first"><a id="contents" name="contents">Contents</a></p>
299 <ul class="auto-toc simple">
300 <li><a class="reference" href="#get-and-sort-the-wrapper-classes" id="id1" name="id1">1&nbsp;&nbsp;&nbsp;Get and sort the wrapper classes</a></li>
301 <li><a class="reference" href="#test-wrappers" id="id2" name="id2">2&nbsp;&nbsp;&nbsp;Test Wrappers</a></li>
302 <li><a class="reference" href="#test-peekables" id="id3" name="id3">3&nbsp;&nbsp;&nbsp;Test Peekables</a></li>
303 <li><a class="reference" href="#test-pushables" id="id4" name="id4">4&nbsp;&nbsp;&nbsp;Test Pushables</a></li>
304 <li><a class="reference" href="#test-iterator-queue" id="id5" name="id5">5&nbsp;&nbsp;&nbsp;Test Iterator Queue</a></li>
305 <li><a class="reference" href="#test-state-reporters" id="id6" name="id6">6&nbsp;&nbsp;&nbsp;Test State Reporters</a></li>
306 </ul>
307 </div>
308 <pre class="literal-block">
309 import sys, itertools
310 import iterqueue
311 from iterqueue import *
312 </pre>
313 <div class="section">
314 <h1><a class="toc-backref" href="#id1" id="get-and-sort-the-wrapper-classes" name="get-and-sort-the-wrapper-classes">1&nbsp;&nbsp;&nbsp;Get and sort the wrapper classes</a></h1>
315 <p>List all iterator wrapper objects:</p>
316 <pre class="literal-block">
317 wrappers = [obj for obj in iterqueue.__dict__.values()
318 if is_iterator_wrapper(obj)]
319 # print &quot;\n&quot;.join(repr(wrapper) for wrapper in wrappers)
320 </pre>
321 <p>List iterator wrappers that provide a <cite>peek</cite> method:</p>
322 <pre class="literal-block">
323 peekables = [obj for obj in wrappers if is_peekable(obj)]
324 # print &quot;Peekables&quot;
325 # print &quot;\n&quot;.join(repr(peekable) for peekable in peekables)
326 </pre>
327 <p>List iterator wrappers that provide a <cite>push</cite> method:</p>
328 <pre class="literal-block">
329 pushables = [obj for obj in wrappers if is_pushable(obj)]
330 # print &quot;Pushables&quot;
331 # print &quot;\n&quot;.join(repr(pushable) for pushable in pushables)
332 </pre>
333 <p>List iterator wrappers that provide a test for &quot;values available&quot;:</p>
334 <pre class="literal-block">
335 state_reporters = [obj for obj in wrappers if is_state_reporting(obj)]
336 # print &quot;State Reporters&quot;
337 # print &quot;\n&quot;.join(repr(state_reporter) for state_reporter in state_reporters)
338 </pre>
339 <p>List iterator wrappers that implement the &quot;queue&quot; methods:</p>
340 <pre class="literal-block">
341 iqueues = [obj for obj in wrappers if is_iterator_queue(obj)]
342 # print &quot;Iterator Queues&quot;
343 # print &quot;\n&quot;.join(repr(iqueue) for iqueue in iqueues)
344 </pre>
345 </div>
346 <div class="section">
347 <h1><a class="toc-backref" href="#id2" id="test-wrappers" name="test-wrappers">2&nbsp;&nbsp;&nbsp;Test Wrappers</a></h1>
348 <p>Test the basic iterator features of an iterator wrapper.</p>
349 <pre class="literal-block">
350 class Test_Wrappers:
351 &quot;&quot;&quot;Test the wrapping of iterator wrappers&quot;&quot;&quot;
352 def wrap_ok(self, wrapper, base):
353 &quot;&quot;&quot;iterating over the wrapper should return the same
354 as iterating over base
355 &quot;&quot;&quot;
356 print wrapper
357 assert list(wrapper(base)) == list(base)
358 assert [i for i in wrapper(base)] == [i for i in base]
360 def test_wrappers(self, base=xrange(3)):
361 for wrapper in wrappers:
362 yield self.wrap_ok, wrapper, base
363 </pre>
364 </div>
365 <div class="section">
366 <h1><a class="toc-backref" href="#id3" id="test-peekables" name="test-peekables">3&nbsp;&nbsp;&nbsp;Test Peekables</a></h1>
367 <pre class="literal-block">
368 class Test_Peekables:
369 &quot;&quot;&quot;Test the peek method of iterator wrappers&quot;&quot;&quot;
370 def peek_ok(self, wrapper, base):
371 &quot;&quot;&quot;peek() should return next value but not advance the iterator&quot;&quot;&quot;
372 print wrapper
373 print wrapper.peek
374 it = wrapper(base)
375 it.peek()
376 first = it.peek()
377 print first
378 assert first == 0
379 # peek() must not &quot;use up&quot; values
380 result = list(it)
381 print result
382 assert result == list(base)
384 def test_peekables(self, base=xrange(3)):
385 &quot;&quot;&quot;Test generator for peekable iterator wrappers&quot;&quot;&quot;
386 for wrapper in peekables:
387 yield self.peek_ok, wrapper, base
388 </pre>
389 </div>
390 <div class="section">
391 <h1><a class="toc-backref" href="#id4" id="test-pushables" name="test-pushables">4&nbsp;&nbsp;&nbsp;Test Pushables</a></h1>
392 <pre class="literal-block">
393 class Test_Pushables:
394 &quot;&quot;&quot;Test the push method of iterator wrappers&quot;&quot;&quot;
396 def push_ok(self, wrapper, base):
397 &quot;&quot;&quot;push(value) shall prepend `value` to iterator&quot;&quot;&quot;
398 print wrapper.push
399 it = wrapper(base)
400 it.push(9)
401 result = list(it)
402 print result
403 assert result == [9] + list(base)
405 def push_while_iterating_ok(self, wrapper):
406 &quot;&quot;&quot;push shall work even in an iteration loop&quot;&quot;&quot;
407 print wrapper
408 it = wrapper(xrange(3))
409 result = []
410 for i in it:
411 if i == 1:
412 it.push(&quot;xx&quot;)
413 result.append(i)
414 assert result == [0, 1, 'xx', 2]
416 def test_pushables(self, base=xrange(3)):
417 &quot;&quot;&quot;Test generator for pushable iterator wrappers&quot;&quot;&quot;
418 for wrapper in pushables:
419 if not hasattr(wrapper, &quot;push&quot;):
420 wrapper.push = wrapper.appendleft
421 yield self.push_ok, wrapper, base
422 yield self.push_while_iterating_ok, wrapper
423 </pre>
424 </div>
425 <div class="section">
426 <h1><a class="toc-backref" href="#id5" id="test-iterator-queue" name="test-iterator-queue">5&nbsp;&nbsp;&nbsp;Test Iterator Queue</a></h1>
427 <pre class="literal-block">
428 class TestIteratorQueue:
429 &quot;&quot;&quot;Test the queueing methods of iterator queues&quot;&quot;&quot;
431 def extend_ok(self, wrapper, base):
432 &quot;&quot;&quot;extend(iterable) shall append `iterable` to iterator&quot;&quot;&quot;
433 print wrapper
434 it = wrapper(base)
435 it.extend([9])
436 assert list(it) == list(base) + [9]
438 def extendleft_ok(self, wrapper, base):
439 &quot;&quot;&quot;extendleft(iterable) shall prepend `iterable` to iterator&quot;&quot;&quot;
440 print wrapper
441 it = wrapper(base)
442 it.extendleft([9])
443 result = [i for i in it]
444 print result
445 assert result == [9] + list(base)
447 def append_ok(self, wrapper, base):
448 &quot;&quot;&quot;append(value) shall append `value` to iterator&quot;&quot;&quot;
449 print wrapper
450 it = wrapper(base)
451 it.append(9)
452 result = list(it)
453 print result
454 assert result == list(base) + [9]
456 def test_iqueues(self, base=xrange(3)):
457 &quot;&quot;&quot;Test generator for iterator-queue wrappers&quot;&quot;&quot;
458 for wrapper in iqueues:
459 yield self.extend_ok, wrapper, base
460 yield self.extendleft_ok, wrapper, base
461 yield self.append_ok, wrapper, base
462 </pre>
463 </div>
464 <div class="section">
465 <h1><a class="toc-backref" href="#id6" id="test-state-reporters" name="test-state-reporters">6&nbsp;&nbsp;&nbsp;Test State Reporters</a></h1>
466 <pre class="literal-block">
467 class Test_StateReporters:
468 &quot;&quot;&quot;Test the state reporting when converted to bool&quot;&quot;&quot;
469 def bool_ok(self, wrapper):
470 &quot;&quot;&quot;Empty iterator should evaluate to False
471 Non-empty iterator should evaluate to True
472 the evaluation should not advance the iterator
473 &quot;&quot;&quot;
474 base = xrange(3) # make sure it is not empty!
475 it0 = wrapper([])
476 assert bool(it0) == False
477 assert list(it0) == []
478 it1 = wrapper(base)
479 assert bool(it1) == True
480 assert list(it1) == list(base)
481 assert bool(wrapper(iter([]))) == False
482 assert bool(wrapper(iter([1]))) == True
484 def test_iqueues(self):
485 &quot;&quot;&quot;Test generator for iterator-queue wrappers&quot;&quot;&quot;
486 for wrapper in state_reporters:
487 yield self.bool_ok, wrapper
490 if __name__ == &quot;__main__&quot;:
491 import nose
492 # this doesnot show any effect :-(
493 nose.configure([&quot;test.py&quot;, &quot;--detailed-errors&quot;])
494 nose.runmodule() # requires nose 0.9.1
495 </pre>
496 </div>
497 </div>
498 <div class="footer">
499 <hr class="footer" />
500 Generated on: 2007-02-22.
502 </div>
503 </body>
504 </html>