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:
parent
45f55a9fcc
commit
d04f72fb31
|
@ -8,6 +8,9 @@ See also http://www.aryehleib.com/MutableLists.html
|
||||||
|
|
||||||
Author: Aryeh Leib Taurog.
|
Author: Aryeh Leib Taurog.
|
||||||
"""
|
"""
|
||||||
|
from django.utils.functional import total_ordering
|
||||||
|
|
||||||
|
@total_ordering
|
||||||
class ListMixin(object):
|
class ListMixin(object):
|
||||||
"""
|
"""
|
||||||
A base class which provides complete list interface.
|
A base class which provides complete list interface.
|
||||||
|
@ -143,20 +146,28 @@ class ListMixin(object):
|
||||||
self.extend(cache)
|
self.extend(cache)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
'cmp'
|
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)
|
slen = len(self)
|
||||||
for i in range(slen):
|
for i in range(slen):
|
||||||
try:
|
try:
|
||||||
c = cmp(self[i], other[i])
|
c = self[i] < other[i]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
# must be other is shorter
|
# must be other is shorter
|
||||||
return 1
|
return False
|
||||||
else:
|
if c:
|
||||||
# elements not equal
|
return c
|
||||||
if c: return c
|
return slen < len(other)
|
||||||
|
|
||||||
return cmp(slen, len(other))
|
|
||||||
|
|
||||||
### Public list interface Methods ###
|
### Public list interface Methods ###
|
||||||
## Non-mutating ##
|
## Non-mutating ##
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
from django.utils.safestring import mark_safe
|
|
||||||
from django.contrib.gis.geos import fromstr, Point, LineString, LinearRing, Polygon
|
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):
|
class GEvent(object):
|
||||||
"""
|
"""
|
||||||
|
@ -166,6 +168,7 @@ class GPolyline(GOverlayBase):
|
||||||
return '%s, "%s", %s, %s' % (self.latlngs, self.color, self.weight, self.opacity)
|
return '%s, "%s", %s, %s' % (self.latlngs, self.color, self.weight, self.opacity)
|
||||||
|
|
||||||
|
|
||||||
|
@total_ordering
|
||||||
class GIcon(object):
|
class GIcon(object):
|
||||||
"""
|
"""
|
||||||
Creates a GIcon object to pass into a Gmarker object.
|
Creates a GIcon object to pass into a Gmarker object.
|
||||||
|
@ -231,8 +234,11 @@ class GIcon(object):
|
||||||
self.iconanchor = iconanchor
|
self.iconanchor = iconanchor
|
||||||
self.infowindowanchor = infowindowanchor
|
self.infowindowanchor = infowindowanchor
|
||||||
|
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
return cmp(self.varname, other.varname)
|
return self.varname == other.varname
|
||||||
|
|
||||||
|
def __lt__(self, other):
|
||||||
|
return self.varname < other.varname
|
||||||
|
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
# XOR with hash of GIcon type so that hash('varname') won't
|
# XOR with hash of GIcon type so that hash('varname') won't
|
||||||
|
|
|
@ -38,6 +38,8 @@ and Geoff Biggs' PhD work on dimensioned units for robotics.
|
||||||
__all__ = ['A', 'Area', 'D', 'Distance']
|
__all__ = ['A', 'Area', 'D', 'Distance']
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
|
||||||
|
from django.utils.functional import total_ordering
|
||||||
|
|
||||||
class MeasureBase(object):
|
class MeasureBase(object):
|
||||||
def default_units(self, kwargs):
|
def default_units(self, kwargs):
|
||||||
"""
|
"""
|
||||||
|
@ -84,6 +86,7 @@ class MeasureBase(object):
|
||||||
else:
|
else:
|
||||||
raise Exception('Could not find a unit keyword associated with "%s"' % unit_str)
|
raise Exception('Could not find a unit keyword associated with "%s"' % unit_str)
|
||||||
|
|
||||||
|
@total_ordering
|
||||||
class Distance(MeasureBase):
|
class Distance(MeasureBase):
|
||||||
UNITS = {
|
UNITS = {
|
||||||
'chain' : 20.1168,
|
'chain' : 20.1168,
|
||||||
|
@ -178,9 +181,15 @@ class Distance(MeasureBase):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return '%s %s' % (getattr(self, self._default_unit), self._default_unit)
|
return '%s %s' % (getattr(self, self._default_unit), self._default_unit)
|
||||||
|
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
if isinstance(other, Distance):
|
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:
|
else:
|
||||||
return NotImplemented
|
return NotImplemented
|
||||||
|
|
||||||
|
@ -244,6 +253,7 @@ class Distance(MeasureBase):
|
||||||
def __nonzero__(self):
|
def __nonzero__(self):
|
||||||
return bool(self.m)
|
return bool(self.m)
|
||||||
|
|
||||||
|
@total_ordering
|
||||||
class Area(MeasureBase):
|
class Area(MeasureBase):
|
||||||
# Getting the square units values and the alias dictionary.
|
# Getting the square units values and the alias dictionary.
|
||||||
UNITS = dict([('sq_%s' % k, v ** 2) for k, v in Distance.UNITS.items()])
|
UNITS = dict([('sq_%s' % k, v ** 2) for k, v in Distance.UNITS.items()])
|
||||||
|
@ -267,9 +277,15 @@ class Area(MeasureBase):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return '%s %s' % (getattr(self, self._default_unit), self._default_unit)
|
return '%s %s' % (getattr(self, self._default_unit), self._default_unit)
|
||||||
|
|
||||||
def __cmp__(self, other):
|
def __eq__(self, other):
|
||||||
if isinstance(other, Area):
|
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:
|
else:
|
||||||
return NotImplemented
|
return NotImplemented
|
||||||
|
|
||||||
|
|
|
@ -156,11 +156,11 @@ class BoundMethodWeakref(object):
|
||||||
"""Whether we are still a valid reference"""
|
"""Whether we are still a valid reference"""
|
||||||
return self() is not None
|
return self() is not None
|
||||||
|
|
||||||
def __cmp__( self, other ):
|
def __eq__(self, other):
|
||||||
"""Compare with another reference"""
|
"""Compare with another reference"""
|
||||||
if not isinstance(other, self.__class__):
|
if not isinstance(other, self.__class__):
|
||||||
return cmp( self.__class__, type(other) )
|
return self.__class__ == type(other)
|
||||||
return cmp( self.key, other.key)
|
return self.key == other.key
|
||||||
|
|
||||||
def __call__(self):
|
def __call__(self):
|
||||||
"""Return a strong reference to the bound method
|
"""Return a strong reference to the bound method
|
||||||
|
|
|
@ -58,6 +58,7 @@ def lazy(func, *resultclasses):
|
||||||
function is evaluated on every access.
|
function is evaluated on every access.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
@total_ordering
|
||||||
class __proxy__(Promise):
|
class __proxy__(Promise):
|
||||||
"""
|
"""
|
||||||
Encapsulate a function call and act as a proxy for methods that are
|
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):
|
def __str_cast(self):
|
||||||
return str(func(*self.__args, **self.__kw))
|
return str(func(*self.__args, **self.__kw))
|
||||||
|
|
||||||
def __cmp__(self, rhs):
|
def __cast(self):
|
||||||
if self._delegate_str:
|
if self._delegate_str:
|
||||||
s = str(func(*self.__args, **self.__kw))
|
return self.__str_cast()
|
||||||
elif self._delegate_unicode:
|
elif self._delegate_unicode:
|
||||||
s = unicode(func(*self.__args, **self.__kw))
|
return self.__unicode_cast()
|
||||||
else:
|
else:
|
||||||
s = func(*self.__args, **self.__kw)
|
return func(*self.__args, **self.__kw)
|
||||||
if isinstance(rhs, Promise):
|
|
||||||
return -cmp(rhs, s)
|
def __eq__(self, other):
|
||||||
else:
|
if isinstance(other, Promise):
|
||||||
return cmp(s, rhs)
|
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):
|
def __mod__(self, rhs):
|
||||||
if self._delegate_str:
|
if self._delegate_str:
|
||||||
|
|
Loading…
Reference in New Issue