Add stones to mass units (#83933)

pull/83936/head
epenet 2022-12-13 17:35:14 +01:00 committed by GitHub
parent fed308b59d
commit 4a848e8222
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 1 deletions

View File

@ -767,6 +767,7 @@ class UnitOfMass(StrEnum):
MICROGRAMS = "µg"
OUNCES = "oz"
POUNDS = "lb"
STONES = "st"
MASS_GRAMS: Final = "g"

View File

@ -33,7 +33,8 @@ _DAYS_TO_SECS = 24 * _HRS_TO_SECS # 1 day = 24 hours = 86400 seconds
# Mass conversion constants
_POUND_TO_G = 453.59237
_OUNCE_TO_G = _POUND_TO_G / 16
_OUNCE_TO_G = _POUND_TO_G / 16 # 16 ounces to a pound
_STONE_TO_G = _POUND_TO_G * 14 # 14 pounds to a stone
# Pressure conversion constants
_STANDARD_GRAVITY = 9.80665
@ -143,6 +144,7 @@ class MassConverter(BaseUnitConverter):
UnitOfMass.KILOGRAMS: 1 / 1000,
UnitOfMass.OUNCES: 1 / _OUNCE_TO_G,
UnitOfMass.POUNDS: 1 / _POUND_TO_G,
UnitOfMass.STONES: 1 / _STONE_TO_G,
}
VALID_UNITS = {
UnitOfMass.GRAMS,
@ -151,6 +153,7 @@ class MassConverter(BaseUnitConverter):
UnitOfMass.MICROGRAMS,
UnitOfMass.OUNCES,
UnitOfMass.POUNDS,
UnitOfMass.STONES,
}

View File

@ -345,6 +345,11 @@ def test_energy_convert(
(16, UnitOfMass.OUNCES, 453592.37, UnitOfMass.MILLIGRAMS),
(16, UnitOfMass.OUNCES, 453592370, UnitOfMass.MICROGRAMS),
(16, UnitOfMass.OUNCES, 1, UnitOfMass.POUNDS),
(1, UnitOfMass.STONES, pytest.approx(6.350293), UnitOfMass.KILOGRAMS),
(1, UnitOfMass.STONES, pytest.approx(6350.293), UnitOfMass.GRAMS),
(1, UnitOfMass.STONES, pytest.approx(6350293), UnitOfMass.MILLIGRAMS),
(1, UnitOfMass.STONES, pytest.approx(14), UnitOfMass.POUNDS),
(1, UnitOfMass.STONES, pytest.approx(224), UnitOfMass.OUNCES),
],
)
def test_mass_convert(