Ticket #17754: measure_u.diff
File measure_u.diff, 3.5 KB (added by , 12 years ago) |
---|
-
.py
old new 54 54 ALIAS = {} 55 55 UNITS = {} 56 56 LALIAS = {} 57 58 to_number = float 57 59 58 60 def __init__(self, default_unit=None, **kwargs): 59 61 value, self._default_unit = self.default_units(kwargs) … … 65 67 return getattr(self, self.STANDARD_UNIT) 66 68 67 69 def _set_standard(self, value): 68 setattr(self, self.STANDARD_UNIT, value)70 setattr(self, self.STANDARD_UNIT, self.to_number(value)) 69 71 70 72 standard = property(_get_standard, _set_standard) 71 73 72 74 def __getattr__(self, name): 73 75 if name in self.UNITS: 74 return self.standard / self. UNITS[name]76 return self.standard / self.to_number(self.UNITS[name]) 75 77 else: 76 78 raise AttributeError('Unknown unit type: %s' % name) 77 79 … … 129 131 def __mul__(self, other): 130 132 if isinstance(other, NUMERIC_TYPES): 131 133 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))}) 133 135 else: 134 136 raise TypeError('%(class)s must be multiplied with number' % {"class":pretty_name(self)}) 135 137 136 138 def __imul__(self, other): 137 139 if isinstance(other, NUMERIC_TYPES): 138 self.standard *= float(other)140 self.standard *= self.to_number(other) 139 141 return self 140 142 else: 141 143 raise TypeError('%(class)s must be multiplied with number' % {"class":pretty_name(self)}) 142 144 143 145 def __rmul__(self, other): 144 return self * other146 return self * self.to_number(other) 145 147 146 148 def __truediv__(self, other): 147 149 if isinstance(other, self.__class__): 148 150 return self.standard / other.standard 149 151 if isinstance(other, NUMERIC_TYPES): 150 152 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))}) 152 154 else: 153 155 raise TypeError('%(class)s must be divided with number or %(class)s' % {"class":pretty_name(self)}) 154 156 … … 157 159 158 160 def __itruediv__(self, other): 159 161 if isinstance(other, NUMERIC_TYPES): 160 self.standard /= float(other)162 self.standard /= self.to_number(other) 161 163 return self 162 164 else: 163 165 raise TypeError('%(class)s must be divided with number' % {"class":pretty_name(self)}) … … 300 302 **{AREA_PREFIX + self.STANDARD_UNIT: (self.standard * other.standard)}) 301 303 elif isinstance(other, NUMERIC_TYPES): 302 304 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))}) 304 306 else: 305 307 raise TypeError('%(distance)s must be multiplied with number or %(distance)s' % { 306 308 "distance" : pretty_name(self.__class__), … … 317 319 def __truediv__(self, other): 318 320 if isinstance(other, NUMERIC_TYPES): 319 321 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))}) 321 323 else: 322 324 raise TypeError('%(class)s must be divided by a number' % {"class":pretty_name(self)}) 323 325