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">
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 {
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 }
33 /* Override more specific margin styles with
"! important". */
34 margin-top:
0 ! important }
36 .last, .with-subtitle {
37 margin-bottom:
0 ! important }
43 text-decoration: none ;
50 margin-bottom:
0.5em }
52 /* Uncomment (and remove this text!) to get bold-faced definition list terms
60 div.abstract p.topic-title {
64 div.admonition, div.attention, div.caution, div.danger, div.error,
65 div.hint, div.important, div.note, div.tip, div.warning {
67 border: medium outset ;
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 {
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 {
81 font-family: sans-serif }
83 /* Uncomment (and remove this text!) to get reduced vertical space in
85 div.compound .compound-first, div.compound .compound-middle {
86 margin-bottom:
0.5em }
88 div.compound .compound-last, div.compound .compound-middle {
97 div.dedication p.topic-title {
105 div.footer, div.header {
114 div.line-block div.line-block {
121 border: medium outset ;
123 background-color: #ffffee ;
128 div.sidebar p.rubric {
129 font-family: sans-serif ;
132 div.system-messages {
135 div.system-messages h1 {
139 border: medium outset ;
142 div.system-message p.system-message-title {
149 h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
150 h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
168 ol.simple, ul.simple {
172 list-style: decimal }
175 list-style: lower-alpha }
178 list-style: upper-alpha }
181 list-style: lower-roman }
184 list-style: upper-roman }
198 white-space: nowrap }
207 font-family: sans-serif ;
212 font-family: sans-serif ;
224 pre.literal-block, pre.doctest-block {
227 background-color: #eeeeee }
230 font-family: sans-serif ;
231 font-style: oblique }
233 span.classifier-delimiter {
234 font-family: sans-serif ;
238 font-family: sans-serif }
241 white-space: nowrap }
249 span.section-subtitle {
250 /* font-size relative to parent (h1..h6 element) */
254 border-left: solid
1px gray;
262 margin-bottom:
0.5em }
265 border-left: solid
1px black;
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 {
277 white-space: nowrap ;
280 h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
281 h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
285 background-color: #eeeeee }
288 list-style-type: none }
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 Get and sort the wrapper classes
</a></li>
301 <li><a class=
"reference" href=
"#test-wrappers" id=
"id2" name=
"id2">2 Test Wrappers
</a></li>
302 <li><a class=
"reference" href=
"#test-peekables" id=
"id3" name=
"id3">3 Test Peekables
</a></li>
303 <li><a class=
"reference" href=
"#test-pushables" id=
"id4" name=
"id4">4 Test Pushables
</a></li>
304 <li><a class=
"reference" href=
"#test-iterator-queue" id=
"id5" name=
"id5">5 Test Iterator Queue
</a></li>
305 <li><a class=
"reference" href=
"#test-state-reporters" id=
"id6" name=
"id6">6 Test State Reporters
</a></li>
308 <pre class=
"literal-block">
309 import sys, itertools
311 from iterqueue import *
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 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
"\n
".join(repr(wrapper) for wrapper in wrappers)
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
"Peekables
"
325 # print
"\n
".join(repr(peekable) for peekable in peekables)
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
"Pushables
"
331 # print
"\n
".join(repr(pushable) for pushable in pushables)
333 <p>List iterator wrappers that provide a test for
"values available
":
</p>
334 <pre class=
"literal-block">
335 state_reporters = [obj for obj in wrappers if is_state_reporting(obj)]
336 # print
"State Reporters
"
337 # print
"\n
".join(repr(state_reporter) for state_reporter in state_reporters)
339 <p>List iterator wrappers that implement the
"queue
" methods:
</p>
340 <pre class=
"literal-block">
341 iqueues = [obj for obj in wrappers if is_iterator_queue(obj)]
342 # print
"Iterator Queues
"
343 # print
"\n
".join(repr(iqueue) for iqueue in iqueues)
346 <div class=
"section">
347 <h1><a class=
"toc-backref" href=
"#id2" id=
"test-wrappers" name=
"test-wrappers">2 Test Wrappers
</a></h1>
348 <p>Test the basic iterator features of an iterator wrapper.
</p>
349 <pre class=
"literal-block">
351 """Test the wrapping of iterator wrappers
"""
352 def wrap_ok(self, wrapper, base):
353 """iterating over the wrapper should return the same
354 as iterating over base
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
365 <div class=
"section">
366 <h1><a class=
"toc-backref" href=
"#id3" id=
"test-peekables" name=
"test-peekables">3 Test Peekables
</a></h1>
367 <pre class=
"literal-block">
368 class Test_Peekables:
369 """Test the peek method of iterator wrappers
"""
370 def peek_ok(self, wrapper, base):
371 """peek() should return next value but not advance the iterator
"""
379 # peek() must not
"use up
" values
382 assert result == list(base)
384 def test_peekables(self, base=xrange(
3)):
385 """Test generator for peekable iterator wrappers
"""
386 for wrapper in peekables:
387 yield self.peek_ok, wrapper, base
390 <div class=
"section">
391 <h1><a class=
"toc-backref" href=
"#id4" id=
"test-pushables" name=
"test-pushables">4 Test Pushables
</a></h1>
392 <pre class=
"literal-block">
393 class Test_Pushables:
394 """Test the push method of iterator wrappers
"""
396 def push_ok(self, wrapper, base):
397 """push(value) shall prepend `value` to iterator
"""
403 assert result == [
9] + list(base)
405 def push_while_iterating_ok(self, wrapper):
406 """push shall work even in an iteration loop
"""
408 it = wrapper(xrange(
3))
412 it.push(
"xx
")
414 assert result == [
0,
1, 'xx',
2]
416 def test_pushables(self, base=xrange(
3)):
417 """Test generator for pushable iterator wrappers
"""
418 for wrapper in pushables:
419 if not hasattr(wrapper,
"push
"):
420 wrapper.push = wrapper.appendleft
421 yield self.push_ok, wrapper, base
422 yield self.push_while_iterating_ok, wrapper
425 <div class=
"section">
426 <h1><a class=
"toc-backref" href=
"#id5" id=
"test-iterator-queue" name=
"test-iterator-queue">5 Test Iterator Queue
</a></h1>
427 <pre class=
"literal-block">
428 class TestIteratorQueue:
429 """Test the queueing methods of iterator queues
"""
431 def extend_ok(self, wrapper, base):
432 """extend(iterable) shall append `iterable` to iterator
"""
436 assert list(it) == list(base) + [
9]
438 def extendleft_ok(self, wrapper, base):
439 """extendleft(iterable) shall prepend `iterable` to iterator
"""
443 result = [i for i in it]
445 assert result == [
9] + list(base)
447 def append_ok(self, wrapper, base):
448 """append(value) shall append `value` to iterator
"""
454 assert result == list(base) + [
9]
456 def test_iqueues(self, base=xrange(
3)):
457 """Test generator for iterator-queue wrappers
"""
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
464 <div class=
"section">
465 <h1><a class=
"toc-backref" href=
"#id6" id=
"test-state-reporters" name=
"test-state-reporters">6 Test State Reporters
</a></h1>
466 <pre class=
"literal-block">
467 class Test_StateReporters:
468 """Test the state reporting when converted to bool
"""
469 def bool_ok(self, wrapper):
470 """Empty iterator should evaluate to False
471 Non-empty iterator should evaluate to True
472 the evaluation should not advance the iterator
474 base = xrange(
3) # make sure it is not empty!
476 assert bool(it0) == False
477 assert list(it0) == []
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 """Test generator for iterator-queue wrappers
"""
486 for wrapper in state_reporters:
487 yield self.bool_ok, wrapper
490 if __name__ ==
"__main__
":
492 # this doesnot show any effect :-(
493 nose.configure([
"test.py
",
"--detailed-errors
"])
494 nose.runmodule() # requires nose
0.9.1
499 <hr class=
"footer" />
500 Generated on:
2007-
02-
22.