Merge branch 'master' into develop
This commit is contained in:
commit
5ca51a56a8
3
.gitignore
vendored
3
.gitignore
vendored
@ -12,3 +12,6 @@ MANIFEST
|
||||
|
||||
# WebDAV file system cache files
|
||||
.DAV/
|
||||
|
||||
# tags files (from ctags)
|
||||
tags
|
||||
|
@ -72,6 +72,10 @@ cmil = 5.067075e-10 * m ** 2 = circular_mils
|
||||
darcy = 9.869233e-13 * m ** 2
|
||||
hectare = 100 * are = ha
|
||||
|
||||
# Concentration
|
||||
[concentration] = [substance] / [volume]
|
||||
molar = mol / (1e-3 * m ** 3) = M
|
||||
|
||||
# EM
|
||||
esu = 1 * erg**0.5 * centimeter**0.5 = statcoulombs = statC = franklin = Fr
|
||||
esu_per_second = 1 * esu / second = statampere
|
||||
|
@ -142,6 +142,13 @@ class _Quantity(SharedRegistryObject):
|
||||
def __repr__(self):
|
||||
return "<Quantity({0}, '{1}')>".format(self._magnitude, self._units)
|
||||
|
||||
def __hash__(self):
|
||||
self_base = self.to_base_units()
|
||||
if self_base.dimensionless:
|
||||
return hash(self_base.magnitude)
|
||||
else:
|
||||
return hash((self_base.__class__, self_base.magnitude, self_base.units))
|
||||
|
||||
def __format__(self, spec):
|
||||
spec = spec or self.default_format
|
||||
|
||||
@ -393,7 +400,7 @@ class _Quantity(SharedRegistryObject):
|
||||
warnings.warn(w, stacklevel=2)
|
||||
return self
|
||||
|
||||
if (self.unitless or self.magnitude==0 or
|
||||
if (self.unitless or self.magnitude==0 or
|
||||
math.isnan(self.magnitude) or math.isinf(self.magnitude)):
|
||||
return self
|
||||
|
||||
|
@ -39,7 +39,6 @@ class TestIssues(QuantityTestCase):
|
||||
|
||||
def test_issue29(self):
|
||||
ureg = UnitRegistry()
|
||||
ureg.define('molar = mole / liter = M')
|
||||
t = 4 * ureg('mM')
|
||||
self.assertEqual(t.magnitude, 4)
|
||||
self.assertEqual(t._units, UnitsContainer(millimolar=1))
|
||||
|
@ -90,6 +90,22 @@ class TestQuantity(QuantityTestCase):
|
||||
self.assertEqual(str(x), '4.2 meter')
|
||||
self.assertEqual(repr(x), "<Quantity(4.2, 'meter')>")
|
||||
|
||||
def test_quantity_hash(self):
|
||||
x = self.Q_(4.2, 'meter')
|
||||
x2 = self.Q_(4200, 'millimeter')
|
||||
y = self.Q_(2, 'second')
|
||||
z = self.Q_(0.5, 'hertz')
|
||||
self.assertEqual(hash(x), hash(x2))
|
||||
|
||||
# Dimensionless equality
|
||||
self.assertEqual(hash(y * z), hash(1.0))
|
||||
|
||||
# Dimensionless equality from a different unit registry
|
||||
ureg2 = UnitRegistry(force_ndarray=self.FORCE_NDARRAY)
|
||||
y2 = ureg2.Quantity(2, 'second')
|
||||
z2 = ureg2.Quantity(0.5, 'hertz')
|
||||
self.assertEqual(hash(y * z), hash(y2 * z2))
|
||||
|
||||
def test_quantity_format(self):
|
||||
x = self.Q_(4.12345678, UnitsContainer(meter=2, kilogram=1, second=-1))
|
||||
for spec, result in (('{0}', str(x)), ('{0!s}', str(x)), ('{0!r}', repr(x)),
|
||||
|
Loading…
Reference in New Issue
Block a user