Ticket #17754: measure_u.diff

File measure_u.diff, 3.5 KB (added by Riccardo Di Virgilio, 12 years ago)
  • .py

    old new  
    5454    ALIAS  = {}
    5555    UNITS  = {}
    5656    LALIAS = {}
     57   
     58    to_number = float
    5759
    5860    def __init__(self, default_unit=None, **kwargs):
    5961        value, self._default_unit = self.default_units(kwargs)
     
    6567        return getattr(self, self.STANDARD_UNIT)
    6668
    6769    def _set_standard(self, value):
    68         setattr(self, self.STANDARD_UNIT, value)
     70        setattr(self, self.STANDARD_UNIT, self.to_number(value))
    6971
    7072    standard = property(_get_standard, _set_standard)
    71 
     73   
    7274    def __getattr__(self, name):
    7375        if name in self.UNITS:
    74             return self.standard / self.UNITS[name]
     76            return self.standard / self.to_number(self.UNITS[name])
    7577        else:
    7678            raise AttributeError('Unknown unit type: %s' % name)
    7779
     
    129131    def __mul__(self, other):
    130132        if isinstance(other, NUMERIC_TYPES):
    131133            return self.__class__(default_unit=self._default_unit,
    132                 **{self.STANDARD_UNIT: (self.standard * other)})
     134                **{self.STANDARD_UNIT: (self.standard * self.to_number(other))})
    133135        else:
    134136            raise TypeError('%(class)s must be multiplied with number' % {"class":pretty_name(self)})
    135137
    136138    def __imul__(self, other):
    137139        if isinstance(other, NUMERIC_TYPES):
    138             self.standard *= float(other)
     140            self.standard *= self.to_number(other)
    139141            return self
    140142        else:
    141143            raise TypeError('%(class)s must be multiplied with number' % {"class":pretty_name(self)})
    142144
    143145    def __rmul__(self, other):
    144         return self * other
     146        return self * self.to_number(other)
    145147
    146148    def __truediv__(self, other):
    147149        if isinstance(other, self.__class__):
    148150            return self.standard / other.standard
    149151        if isinstance(other, NUMERIC_TYPES):
    150152            return self.__class__(default_unit=self._default_unit,
    151                 **{self.STANDARD_UNIT: (self.standard / other)})
     153                **{self.STANDARD_UNIT: (self.standard / self.to_number(other))})
    152154        else:
    153155            raise TypeError('%(class)s must be divided with number or %(class)s' % {"class":pretty_name(self)})
    154156
     
    157159
    158160    def __itruediv__(self, other):
    159161        if isinstance(other, NUMERIC_TYPES):
    160             self.standard /= float(other)
     162            self.standard /= self.to_number(other)
    161163            return self
    162164        else:
    163165            raise TypeError('%(class)s must be divided with number' % {"class":pretty_name(self)})
     
    300302                **{AREA_PREFIX + self.STANDARD_UNIT: (self.standard * other.standard)})
    301303        elif isinstance(other, NUMERIC_TYPES):
    302304            return self.__class__(default_unit=self._default_unit,
    303                 **{self.STANDARD_UNIT: (self.standard * other)})
     305                **{self.STANDARD_UNIT: (self.standard * self.to_number(other))})
    304306        else:
    305307            raise TypeError('%(distance)s must be multiplied with number or %(distance)s' % {
    306308                "distance" : pretty_name(self.__class__),
     
    317319    def __truediv__(self, other):
    318320        if isinstance(other, NUMERIC_TYPES):
    319321            return self.__class__(default_unit=self._default_unit,
    320                 **{self.STANDARD_UNIT: (self.standard / other)})
     322                **{self.STANDARD_UNIT: (self.standard / self.to_number(other))})
    321323        else:
    322324            raise TypeError('%(class)s must be divided by a number' % {"class":pretty_name(self)})
    323325
Back to Top