Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Got rid of old __cmp__methods replaced by rich comparison.

The __cmp__ methods are unsupported in Python 3.
_doctest.py has been left untouched because it is likely it will
not be migrated to Python 3.
  • Loading branch information...
commit d04f72fb31bab43386e12963c1c6dec23ae16143 1 parent 45f55a9
Claude Paroz authored May 18, 2012
29  django/contrib/gis/geos/mutable_list.py
@@ -8,6 +8,9 @@
8 8
 
9 9
 Author: Aryeh Leib Taurog.
10 10
 """
  11
+from django.utils.functional import total_ordering
  12
+
  13
+@total_ordering
11 14
 class ListMixin(object):
12 15
     """
13 16
     A base class which provides complete list interface.
@@ -143,20 +146,28 @@ def __imul__(self, n):
143 146
                 self.extend(cache)
144 147
         return self
145 148
 
146  
-    def __cmp__(self, other):
147  
-        'cmp'
  149
+    def __eq__(self, other):
  150
+        for i in range(len(self)):
  151
+            try:
  152
+                c = self[i] == other[i]
  153
+            except IndexError:
  154
+                # must be other is shorter
  155
+                return False
  156
+            if not c:
  157
+                return False
  158
+        return True
  159
+
  160
+    def __lt__(self, other):
148 161
         slen = len(self)
149 162
         for i in range(slen):
150 163
             try:
151  
-                c = cmp(self[i], other[i])
  164
+                c = self[i] < other[i]
152 165
             except IndexError:
153 166
                 # must be other is shorter
154  
-                return 1
155  
-            else:
156  
-                # elements not equal
157  
-                if c: return c
158  
-
159  
-        return cmp(slen, len(other))
  167
+                return False
  168
+            if c:
  169
+                return c
  170
+        return slen < len(other)
160 171
 
161 172
     ### Public list interface Methods ###
162 173
     ## Non-mutating ##
12  django/contrib/gis/maps/google/overlays.py
... ...
@@ -1,5 +1,7 @@
1  
-from django.utils.safestring import mark_safe
2 1
 from django.contrib.gis.geos import fromstr, Point, LineString, LinearRing, Polygon
  2
+from django.utils.functional import total_ordering
  3
+from django.utils.safestring import mark_safe
  4
+
3 5
 
4 6
 class GEvent(object):
5 7
     """
@@ -166,6 +168,7 @@ def js_params(self):
166 168
         return '%s, "%s", %s, %s' % (self.latlngs, self.color, self.weight, self.opacity)
167 169
 
168 170
 
  171
+@total_ordering
169 172
 class GIcon(object):
170 173
     """
171 174
     Creates a GIcon object to pass into a Gmarker object.
