Skip to content

Commit

Permalink
Got rid of old __cmp__methods replaced by rich comparison.
Browse files Browse the repository at this point in the history
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
claudep committed May 18, 2012
1 parent 45f55a9 commit d04f72f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 29 deletions.
29 changes: 20 additions & 9 deletions django/contrib/gis/geos/mutable_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 ##
Expand Down
12 changes: 9 additions & 3 deletions django/contrib/gis/maps/google/overlays.py
Original file line number Diff line number Diff line change
@@ -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):
"""
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
24 changes: 20 additions & 4 deletions django/contrib/gis/measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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()])
Expand All @@ -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

Expand Down
10 changes: 5 additions & 5 deletions django/dispatch/saferef.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 15 additions & 8 deletions django/utils/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit d04f72f

Please sign in to comment.