# [pypy-svn] pypy collections-module: Comparison.

Tue Feb 15 16:02:51 CET 2011

```Author: Armin Rigo <arigo at tunes.org>
Branch: collections-module
Changeset: r41967:68683618f8b6
Date: 2011-02-15 14:53 +0100
http://bitbucket.org/pypy/pypy/changeset/68683618f8b6/

Log:	Comparison.

diff --git a/pypy/module/_collections/test/test_deque.py b/pypy/module/_collections/test/test_deque.py
--- a/pypy/module/_collections/test/test_deque.py
+++ b/pypy/module/_collections/test/test_deque.py
@@ -93,21 +93,22 @@
raises(RuntimeError, d.count, 3)

def test_comparisons(self):
+        from _collections import deque
d = deque('xabc'); d.popleft()
for e in [d, deque('abc'), deque('ab'), deque(), list(d)]:
-            self.assertEqual(d==e, type(d)==type(e) and list(d)==list(e))
-            self.assertEqual(d!=e, not(type(d)==type(e) and list(d)==list(e)))
+            assert (d==e) == (type(d)==type(e) and list(d)==list(e))
+            assert (d!=e) == (not(type(d)==type(e) and list(d)==list(e)))

args = map(deque, ('', 'a', 'b', 'ab', 'ba', 'abc', 'xba', 'xabc', 'cba'))
for x in args:
for y in args:
-                self.assertEqual(x == y, list(x) == list(y), (x,y))
-                self.assertEqual(x != y, list(x) != list(y), (x,y))
-                self.assertEqual(x <  y, list(x) <  list(y), (x,y))
-                self.assertEqual(x <= y, list(x) <= list(y), (x,y))
-                self.assertEqual(x >  y, list(x) >  list(y), (x,y))
-                self.assertEqual(x >= y, list(x) >= list(y), (x,y))
-                self.assertEqual(cmp(x,y), cmp(list(x),list(y)), (x,y))
+                assert (x == y) == (list(x) == list(y))
+                assert (x != y) == (list(x) != list(y))
+                assert (x <  y) == (list(x) <  list(y))
+                assert (x <= y) == (list(x) <= list(y))
+                assert (x >  y) == (list(x) >  list(y))
+                assert (x >= y) == (list(x) >= list(y))
+                assert cmp(x,y) == cmp(list(x),list(y))

def test_extend(self):
d = deque('a')

diff --git a/pypy/module/_collections/interp_deque.py b/pypy/module/_collections/interp_deque.py
--- a/pypy/module/_collections/interp_deque.py
+++ b/pypy/module/_collections/interp_deque.py
@@ -251,6 +251,32 @@
w_currently_in_repr = ec._py_repr = space.newdict()
return dequerepr(space, w_currently_in_repr, space.wrap(self))

+    def compare(self, w_other, op):
+        space = self.space
+        if not isinstance(space.interpclass_w(w_other), W_Deque):
+            return space.w_NotImplemented
+        return space.compare_by_iteration(space.wrap(self), w_other, op)
+    compare._annspecialcase_ = 'specialize:arg(2)'
+
+    @unwrap_spec('self', W_Root)
+    def lt(self, w_other):
+        return self.compare(w_other, 'lt')
+    @unwrap_spec('self', W_Root)
+    def le(self, w_other):
+        return self.compare(w_other, 'le')
+    @unwrap_spec('self', W_Root)
+    def eq(self, w_other):
+        return self.compare(w_other, 'eq')
+    @unwrap_spec('self', W_Root)
+    def ne(self, w_other):
+        return self.compare(w_other, 'ne')
+    @unwrap_spec('self', W_Root)
+    def gt(self, w_other):
+        return self.compare(w_other, 'gt')
+    @unwrap_spec('self', W_Root)
+    def ge(self, w_other):
+        return self.compare(w_other, 'ge')
+
def get_maxlen(space, self):
if self.maxlen == sys.maxint:
return self.space.w_None
@@ -304,6 +330,12 @@
__iter__ = interp2app(W_Deque.iter),
__len__ = interp2app(W_Deque.length),
__repr__ = interp2app(W_Deque.repr),
+    __lt__ = interp2app(W_Deque.lt),
+    __le__ = interp2app(W_Deque.le),
+    __eq__ = interp2app(W_Deque.eq),
+    __ne__ = interp2app(W_Deque.ne),
+    __gt__ = interp2app(W_Deque.gt),
+    __ge__ = interp2app(W_Deque.ge),
maxlen = GetSetProperty(W_Deque.get_maxlen),
)

```