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

adurdin at codespeak.net adurdin at codespeak.net
Thu Jul 10 16:47:28 CEST 2008


Author: adurdin
Date: Thu Jul 10 16:47:27 2008
New Revision: 56419

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 - implemented ifilter

Modified: pypy/dist/pypy/module/itertools/__init__.py
==============================================================================
--- pypy/dist/pypy/module/itertools/__init__.py	(original)
+++ pypy/dist/pypy/module/itertools/__init__.py	Thu Jul 10 16:47:27 2008
@@ -7,6 +7,7 @@
     interpleveldefs = {
         'count'     : 'interp_itertools.W_Count',
         'dropwhile' : 'interp_itertools.W_DropWhile',
+        'ifilter'   : 'interp_itertools.W_IFilter',
         '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	Thu Jul 10 16:47:27 2008
@@ -196,7 +196,7 @@
         __new__  = interp2app(W_DropWhile___new__, unwrap_spec=[ObjSpace, W_Root, W_Root, W_Root]),
         __iter__ = interp2app(W_DropWhile.iter_w, unwrap_spec=['self']),
         next     = interp2app(W_DropWhile.next_w, unwrap_spec=['self']),
-        __doc__  =     """Make an iterator that drops elements from the iterable as long
+        __doc__  = """Make an iterator that drops elements from the iterable as long
     as the predicate is true; afterwards, returns every
     element. Note, the iterator does not produce any output until the
     predicate is true, so it may have a lengthy start-up time.
@@ -212,3 +212,50 @@
         for x in iterable:
             yield x
     """)
+
+
+class W_IFilter(Wrappable):
+
+    def __init__(self, space, w_predicate, w_iterable):
+        self.space = space
+        if space.is_w(w_predicate, space.w_None):
+            self.w_predicate = space.w_bool
+        else:
+            self.w_predicate = w_predicate
+        self.iterable = space.iter(w_iterable)
+
+    def iter_w(self):
+        return self.space.wrap(self)
+
+    def next_w(self):
+        while True:
+            try:
+                w_obj = self.space.next(self.iterable)
+            except StopIteration:
+                raise OperationError(self.space.w_StopIteration, self.space.w_None)
+
+            w_bool = self.space.call_function(self.w_predicate, w_obj)
+            if self.space.is_true(w_bool):
+                return w_obj
+
+def W_IFilter___new__(space, w_subtype, w_predicate, w_iterable):
+    return space.wrap(W_IFilter(space, w_predicate, w_iterable))
+
+W_IFilter.typedef = TypeDef(
+        'ifilter',
+        __new__  = interp2app(W_IFilter___new__, unwrap_spec=[ObjSpace, W_Root, W_Root, W_Root]),
+        __iter__ = interp2app(W_IFilter.iter_w, unwrap_spec=['self']),
+        next     = interp2app(W_IFilter.next_w, unwrap_spec=['self']),
+        __doc__  = """Make an iterator that filters elements from iterable returning
+    only those for which the predicate is True.  If predicate is
+    None, return the items that are true.
+
+    Equivalent to :
+
+    def ifilter:
+        if predicate is None:
+            predicate = bool
+        for x in iterable:
+            if predicate(x):
+                yield x
+    """)

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	Thu Jul 10 16:47:27 2008
@@ -12,6 +12,7 @@
             itertools.repeat(None),
             itertools.takewhile(bool, []),
             itertools.dropwhile(bool, []),
+            itertools.ifilter(None, []),
             ]
 
         for it in iterables:
@@ -128,3 +129,30 @@
         raises(TypeError, it.next)
 
         raises(TypeError, itertools.dropwhile, bool, None)
+
+    def test_ifilter(self):
+        import itertools
+
+        it = itertools.ifilter(None, [])
+        raises(StopIteration, it.next)
+
+        it = itertools.ifilter(None, [1, 0, 2, 3, 0])
+        for x in [1, 2, 3]:
+            assert it.next() == x
+        raises(StopIteration, it.next)
+
+        def is_odd(arg):
+            return (arg % 2 == 1)
+
+        it = itertools.ifilter(is_odd, [1, 2, 3, 4, 5, 6])
+        for x in [1, 3, 5]:
+            assert it.next() == x
+        raises(StopIteration, it.next)
+
+    def test_ifilter_wrongargs(self):
+        import itertools
+
+        it = itertools.ifilter(0, [1])
+        raises(TypeError, it.next)
+
+        raises(TypeError, itertools.ifilter, bool, None)



More information about the Pypy-commit mailing list