测试gitnore
This commit is contained in:
@@ -38,7 +38,7 @@ and Geoff Biggs' PhD work on dimensioned units for robotics.
|
||||
from decimal import Decimal
|
||||
from functools import total_ordering
|
||||
|
||||
__all__ = ["A", "Area", "D", "Distance"]
|
||||
__all__ = ['A', 'Area', 'D', 'Distance']
|
||||
|
||||
NUMERIC_TYPES = (int, float, Decimal)
|
||||
AREA_PREFIX = "sq_"
|
||||
@@ -73,17 +73,13 @@ class MeasureBase:
|
||||
if name in self.UNITS:
|
||||
return self.standard / self.UNITS[name]
|
||||
else:
|
||||
raise AttributeError("Unknown unit type: %s" % name)
|
||||
raise AttributeError('Unknown unit type: %s' % name)
|
||||
|
||||
def __repr__(self):
|
||||
return "%s(%s=%s)" % (
|
||||
pretty_name(self),
|
||||
self._default_unit,
|
||||
getattr(self, self._default_unit),
|
||||
)
|
||||
return '%s(%s=%s)' % (pretty_name(self), self._default_unit, getattr(self, self._default_unit))
|
||||
|
||||
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)
|
||||
|
||||
# **** Comparison methods ****
|
||||
|
||||
@@ -108,65 +104,49 @@ class MeasureBase:
|
||||
if isinstance(other, self.__class__):
|
||||
return self.__class__(
|
||||
default_unit=self._default_unit,
|
||||
**{self.STANDARD_UNIT: (self.standard + other.standard)},
|
||||
**{self.STANDARD_UNIT: (self.standard + other.standard)}
|
||||
)
|
||||
else:
|
||||
raise TypeError(
|
||||
"%(class)s must be added with %(class)s" % {"class": pretty_name(self)}
|
||||
)
|
||||
raise TypeError('%(class)s must be added with %(class)s' % {"class": pretty_name(self)})
|
||||
|
||||
def __iadd__(self, other):
|
||||
if isinstance(other, self.__class__):
|
||||
self.standard += other.standard
|
||||
return self
|
||||
else:
|
||||
raise TypeError(
|
||||
"%(class)s must be added with %(class)s" % {"class": pretty_name(self)}
|
||||
)
|
||||
raise TypeError('%(class)s must be added with %(class)s' % {"class": pretty_name(self)})
|
||||
|
||||
def __sub__(self, other):
|
||||
if isinstance(other, self.__class__):
|
||||
return self.__class__(
|
||||
default_unit=self._default_unit,
|
||||
**{self.STANDARD_UNIT: (self.standard - other.standard)},
|
||||
**{self.STANDARD_UNIT: (self.standard - other.standard)}
|
||||
)
|
||||
else:
|
||||
raise TypeError(
|
||||
"%(class)s must be subtracted from %(class)s"
|
||||
% {"class": pretty_name(self)}
|
||||
)
|
||||
raise TypeError('%(class)s must be subtracted from %(class)s' % {"class": pretty_name(self)})
|
||||
|
||||
def __isub__(self, other):
|
||||
if isinstance(other, self.__class__):
|
||||
self.standard -= other.standard
|
||||
return self
|
||||
else:
|
||||
raise TypeError(
|
||||
"%(class)s must be subtracted from %(class)s"
|
||||
% {"class": pretty_name(self)}
|
||||
)
|
||||
raise TypeError('%(class)s must be subtracted from %(class)s' % {"class": pretty_name(self)})
|
||||
|
||||
def __mul__(self, other):
|
||||
if isinstance(other, NUMERIC_TYPES):
|
||||
return self.__class__(
|
||||
default_unit=self._default_unit,
|
||||
**{self.STANDARD_UNIT: (self.standard * other)},
|
||||
**{self.STANDARD_UNIT: (self.standard * other)}
|
||||
)
|
||||
else:
|
||||
raise TypeError(
|
||||
"%(class)s must be multiplied with number"
|
||||
% {"class": pretty_name(self)}
|
||||
)
|
||||
raise TypeError('%(class)s must be multiplied with number' % {"class": pretty_name(self)})
|
||||
|
||||
def __imul__(self, other):
|
||||
if isinstance(other, NUMERIC_TYPES):
|
||||
self.standard *= float(other)
|
||||
return self
|
||||
else:
|
||||
raise TypeError(
|
||||
"%(class)s must be multiplied with number"
|
||||
% {"class": pretty_name(self)}
|
||||
)
|
||||
raise TypeError('%(class)s must be multiplied with number' % {"class": pretty_name(self)})
|
||||
|
||||
def __rmul__(self, other):
|
||||
return self * other
|
||||
@@ -177,22 +157,17 @@ class MeasureBase:
|
||||
if isinstance(other, NUMERIC_TYPES):
|
||||
return self.__class__(
|
||||
default_unit=self._default_unit,
|
||||
**{self.STANDARD_UNIT: (self.standard / other)},
|
||||
**{self.STANDARD_UNIT: (self.standard / other)}
|
||||
)
|
||||
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)})
|
||||
|
||||
def __itruediv__(self, other):
|
||||
if isinstance(other, NUMERIC_TYPES):
|
||||
self.standard /= float(other)
|
||||
return self
|
||||
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)})
|
||||
|
||||
def __bool__(self):
|
||||
return bool(self.standard)
|
||||
@@ -224,7 +199,7 @@ class MeasureBase:
|
||||
val += self.UNITS[u] * value
|
||||
default_unit = u
|
||||
else:
|
||||
raise AttributeError("Unknown unit type: %s" % unit)
|
||||
raise AttributeError('Unknown unit type: %s' % unit)
|
||||
return val, default_unit
|
||||
|
||||
@classmethod
|
||||
@@ -242,87 +217,85 @@ class MeasureBase:
|
||||
elif lower in cls.LALIAS:
|
||||
return cls.LALIAS[lower]
|
||||
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)
|
||||
|
||||
|
||||
class Distance(MeasureBase):
|
||||
STANDARD_UNIT = "m"
|
||||
UNITS = {
|
||||
"chain": 20.1168,
|
||||
"chain_benoit": 20.116782,
|
||||
"chain_sears": 20.1167645,
|
||||
"british_chain_benoit": 20.1167824944,
|
||||
"british_chain_sears": 20.1167651216,
|
||||
"british_chain_sears_truncated": 20.116756,
|
||||
"cm": 0.01,
|
||||
"british_ft": 0.304799471539,
|
||||
"british_yd": 0.914398414616,
|
||||
"clarke_ft": 0.3047972654,
|
||||
"clarke_link": 0.201166195164,
|
||||
"fathom": 1.8288,
|
||||
"ft": 0.3048,
|
||||
"furlong": 201.168,
|
||||
"german_m": 1.0000135965,
|
||||
"gold_coast_ft": 0.304799710181508,
|
||||
"indian_yd": 0.914398530744,
|
||||
"inch": 0.0254,
|
||||
"km": 1000.0,
|
||||
"link": 0.201168,
|
||||
"link_benoit": 0.20116782,
|
||||
"link_sears": 0.20116765,
|
||||
"m": 1.0,
|
||||
"mi": 1609.344,
|
||||
"mm": 0.001,
|
||||
"nm": 1852.0,
|
||||
"nm_uk": 1853.184,
|
||||
"rod": 5.0292,
|
||||
"sears_yd": 0.91439841,
|
||||
"survey_ft": 0.304800609601,
|
||||
"um": 0.000001,
|
||||
"yd": 0.9144,
|
||||
'chain': 20.1168,
|
||||
'chain_benoit': 20.116782,
|
||||
'chain_sears': 20.1167645,
|
||||
'british_chain_benoit': 20.1167824944,
|
||||
'british_chain_sears': 20.1167651216,
|
||||
'british_chain_sears_truncated': 20.116756,
|
||||
'cm': 0.01,
|
||||
'british_ft': 0.304799471539,
|
||||
'british_yd': 0.914398414616,
|
||||
'clarke_ft': 0.3047972654,
|
||||
'clarke_link': 0.201166195164,
|
||||
'fathom': 1.8288,
|
||||
'ft': 0.3048,
|
||||
'furlong': 201.168,
|
||||
'german_m': 1.0000135965,
|
||||
'gold_coast_ft': 0.304799710181508,
|
||||
'indian_yd': 0.914398530744,
|
||||
'inch': 0.0254,
|
||||
'km': 1000.0,
|
||||
'link': 0.201168,
|
||||
'link_benoit': 0.20116782,
|
||||
'link_sears': 0.20116765,
|
||||
'm': 1.0,
|
||||
'mi': 1609.344,
|
||||
'mm': 0.001,
|
||||
'nm': 1852.0,
|
||||
'nm_uk': 1853.184,
|
||||
'rod': 5.0292,
|
||||
'sears_yd': 0.91439841,
|
||||
'survey_ft': 0.304800609601,
|
||||
'um': 0.000001,
|
||||
'yd': 0.9144,
|
||||
}
|
||||
|
||||
# Unit aliases for `UNIT` terms encountered in Spatial Reference WKT.
|
||||
ALIAS = {
|
||||
"centimeter": "cm",
|
||||
"foot": "ft",
|
||||
"inches": "inch",
|
||||
"kilometer": "km",
|
||||
"kilometre": "km",
|
||||
"meter": "m",
|
||||
"metre": "m",
|
||||
"micrometer": "um",
|
||||
"micrometre": "um",
|
||||
"millimeter": "mm",
|
||||
"millimetre": "mm",
|
||||
"mile": "mi",
|
||||
"yard": "yd",
|
||||
"British chain (Benoit 1895 B)": "british_chain_benoit",
|
||||
"British chain (Sears 1922)": "british_chain_sears",
|
||||
"British chain (Sears 1922 truncated)": "british_chain_sears_truncated",
|
||||
"British foot (Sears 1922)": "british_ft",
|
||||
"British foot": "british_ft",
|
||||
"British yard (Sears 1922)": "british_yd",
|
||||
"British yard": "british_yd",
|
||||
"Clarke's Foot": "clarke_ft",
|
||||
"Clarke's link": "clarke_link",
|
||||
"Chain (Benoit)": "chain_benoit",
|
||||
"Chain (Sears)": "chain_sears",
|
||||
"Foot (International)": "ft",
|
||||
"Furrow Long": "furlong",
|
||||
"German legal metre": "german_m",
|
||||
"Gold Coast foot": "gold_coast_ft",
|
||||
"Indian yard": "indian_yd",
|
||||
"Link (Benoit)": "link_benoit",
|
||||
"Link (Sears)": "link_sears",
|
||||
"Nautical Mile": "nm",
|
||||
"Nautical Mile (UK)": "nm_uk",
|
||||
"US survey foot": "survey_ft",
|
||||
"U.S. Foot": "survey_ft",
|
||||
"Yard (Indian)": "indian_yd",
|
||||
"Yard (Sears)": "sears_yd",
|
||||
'centimeter': 'cm',
|
||||
'foot': 'ft',
|
||||
'inches': 'inch',
|
||||
'kilometer': 'km',
|
||||
'kilometre': 'km',
|
||||
'meter': 'm',
|
||||
'metre': 'm',
|
||||
'micrometer': 'um',
|
||||
'micrometre': 'um',
|
||||
'millimeter': 'mm',
|
||||
'millimetre': 'mm',
|
||||
'mile': 'mi',
|
||||
'yard': 'yd',
|
||||
'British chain (Benoit 1895 B)': 'british_chain_benoit',
|
||||
'British chain (Sears 1922)': 'british_chain_sears',
|
||||
'British chain (Sears 1922 truncated)': 'british_chain_sears_truncated',
|
||||
'British foot (Sears 1922)': 'british_ft',
|
||||
'British foot': 'british_ft',
|
||||
'British yard (Sears 1922)': 'british_yd',
|
||||
'British yard': 'british_yd',
|
||||
"Clarke's Foot": 'clarke_ft',
|
||||
"Clarke's link": 'clarke_link',
|
||||
'Chain (Benoit)': 'chain_benoit',
|
||||
'Chain (Sears)': 'chain_sears',
|
||||
'Foot (International)': 'ft',
|
||||
'Furrow Long': 'furlong',
|
||||
'German legal metre': 'german_m',
|
||||
'Gold Coast foot': 'gold_coast_ft',
|
||||
'Indian yard': 'indian_yd',
|
||||
'Link (Benoit)': 'link_benoit',
|
||||
'Link (Sears)': 'link_sears',
|
||||
'Nautical Mile': 'nm',
|
||||
'Nautical Mile (UK)': 'nm_uk',
|
||||
'US survey foot': 'survey_ft',
|
||||
'U.S. Foot': 'survey_ft',
|
||||
'Yard (Indian)': 'indian_yd',
|
||||
'Yard (Sears)': 'sears_yd'
|
||||
}
|
||||
LALIAS = {k.lower(): v for k, v in ALIAS.items()}
|
||||
|
||||
@@ -330,39 +303,34 @@ class Distance(MeasureBase):
|
||||
if isinstance(other, self.__class__):
|
||||
return Area(
|
||||
default_unit=AREA_PREFIX + self._default_unit,
|
||||
**{AREA_PREFIX + self.STANDARD_UNIT: (self.standard * other.standard)},
|
||||
**{AREA_PREFIX + self.STANDARD_UNIT: (self.standard * other.standard)}
|
||||
)
|
||||
elif isinstance(other, NUMERIC_TYPES):
|
||||
return self.__class__(
|
||||
default_unit=self._default_unit,
|
||||
**{self.STANDARD_UNIT: (self.standard * other)},
|
||||
**{self.STANDARD_UNIT: (self.standard * other)}
|
||||
)
|
||||
else:
|
||||
raise TypeError(
|
||||
"%(distance)s must be multiplied with number or %(distance)s"
|
||||
% {
|
||||
"distance": pretty_name(self.__class__),
|
||||
}
|
||||
)
|
||||
raise TypeError('%(distance)s must be multiplied with number or %(distance)s' % {
|
||||
"distance": pretty_name(self.__class__),
|
||||
})
|
||||
|
||||
|
||||
class Area(MeasureBase):
|
||||
STANDARD_UNIT = AREA_PREFIX + Distance.STANDARD_UNIT
|
||||
# Getting the square units values and the alias dictionary.
|
||||
UNITS = {"%s%s" % (AREA_PREFIX, k): v**2 for k, v in Distance.UNITS.items()}
|
||||
ALIAS = {k: "%s%s" % (AREA_PREFIX, v) for k, v in Distance.ALIAS.items()}
|
||||
UNITS = {'%s%s' % (AREA_PREFIX, k): v ** 2 for k, v in Distance.UNITS.items()}
|
||||
ALIAS = {k: '%s%s' % (AREA_PREFIX, v) for k, v in Distance.ALIAS.items()}
|
||||
LALIAS = {k.lower(): v for k, v in ALIAS.items()}
|
||||
|
||||
def __truediv__(self, other):
|
||||
if isinstance(other, NUMERIC_TYPES):
|
||||
return self.__class__(
|
||||
default_unit=self._default_unit,
|
||||
**{self.STANDARD_UNIT: (self.standard / other)},
|
||||
**{self.STANDARD_UNIT: (self.standard / other)}
|
||||
)
|
||||
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)})
|
||||
|
||||
|
||||
# Shortcuts
|
||||
|
||||
Reference in New Issue
Block a user