@@ -231,8 +234,11 @@ def __init__(self, varname, image=None, iconsize=None,
231 234
         self.iconanchor = iconanchor
232 235
         self.infowindowanchor = infowindowanchor
233 236
 
234  
-    def __cmp__(self, other):
235  
-        return cmp(self.varname, other.varname)
  237
+    def __eq__(self, other):
  238
+        return self.varname == other.varname
  239
+
  240
+    def __lt__(self, other):
  241
+        return self.varname < other.varname
236 242
     
237 243
     def __hash__(self):
238 244
         # XOR with hash of GIcon type so that hash('varname') won't 
24  django/contrib/gis/measure.py
@@ -38,6 +38,8 @@
38 38
 __all__ = ['A', 'Area', 'D', 'Distance']
39 39
 from decimal import Decimal
40 40
 
  41
+from django.utils.functional import total_ordering
  42
+
41 43
 class MeasureBase(object):
42 44
     def default_units(self, kwargs):
43 45
         """
@@ -84,6 +86,7 @@ def unit_attname(cls, unit_str):
84 86
         else:
85 87
             raise Exception('Could not find a unit keyword associated with "%s"' % unit_str)
86 88
 
  89
+@total_ordering
87 90
 class Distance(MeasureBase):
88 91
     UNITS = {
89 92
         'chain' : 20.1168,
@@ -178,9 +181,15 @@ def __repr__(self):
178 181
     def __str__(self):
179 182
         return '%s %s' % (getattr(self, self._default_unit), self._default_unit)
180 183
 
181  
-    def __cmp__(self, other):
  184
+    def __eq__(self, other):
  185
+        if isinstance(other, Distance):
  186
+            return self.m == other.m
  187
+        else:
  188
+            return NotImplemented
  189
+
  190
+    def __lt__(self, other):
182 191
         if isinstance(other, Distance):
183  
-            return cmp(self.m, other.m)
  192
+            return self.m < other.m
184 193
         else:
185 194
             return NotImplemented
186 195
 
@@ -244,6 +253,7 @@ def __idiv__(self, other):
244 253
     def __nonzero__(self):
245 254
         return bool(self.m)
246 255
 
  256
+@total_ordering
247 257
 class Area(MeasureBase):
248 258
     # Getting the square units values and the alias dictionary.
249 259
     UNITS = dict([('sq_%s' % k, v ** 2) for k, v in Distance.UNITS.items()])
@@ -267,9 +277,15 @@ def __repr__(self):
267 277
     def __str__(self):
268 278
         return '%s %s' % (getattr(self, self._default_unit), self._default_unit)
269 279
 
270  
-    def __cmp__(self, other):
  280
+    def __eq__(self, other):
  281
+        if isinstance(other, Area):
  282
+            return self.sq_m == other.sq_m
  283
+        else:
  284
+            return NotImplemented
  285
+
  286
+    def __lt__(self, other):
271 287
         if isinstance(other, Area):
272  
-            return cmp(self.sq_m, other.sq_m)
  288
+            return self.sq_m < other.sq_m
273 289
         else:
274 290
             return NotImplemented
275 291
 
10  django/dispatch/saferef.py
@@ -155,12 +155,12 @@ def __str__(self):
155 155
     def __nonzero__( self ):
156 156
         """Whether we are still a valid reference"""
157 157
         return self() is not None
158  
-    
159  
-    def __cmp__( self, other ):
  158
+
  159
+    def __eq__(self, other):
160 160
         """Compare with another reference"""
161  
-        if not isinstance (other,self.__class__):
162  
-            return cmp( self.__class__, type(other) )
163  
-        return cmp( self.key, other.key)
  161
+        if not isinstance(other, self.__class__):
  162
+            return self.__class__ == type(other)
  163
+        return self.key == other.key
164 164
     
165 165
     def __call__(self):
166 166
         """Return a strong reference to the bound method
23  django/utils/functional.py
@@ -58,6 +58,7 @@ def lazy(func, *resultclasses):
58 58
     function is evaluated on every access.
59 59
     """
60 60
 
  61
+    @total_ordering
61 62
     class __proxy__(Promise):
62 63
         """
63 64
         Encapsulate a function call and act as a proxy for methods that are
@@ -124,17 +125,23 @@ def __unicode_cast(self):
124 125
         def __str_cast(self):
125 126
             return str(func(*self.__args, **self.__kw))
126 127
 
127  
-        def __cmp__(self, rhs):
  128
+        def __cast(self):
128 129
             if self._delegate_str:
129  
-                s = str(func(*self.__args, **self.__kw))
  130
+                return self.__str_cast()
130 131
             elif self._delegate_unicode:
131  
-                s = unicode(func(*self.__args, **self.__kw))
  132
+                return self.__unicode_cast()
132 133
             else:
133  
-                s = func(*self.__args, **self.__kw)
134  
-            if isinstance(rhs, Promise):
135  
-                return -cmp(rhs, s)
136  
-            else:
137  
-                return cmp(s, rhs)
  134
+                return func(*self.__args, **self.__kw)
  135
+
  136
+        def __eq__(self, other):
  137
+            if isinstance(other, Promise):
  138
+                other = other.__cast()
  139
+            return self.__cast() == other
  140
+
  141
+        def __lt__(self, other):
  142
+            if isinstance(other, Promise):
  143
+                other = other.__cast()
  144
+            return self.__cast() < other
138 145
 
139 146
         def __mod__(self, rhs):
140 147
             if self._delegate_str:

0 notes on commit d04f72f

Please sign in to comment.
Something went wrong with that request. Please try again.