27
27
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
Distance and Area objects to allow for sensible and convienient calculation
30
Distance and Area objects to allow for sensible and convienient calculation
33
33
Authors: Robert Coup, Justin Bronn
71
71
def unit_attname(cls, unit_str):
73
Retrieves the unit attribute name for the given unit string.
73
Retrieves the unit attribute name for the given unit string.
74
74
For example, if the given unit string is 'metre', 'm' would be returned.
75
75
An exception is raised if an attribute cannot be found.
165
165
self.m, self._default_unit = self.default_units(kwargs)
166
166
if default_unit and isinstance(default_unit, str):
167
167
self._default_unit = default_unit
169
169
def __getattr__(self, name):
170
170
if name in self.UNITS:
171
171
return self.m / self.UNITS[name]
173
173
raise AttributeError('Unknown unit type: %s' % name)
175
175
def __repr__(self):
176
176
return 'Distance(%s=%s)' % (self._default_unit, getattr(self, self._default_unit))
178
178
def __str__(self):
179
179
return '%s %s' % (getattr(self, self._default_unit), self._default_unit)
181
181
def __cmp__(self, other):
182
182
if isinstance(other, Distance):
183
183
return cmp(self.m, other.m)
185
185
return NotImplemented
187
187
def __add__(self, other):
188
188
if isinstance(other, Distance):
189
189
return Distance(default_unit=self._default_unit, m=(self.m + other.m))
191
191
raise TypeError('Distance must be added with Distance')
193
193
def __iadd__(self, other):
194
194
if isinstance(other, Distance):
195
195
self.m += other.m
198
198
raise TypeError('Distance must be added with Distance')
200
200
def __sub__(self, other):
201
201
if isinstance(other, Distance):
202
202
return Distance(default_unit=self._default_unit, m=(self.m - other.m))
204
204
raise TypeError('Distance must be subtracted from Distance')
206
206
def __isub__(self, other):
207
207
if isinstance(other, Distance):
208
208
self.m -= other.m
211
211
raise TypeError('Distance must be subtracted from Distance')
213
213
def __mul__(self, other):
214
214
if isinstance(other, (int, float, long, Decimal)):
215
215
return Distance(default_unit=self._default_unit, m=(self.m * float(other)))
217
217
return Area(default_unit='sq_' + self._default_unit, sq_m=(self.m * other.m))
219
219
raise TypeError('Distance must be multiplied with number or Distance')
221
221
def __imul__(self, other):
222
222
if isinstance(other, (int, float, long, Decimal)):
223
223
self.m *= float(other)
226
226
raise TypeError('Distance must be multiplied with number')
228
def __rmul__(self, other):
228
231
def __div__(self, other):
229
232
if isinstance(other, (int, float, long, Decimal)):
230
233
return Distance(default_unit=self._default_unit, m=(self.m / float(other)))
251
254
self.sq_m, self._default_unit = self.default_units(kwargs)
252
255
if default_unit and isinstance(default_unit, str):
253
256
self._default_unit = default_unit
255
258
def __getattr__(self, name):
256
259
if name in self.UNITS:
257
260
return self.sq_m / self.UNITS[name]
259
262
raise AttributeError('Unknown unit type: ' + name)
261
264
def __repr__(self):
262
265
return 'Area(%s=%s)' % (self._default_unit, getattr(self, self._default_unit))
269
272
return cmp(self.sq_m, other.sq_m)
271
274
return NotImplemented
273
276
def __add__(self, other):
274
277
if isinstance(other, Area):
275
278
return Area(default_unit=self._default_unit, sq_m=(self.sq_m + other.sq_m))
277
280
raise TypeError('Area must be added with Area')
279
282
def __iadd__(self, other):
280
283
if isinstance(other, Area):
281
284
self.sq_m += other.sq_m
284
287
raise TypeError('Area must be added with Area')
286
289
def __sub__(self, other):
287
290
if isinstance(other, Area):
288
291
return Area(default_unit=self._default_unit, sq_m=(self.sq_m - other.sq_m))
290
293
raise TypeError('Area must be subtracted from Area')
292
295
def __isub__(self, other):
293
296
if isinstance(other, Area):
294
297
self.sq_m -= other.sq_m
297
300
raise TypeError('Area must be subtracted from Area')
299
302
def __mul__(self, other):
300
303
if isinstance(other, (int, float, long, Decimal)):
301
304
return Area(default_unit=self._default_unit, sq_m=(self.sq_m * float(other)))
303
306
raise TypeError('Area must be multiplied with number')
305
308
def __imul__(self, other):
306
309
if isinstance(other, (int, float, long, Decimal)):
307
310
self.sq_m *= float(other)
310
313
raise TypeError('Area must be multiplied with number')
315
def __rmul__(self, other):
312
318
def __div__(self, other):
313
319
if isinstance(other, (int, float, long, Decimal)):
314
320
return Area(default_unit=self._default_unit, sq_m=(self.sq_m / float(other)))