catacomb/__init__.py: Implement `rich' comparisons on rationals.
[catacomb-python] / catacomb / __init__.py
index 24f3a61..508f1f8 100644 (file)
@@ -387,12 +387,15 @@ class BaseRat (object):
     return type(me)(me._d*n, me._n*d)
   __div__ = __truediv__
   __rdiv__ = __rtruediv__
-  def __cmp__(me, you):
+  def _order(me, you, op):
     n, d = _split_rat(you)
-    return cmp(me._n*d, n*me._d)
-  def __rcmp__(me, you):
-    n, d = _split_rat(you)
-    return cmp(n*me._d, me._n*d)
+    return op(me._n*d, n*me._d)
+  def __eq__(me, you): return me._order(you, lambda x, y: x == y)
+  def __ne__(me, you): return me._order(you, lambda x, y: x != y)
+  def __le__(me, you): return me._order(you, lambda x, y: x <= y)
+  def __lt__(me, you): return me._order(you, lambda x, y: x <  y)
+  def __gt__(me, you): return me._order(you, lambda x, y: x >  y)
+  def __ge__(me, you): return me._order(you, lambda x, y: x >= y)
 
 class IntRat (BaseRat):
   RING = MP