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

jlg at codespeak.net jlg at codespeak.net
Thu Jul 10 17:02:55 CEST 2008


Author: jlg
Date: Thu Jul 10 17:02:54 2008
New Revision: 56421

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

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 17:02:54 2008
@@ -8,6 +8,7 @@
         'count'     : 'interp_itertools.W_Count',
         'dropwhile' : 'interp_itertools.W_DropWhile',
         'ifilter'   : 'interp_itertools.W_IFilter',
+        'ifilterfalse' : 'interp_itertools.W_IFilterFalse',
         '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 17:02:54 2008
@@ -213,8 +213,7 @@
             yield x
     """)
 
-
-class W_IFilter(Wrappable):
+class _IFilterBase(Wrappable):
 
     def __init__(self, space, w_predicate, w_iterable):
         self.space = space
@@ -234,10 +233,15 @@
             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):
+            if self._call_predicate(w_obj):
                 return w_obj
 
+
+class W_IFilter(_IFilterBase):
+
+    def _call_predicate(self, w_obj):
+        return self.space.is_true(self.space.call_function(self.w_predicate, w_obj))
+
 def W_IFilter___new__(space, w_subtype, w_predicate, w_iterable):
     return space.wrap(W_IFilter(space, w_predicate, w_iterable))
 
@@ -259,3 +263,31 @@
             if predicate(x):
                 yield x
     """)
+
+class W_IFilterFalse(_IFilterBase):
+
+    def _call_predicate(self, w_obj):
+        return not self.space.is_true(self.space.call_function(self.w_predicate, w_obj))
+
+def W_IFilterFalse___new__(space, w_subtype, w_predicate, w_iterable):
+    return space.wrap(W_IFilterFalse(space, w_predicate, w_iterable))
+
+W_IFilterFalse.typedef = TypeDef(
+        'ifilterfalse',
+        __new__  = interp2app(W_IFilterFalse___new__, unwrap_spec=[ObjSpace, W_Root, W_Root, W_Root]),
+        __iter__ = interp2app(W_IFilterFalse.iter_w, unwrap_spec=['self']),
+        next     = interp2app(W_IFilterFalse.next_w, unwrap_spec=['self']),
+        __doc__  = """Make an iterator that filters elements from iterable returning
+    only those for which the predicate is False.  If predicate is
+    None, return the items that are false.
+
+    Equivalent to :
+    
+    def ifilterfalse(predicate, iterable):
+        if predicate is None:
+            predicate = bool
+        for x in iterable:
+            if not 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 17:02:54 2008
@@ -13,6 +13,7 @@
             itertools.takewhile(bool, []),
             itertools.dropwhile(bool, []),
             itertools.ifilter(None, []),
+            itertools.ifilterfalse(None, []),
             ]
 
         for it in iterables:
@@ -156,3 +157,31 @@
         raises(TypeError, it.next)
 
         raises(TypeError, itertools.ifilter, bool, None)
+
+    def test_ifilterfalse(self):
+        import itertools
+
+        it = itertools.ifilterfalse(None, [])
+        raises(StopIteration, it.next)
+
+        it = itertools.ifilterfalse(None, [1, 0, 2, 3, 0])
+        for x in [0, 0]:
+            assert it.next() == x
+        raises(StopIteration, it.next)
+
+        def is_odd(arg):
+            return (arg % 2 == 1)
+
+        it = itertools.ifilterfalse(is_odd, [1, 2, 3, 4, 5, 6])
+        for x in [2, 4, 6]:
+            assert it.next() == x
+        raises(StopIteration, it.next)
+
+    def test_ifilterfalse_wrongargs(self):
+        import itertools
+
+        it = itertools.ifilterfalse(0, [1])
+        raises(TypeError, it.next)
+
+        raises(TypeError, itertools.ifilterfalse, bool, None)
+



More information about the Pypy-commit mailing list