[Python-checkins] python/dist/src/Lib/test test_itertools.py, 1.18, 1.19

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Mon Sep 8 17:58:43 EDT 2003


Update of /cvsroot/python/python/dist/src/Lib/test
In directory sc8-pr-cvs1:/tmp/cvs-serv9975/Lib/test

Modified Files:
	test_itertools.py 
Log Message:
Add an example to address a common question of how to split iterators.



Index: test_itertools.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_itertools.py,v
retrieving revision 1.18
retrieving revision 1.19
diff -C2 -d -r1.18 -r1.19
*** test_itertools.py	29 Aug 2003 23:09:58 -0000	1.18
--- test_itertools.py	8 Sep 2003 23:58:40 -0000	1.19
***************
*** 488,491 ****
--- 488,494 ----
  Samuele
  
+ >>> def take(n, seq):
+ ...     return list(islice(seq, n))
+ 
  >>> def enumerate(iterable):
  ...     return izip(count(), iterable)
***************
*** 540,549 ****
  ...         yield result
  
! >>> def take(n, seq):
! ...     return list(islice(seq, n))
  
  This is not part of the examples but it tests to make sure the definitions
  perform as purported.
  
  >>> list(enumerate('abc'))
  [(0, 'a'), (1, 'b'), (2, 'c')]
--- 543,566 ----
  ...         yield result
  
! >>> def tee(iterable):
! ...     "Return two independent iterators from a single iterable"
! ...     def gen(next, data={}, cnt=[0]):
! ...         dpop = data.pop
! ...         for i in count():
! ...             if i == cnt[0]:
! ...                 item = data[i] = next()
! ...                 cnt[0] += 1
! ...             else:
! ...                 item = dpop(i)
! ...             yield item
! ...     next = iter(iterable).next
! ...     return (gen(next), gen(next))
  
  This is not part of the examples but it tests to make sure the definitions
  perform as purported.
  
+ >>> take(10, count())
+ [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
+ 
  >>> list(enumerate('abc'))
  [(0, 'a'), (1, 'b'), (2, 'c')]
***************
*** 591,596 ****
  32
  
! >>> take(10, count())
! [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  
  """
--- 608,622 ----
  32
  
! >>> def irange(start, stop):
! ...     for i in range(start, stop):
! ...         yield i
! 
! >>> x, y = tee(irange(2,10))
! >>> list(x), list(y)
! ([2, 3, 4, 5, 6, 7, 8, 9], [2, 3, 4, 5, 6, 7, 8, 9])
! 
! >>> x, y = tee(irange(2,10))
! >>> zip(x, y)
! [(2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8), (9, 9)]
  
  """





More information about the Python-checkins mailing list