1
0
mirror of https://github.com/django/django.git synced 2024-12-24 01:55:49 +00:00

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.
This commit is contained in:
Claude Paroz 2012-05-18 14:52:24 +02:00
parent 45f55a9fcc
commit d04f72fb31
5 changed files with 69 additions and 29 deletions

View File

@ -8,6 +8,9 @@ See also http://www.aryehleib.com/MutableLists.html
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 @@ class ListMixin(object):
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 File

@ -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 @@ class GPolyline(GOverlayBase):
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 @@ class GIcon(object):
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 File

@ -38,6 +38,8 @@ and Geoff Biggs' PhD work on dimensioned units for robotics.
__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 @@ class MeasureBase(object):
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 @@ class Distance(MeasureBase):
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 cmp(self.m, other.m)
return self.m == other.m
else:
return NotImplemented
def __lt__(self, other):
if isinstance(other, Distance):
return self.m < other.m
else:
return NotImplemented
@ -244,6 +253,7 @@ class Distance(MeasureBase):
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 @@ class Area(MeasureBase):
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 cmp(self.sq_m, other.sq_m)
return self.sq_m == other.sq_m
else:
return NotImplemented
def __lt__(self, other):
if isinstance(other, Area):
return self.sq_m < other.sq_m
else:
return NotImplemented

View File

@ -155,12 +155,12 @@ class BoundMethodWeakref(object):
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 File

@ -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 lazy(func, *resultclasses):
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: