[py3] Made gis.measure Python 3-compatible

This commit is contained in:
Claude Paroz 2012-08-08 13:43:21 +02:00
parent fa4cb34817
commit 2da3af23aa
1 changed files with 8 additions and 4 deletions

View File

@ -143,7 +143,7 @@ class MeasureBase(object):
def __rmul__(self, other): def __rmul__(self, other):
return self * other return self * other
def __div__(self, other): def __truediv__(self, other):
if isinstance(other, self.__class__): if isinstance(other, self.__class__):
return self.standard / other.standard return self.standard / other.standard
if isinstance(other, NUMERIC_TYPES): if isinstance(other, NUMERIC_TYPES):
@ -151,16 +151,19 @@ class MeasureBase(object):
**{self.STANDARD_UNIT: (self.standard / other)}) **{self.STANDARD_UNIT: (self.standard / other)})
else: else:
raise TypeError('%(class)s must be divided with number or %(class)s' % {"class":pretty_name(self)}) raise TypeError('%(class)s must be divided with number or %(class)s' % {"class":pretty_name(self)})
__div__ = __truediv__ # Python 2 compatibility
def __idiv__(self, other): def __itruediv__(self, other):
if isinstance(other, NUMERIC_TYPES): if isinstance(other, NUMERIC_TYPES):
self.standard /= float(other) self.standard /= float(other)
return self return self
else: else:
raise TypeError('%(class)s must be divided with number' % {"class":pretty_name(self)}) raise TypeError('%(class)s must be divided with number' % {"class":pretty_name(self)})
__idiv__ = __itruediv__ # Python 2 compatibility
def __nonzero__(self): def __bool__(self):
return bool(self.standard) return bool(self.standard)
__nonzero__ = __bool__ # Python 2 compatibility
def default_units(self, kwargs): def default_units(self, kwargs):
""" """
@ -305,12 +308,13 @@ class Area(MeasureBase):
ALIAS = dict([(k, '%s%s' % (AREA_PREFIX, v)) for k, v in Distance.ALIAS.items()]) ALIAS = dict([(k, '%s%s' % (AREA_PREFIX, v)) for k, v in Distance.ALIAS.items()])
LALIAS = dict([(k.lower(), v) for k, v in ALIAS.items()]) LALIAS = dict([(k.lower(), v) for k, v in ALIAS.items()])
def __div__(self, other): def __truediv__(self, other):
if isinstance(other, NUMERIC_TYPES): if isinstance(other, NUMERIC_TYPES):
return self.__class__(default_unit=self._default_unit, return self.__class__(default_unit=self._default_unit,
**{self.STANDARD_UNIT: (self.standard / other)}) **{self.STANDARD_UNIT: (self.standard / other)})
else: else:
raise TypeError('%(class)s must be divided by a number' % {"class":pretty_name(self)}) raise TypeError('%(class)s must be divided by a number' % {"class":pretty_name(self)})
__div__ = __truediv__ # Python 2 compatibility
# Shortcuts # Shortcuts