[pypy-svn] r56445 - in pypy/dist/pypy/module/itertools: . test

adurdin at codespeak.net adurdin at codespeak.net
Fri Jul 11 14:45:53 CEST 2008


Author: adurdin
Date: Fri Jul 11 14:45:51 2008
New Revision: 56445

Modified:
   pypy/dist/pypy/module/itertools/__init__.py
   pypy/dist/pypy/module/itertools/interp_itertools.py
   pypy/dist/pypy/module/itertools/test/test_itertools.py
Log:
(adurdin, jlg) interp_itertools - izip() implemented.


Modified: pypy/dist/pypy/module/itertools/__init__.py
==============================================================================
--- pypy/dist/pypy/module/itertools/__init__.py	(original)
+++ pypy/dist/pypy/module/itertools/__init__.py	Fri Jul 11 14:45:51 2008
@@ -32,6 +32,7 @@
         'ifilterfalse' : 'interp_itertools.W_IFilterFalse',
         'imap'      : 'interp_itertools.W_IMap',
         'islice'    : 'interp_itertools.W_ISlice',
+        'izip'      : 'interp_itertools.W_IZip',
         'repeat'    : 'interp_itertools.W_Repeat',
         'takewhile' : 'interp_itertools.W_TakeWhile',
     }

Modified: pypy/dist/pypy/module/itertools/interp_itertools.py
==============================================================================
--- pypy/dist/pypy/module/itertools/interp_itertools.py	(original)
+++ pypy/dist/pypy/module/itertools/interp_itertools.py	Fri Jul 11 14:45:51 2008
@@ -504,3 +504,33 @@
     """)
 W_IMap.typedef.acceptable_as_base_class = False
 
+
+class W_IZip(W_IMap):
+
+    def __init__(self, space, args_w):
+        super(W_IZip, self).__init__(space, space.w_None, args_w)
+
+
+def W_IZip___new__(space, w_subtype, args_w):
+    return space.wrap(W_IZip(space, args_w))
+
+W_IZip.typedef = TypeDef(
+        'izip',
+        __new__  = interp2app(W_IZip___new__, unwrap_spec=[ObjSpace, W_Root, 'args_w']),
+        __iter__ = interp2app(W_IZip.iter_w, unwrap_spec=['self']),
+        next     = interp2app(W_IZip.next_w, unwrap_spec=['self']),
+        __doc__  = """Make an iterator that aggregates elements from each of the
+    iterables.  Like zip() except that it returns an iterator instead
+    of a list. Used for lock-step iteration over several iterables at
+    a time.
+
+    Equivalent to :
+
+    def izip(*iterables):
+        iterables = map(iter, iterables)
+        while iterables:
+            result = [i.next() for i in iterables]
+            yield tuple(result)
+    """)
+W_IZip.typedef.acceptable_as_base_class = False
+

Modified: pypy/dist/pypy/module/itertools/test/test_itertools.py
==============================================================================
--- pypy/dist/pypy/module/itertools/test/test_itertools.py	(original)
+++ pypy/dist/pypy/module/itertools/test/test_itertools.py	Fri Jul 11 14:45:51 2008
@@ -17,6 +17,7 @@
             itertools.islice([], 0),
             itertools.chain(),
             itertools.imap(None),
+            itertools.izip(),
             ]
 
         for it in iterables:
@@ -308,6 +309,26 @@
             assert it.next() == x
         raises(StopIteration, it.next)
 
+    def test_izip(self):
+        import itertools
+
+        it = itertools.izip()
+        raises(StopIteration, it.next)
+
+        obj_list = [object(), object(), object()]
+        it = itertools.izip(obj_list)
+        for x in obj_list:
+            assert it.next() == (x, )
+        raises(StopIteration, it.next)
+        
+        it = itertools.izip([1, 2, 3], [4], [5, 6])
+        assert it.next() == (1, 4, 5)
+        raises(StopIteration, it.next)
+        
+        it = itertools.izip([], [], [1], [])
+        raises(StopIteration, it.next)
+        
+
     def test_docstrings(self):
         import itertools
         
@@ -322,6 +343,7 @@
             itertools.islice,
             itertools.chain,
             itertools.imap,
+            itertools.izip,
             ]
         for method in methods:
             assert method.__doc__
@@ -339,6 +361,7 @@
             (itertools.islice, ([], 0)),
             (itertools.chain, ()),
             (itertools.imap, (None,)),
+            (itertools.izip, ()),
             ]
         for cls, args in iterables:
             try:



More information about the Pypy-commit mailing list