summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2002-08-25 18:43:10 (GMT)
committerTim Peters <tim.peters@gmail.com>2002-08-25 18:43:10 (GMT)
commitea76c98014e7f99974e3dcdbe168d45762bf627d (patch)
treef2729aa74a20f828711173c423fe1e44bdb70e62 /Lib
parent93d8d48c15981ffbf9314d4f2aeeff008c227eb9 (diff)
downloadcpython-ea76c98014e7f99974e3dcdbe168d45762bf627d.zip
cpython-ea76c98014e7f99974e3dcdbe168d45762bf627d.tar.gz
cpython-ea76c98014e7f99974e3dcdbe168d45762bf627d.tar.bz2
Implemented <, <=, >, >= for sets, giving subset and proper-subset
meanings. I did not add new, e.g., ispropersubset() methods; we're going nuts on those, and, e.g., there was no "friendly name" for == either.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/sets.py12
-rw-r--r--Lib/test/test_sets.py28
2 files changed, 33 insertions, 7 deletions
diff --git a/Lib/sets.py b/Lib/sets.py
index ff0ace0..3897fb9 100644
--- a/Lib/sets.py
+++ b/Lib/sets.py
@@ -275,6 +275,18 @@ class BaseSet(object):
return False
return True
+ # Inequality comparisons using the is-subset relation.
+ __le__ = issubset
+ __ge__ = issuperset
+
+ def __lt__(self, other):
+ self._binary_sanity_check(other)
+ return len(self) < len(other) and self.issubset(other)
+
+ def __gt__(self, other):
+ self._binary_sanity_check(other)
+ return len(self) > len(other) and self.issuperset(other)
+
# Assorted helpers
def _binary_sanity_check(self, other):
diff --git a/Lib/test/test_sets.py b/Lib/test/test_sets.py
index 15c52c8..840036c 100644
--- a/Lib/test/test_sets.py
+++ b/Lib/test/test_sets.py
@@ -364,23 +364,37 @@ class TestSubsets(unittest.TestCase):
case2method = {"<=": "issubset",
">=": "issuperset",
}
- cases_with_ops = Set(["==", "!="])
+
+ reverse = {"==": "==",
+ "!=": "!=",
+ "<": ">",
+ ">": "<",
+ "<=": ">=",
+ ">=": "<=",
+ }
def test_issubset(self):
x = self.left
y = self.right
for case in "!=", "==", "<", "<=", ">", ">=":
expected = case in self.cases
+ # Test the binary infix spelling.
+ result = eval("x" + case + "y", locals())
+ self.assertEqual(result, expected)
+ # Test the "friendly" method-name spelling, if one exists.
if case in TestSubsets.case2method:
- # Test the method-name spelling.
method = getattr(x, TestSubsets.case2method[case])
result = method(y)
self.assertEqual(result, expected)
- if case in TestSubsets.cases_with_ops:
- # Test the binary infix spelling.
- result = eval("x" + case + "y", locals())
- self.assertEqual(result, expected)
+ # Now do the same for the operands reversed.
+ rcase = TestSubsets.reverse[case]
+ result = eval("y" + rcase + "x", locals())
+ self.assertEqual(result, expected)
+ if rcase in TestSubsets.case2method:
+ method = getattr(y, TestSubsets.case2method[rcase])
+ result = method(x)
+ self.assertEqual(result, expected)
#------------------------------------------------------------------------------
class TestSubsetEqualEmpty(TestSubsets):
@@ -410,7 +424,7 @@ class TestSubsetEmptyNonEmpty(TestSubsets):
class TestSubsetPartial(TestSubsets):
left = Set([1])
right = Set([1, 2])
- name = "one a non-empty subset of other"
+ name = "one a non-empty proper subset of other"
cases = "!=", "<", "<="
#------------------------------------------------------------------------------