Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
@claudep claudep authored
View
29 django/contrib/gis/geos/mutable_list.py
@@ -8,6 +8,9 @@
Author: Aryeh Leib Taurog.
"""
+from django.utils.functional import total_ordering
+
+@total_ordering
class ListMixin(object):
"""
A base class which provides complete list interface.
@@ -143,20 +146,28 @@ def __imul__(self, n):
self.extend(cache)
return self
- def __cmp__(self, other):
- 'cmp'
+ def __eq__(self, other):
+ for i in range(len(self)):
+ try:
+ c = self[i] == other[i]
+ except IndexError:
+ # must be other is shorter
+ return False
+ if not c:
+ return False
+ return True
+
+ def __lt__(self, other):
slen = len(self)
for i in range(slen):
try:
- c = cmp(self[i], other[i])
+ c = self[i] < other[i]
except IndexError:
# must be other is shorter
- return 1
- else:
- # elements not equal
- if c: return c
-
- return cmp(slen, len(other))
+ return False
+ if c:
+ return c
+ return slen < len(other)
### Public list interface Methods ###
## Non-mutating ##
View
12 django/contrib/gis/maps/google/overlays.py
@@ -1,5 +1,7 @@
-from django.utils.safestring import mark_safe
from django.contrib.gis.geos import fromstr, Point, LineString, LinearRing, Polygon
+from django.utils.functional import total_ordering
+from django.utils.safestring import mark_safe
+
class GEvent(object):
"""
@@ -166,6 +168,7 @@ def js_params(self):
return '%s, "%s", %s, %s' % (self.latlngs, self.color, self.weight, self.opacity)
+@total_ordering
class GIcon(object):
"""
Creates a GIcon object to pass into a Gmarker object.
@@ -231,8 +234,11 @@ def __init__(self, varname, image=None, iconsize=None,
self.iconanchor = iconanchor
self.infowindowanchor = infowindowanchor
- def __cmp__(self, other):
- return cmp(self.varname, other.varname)
+ def __eq__(self, other):
+ return self.varname == other.varname
+
+ def __lt__(self, other):
+ return self.varname < other.varname
def __hash__(self):
# XOR with hash of GIcon type so that hash('varname') won't
View
24 django/contrib/gis/measure.py
@@ -38,6 +38,8 @@
__all__ = ['A', 'Area', 'D', 'Distance']
from decimal import Decimal
+from django.utils.functional import total_ordering
+
class MeasureBase(object):
def default_units(self, kwargs):
"""
@@ -84,6 +86,7 @@ def unit_attname(cls, unit_str):
else:
raise Exception('Could not find a unit keyword associated with "%s"' % unit_str)
+@total_ordering
class Distance(MeasureBase):
UNITS = {
'chain' : 20.1168,
@@ -178,9 +181,15 @@ def __repr__(self):
def __str__(self):
return '%s %s' % (getattr(self, self._default_unit), self._default_unit)
- def __cmp__(self, other):
+ def __eq__(self, other):
+ if isinstance(other, Distance):
+ return self.m == other.m
+ else:
+ return NotImplemented
+
+ def __lt__(self, other):
if isinstance(other, Distance):
- return cmp(self.m, other.m)
+ return self.m < other.m
else:
return NotImplemented
@@ -244,6 +253,7 @@ def __idiv__(self, other):
def __nonzero__(self):
return bool(self.m)
+@total_ordering
class Area(MeasureBase):
# Getting the square units values and the alias dictionary.
UNITS = dict([('sq_%s' % k, v ** 2) for k, v in Distance.UNITS.items()])
@@ -267,9 +277,15 @@ def __repr__(self):
def __str__(self):
return '%s %s' % (getattr(self, self._default_unit), self._default_unit)
- def __cmp__(self, other):
+ def __eq__(self, other):
+ if isinstance(other, Area):
+ return self.sq_m == other.sq_m
+ else:
+ return NotImplemented
+
+ def __lt__(self, other):
if isinstance(other, Area):
- return cmp(self.sq_m, other.sq_m)
+ return self.sq_m < other.sq_m
else:
return NotImplemented
View
10 django/dispatch/saferef.py
@@ -155,12 +155,12 @@ def __str__(self):
def __nonzero__( self ):
"""Whether we are still a valid reference"""
return self() is not None
-
- def __cmp__( self, other ):
+
+ def __eq__(self, other):
"""Compare with another reference"""
- if not isinstance (other,self.__class__):
- return cmp( self.__class__, type(other) )
- return cmp( self.key, other.key)
+ if not isinstance(other, self.__class__):
+ return self.__class__ == type(other)
+ return self.key == other.key
def __call__(self):
"""Return a strong reference to the bound method
View
23 django/utils/functional.py
@@ -58,6 +58,7 @@ def lazy(func, *resultclasses):
function is evaluated on every access.
"""
+ @total_ordering
class __proxy__(Promise):
"""
Encapsulate a function call and act as a proxy for methods that are
@@ -124,17 +125,23 @@ def __unicode_cast(self):
def __str_cast(self):
return str(func(*self.__args, **self.__kw))
- def __cmp__(self, rhs):
+ def __cast(self):
if self._delegate_str:
- s = str(func(*self.__args, **self.__kw))
+ return self.__str_cast()
elif self._delegate_unicode:
- s = unicode(func(*self.__args, **self.__kw))
+ return self.__unicode_cast()
else:
- s = func(*self.__args, **self.__kw)
- if isinstance(rhs, Promise):
- return -cmp(rhs, s)
- else:
- return cmp(s, rhs)
+ return func(*self.__args, **self.__kw)
+
+ def __eq__(self, other):
+ if isinstance(other, Promise):
+ other = other.__cast()
+ return self.__cast() == other
+
+ def __lt__(self, other):
+ if isinstance(other, Promise):
+ other = other.__cast()
+ return self.__cast() < other
def __mod__(self, rhs):
if self._delegate_str:
Please sign in to comment.
Something went wrong with that request. Please try again.