Commits

AJ R  committed d797125

Created class decorator for rich comparison operators, so Python 2 and 3 are nicely separated

  • Participants
  • Parent commits 4f4e5d2

Comments (0)

Files changed (2)

File savReaderWriter/py3k.py

                                 "and __str__ methods in Python 2 with __str__"
                                 " and __bytes__ methods, respectively, in "
                                 "Python 3")
+
+if isPy3k:
+    def rich_comparison(cls):
+        assert hasattr(cls, "__cmp__")
+        def __eq__(self, other):
+            return self.__cmp__(other) == 0
+        def __ne__(self, other):
+            return not self.__eq__(other)
+        def __le__(self, other):
+            return self.__cmp__(other) in (0, -1)
+        def __lt__(self, other):
+            return self.__cmp__(other) == -1
+        def __ge__(self, other):
+            return self.__eq__(other) and not self.__lt__(other)
+        def __gt__(self, other):
+            return not self.__eq__(other) and not self.__lt__(other)
+        for op in "__eq__ __ne__ __le__ __lt__ __ge__ __gt__".split():
+            setattr(cls, op, eval(op))
+        return cls
+else:
+    rich_comparison = lambda cls: cls
+rich_comparison.__doc__ = ("class decorator that implements rich comparison "
+                           "operators as this can not be done with __cmp__ in "
+                           "Python 3. Does nothing in Python 2.")
+
+

File savReaderWriter/savReader.py

 from savReaderWriter import *
 from header import *
 
+@rich_comparison
 @implements_to_string
 class SavReader(Header):
     """ Read Spss system files (.sav, .zsav)
         file. For example: len(SavReader(savFileName))"""
         return self.nCases
 
-    # the rich comparison operators --> Python 3 --> does not use __cmp__
-    def __eq__(self, other):
-        return self.__cmp__(other) == 0
-
-    def __ne__(self, other):
-        return not self.__eq__(other)
-
-    def __le__(self, other):
-        return self.__cmp__(other) in (0, -1)
-
-    def __lt__(self, other):
-        return self.__cmp__(other) == -1
-
-    def __ge__(self, other):
-        return self.__eq__(other) and not self.__lt__(other)
-
-    def __gt__(self, other):
-        return not self.__eq__(other) and not self.__lt__(other)
-
+    # Python 3: see @rich_comparison class decorator
     def __cmp__(self, other):
         """ This function implements behavior for all of the comparison
         operators so comparisons can be made between SavReader instances,