summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/sets.py12
-rw-r--r--Lib/test/test_itertools.py25
2 files changed, 28 insertions, 9 deletions
diff --git a/Lib/sets.py b/Lib/sets.py
index 4f55515..9604249 100644
--- a/Lib/sets.py
+++ b/Lib/sets.py
@@ -57,7 +57,7 @@ what's tested is actually `z in y'.
__all__ = ['BaseSet', 'Set', 'ImmutableSet']
-from itertools import ifilter
+from itertools import ifilter, ifilterfalse
class BaseSet(object):
"""Common base class for mutable and immutable sets."""
@@ -204,9 +204,9 @@ class BaseSet(object):
value = True
selfdata = self._data
otherdata = other._data
- for elt in ifilter(otherdata.has_key, selfdata, True):
+ for elt in ifilterfalse(otherdata.has_key, selfdata):
data[elt] = value
- for elt in ifilter(selfdata.has_key, otherdata, True):
+ for elt in ifilterfalse(selfdata.has_key, otherdata):
data[elt] = value
return result
@@ -227,7 +227,7 @@ class BaseSet(object):
result = self.__class__()
data = result._data
value = True
- for elt in ifilter(other._data.has_key, self, True):
+ for elt in ifilterfalse(other._data.has_key, self):
data[elt] = value
return result
@@ -260,7 +260,7 @@ class BaseSet(object):
self._binary_sanity_check(other)
if len(self) > len(other): # Fast check for obvious cases
return False
- for elt in ifilter(other._data.has_key, self, True):
+ for elt in ifilterfalse(other._data.has_key, self):
return False
return True
@@ -269,7 +269,7 @@ class BaseSet(object):
self._binary_sanity_check(other)
if len(self) < len(other): # Fast check for obvious cases
return False
- for elt in ifilter(self._data.has_key, other, True):
+ for elt in ifilterfalse(self._data.has_key, other):
return False
return True
diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py
index cef1718..09b7d13 100644
--- a/Lib/test/test_itertools.py
+++ b/Lib/test/test_itertools.py
@@ -13,12 +13,19 @@ class TestBasicOps(unittest.TestCase):
def isEven(x):
return x%2==0
self.assertEqual(list(ifilter(isEven, range(6))), [0,2,4])
- self.assertEqual(list(ifilter(isEven, range(6), True)), [1,3,5])
self.assertEqual(list(ifilter(None, [0,1,0,2,0])), [1,2])
self.assertRaises(TypeError, ifilter)
self.assertRaises(TypeError, ifilter, 3)
self.assertRaises(TypeError, ifilter, isEven, 3)
- self.assertRaises(TypeError, ifilter, isEven, [3], True, 4)
+
+ def test_ifilterfalse(self):
+ def isEven(x):
+ return x%2==0
+ self.assertEqual(list(ifilterfalse(isEven, range(6))), [1,3,5])
+ self.assertEqual(list(ifilterfalse(None, [0,1,0,2,0])), [0,0,0])
+ self.assertRaises(TypeError, ifilterfalse)
+ self.assertRaises(TypeError, ifilterfalse, 3)
+ self.assertRaises(TypeError, ifilterfalse, isEven, 3)
def test_izip(self):
ans = [(x,y) for x, y in izip('abc',count())]
@@ -133,7 +140,19 @@ Samuele
>>> def nth(iterable, n):
... "Returns the nth item"
-... return islice(iterable, n, n+1).next()
+... return list(islice(iterable, n, n+1))
+
+>>> def all(pred, seq):
+... "Returns True if pred(x) is True for every element in the iterable"
+... return not nth(ifilterfalse(pred, seq), 0)
+
+>>> def some(pred, seq):
+... "Returns True if pred(x) is True at least one element in the iterable"
+... return bool(nth(ifilter(pred, seq), 0))
+
+>>> def no(pred, seq):
+... "Returns True if pred(x) is False for every element in the iterable"
+... return not nth(ifilter(pred, seq), 0)
"""