本文整理汇总了Python中units.unit函数的典型用法代码示例。如果您正苦于以下问题:Python unit函数的具体用法?Python unit怎么用?Python unit使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了unit函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_rmultiply_named_scalar
def test_rmultiply_named_scalar():
"""Scalars * Named quantities"""
m_per_s = NamedComposedUnit('vel',
unit('m') / unit('s'))
assert (2 * Quantity(8, m_per_s) ==
Quantity(16, m_per_s))
示例2: test_divide_named_scalar
def test_divide_named_scalar():
"""Named quantities / scalars"""
m_per_s = NamedComposedUnit('vel',
unit('m') / unit('s'))
assert (Quantity(8, m_per_s) / 2 ==
Quantity(4, m_per_s))
示例3: test_multiply_named_scalar
def test_multiply_named_scalar():
"""Named quantities * scalars"""
m_per_s = NamedComposedUnit('vel',
unit('m') / unit('s'))
assert (Quantity(8, m_per_s) * 2 ==
Quantity(16, m_per_s))
示例4: test_valid_basic_to_composed
def test_valid_basic_to_composed():
"""Test conversion to composed units."""
composed_cm = unit('cm').composed_unit
one_m_in_cm = composed_cm(Quantity(1, unit('m')))
assert one_m_in_cm == Quantity(1, unit('m'))
示例5: test_rdivide_named_scalar
def test_rdivide_named_scalar():
"""Scalars / Named quantities"""
m_per_s = NamedComposedUnit('vel',
unit('m') / unit('s'))
assert (4 / Quantity(2, m_per_s) ==
Quantity(2, unit('s') / unit('m')))
示例6: test_valid_composed_to_basic
def test_valid_composed_to_basic():
"""Composed units should convert to basic equivalents."""
metre = unit('m')
hundred_cm_in_metres = metre(Quantity(100, unit('cm')))
assert hundred_cm_in_metres == Quantity(1.0, metre)
assert str(hundred_cm_in_metres) == '1.0 m'
示例7: test_valid_basic_to_named
def test_valid_basic_to_named():
"""Basic units should convert into named equivalents."""
metre = unit('m')
thousand_m_in_km = unit('km')(Quantity(1000, metre))
assert thousand_m_in_km == Quantity(1, unit('km'))
assert thousand_m_in_km.unit == unit('km')
assert str(thousand_m_in_km) == '1 km'
示例8: test_valid_named_to_named
def test_valid_named_to_named():
"""Named units should convert to named equivalents."""
gray = unit('Gy')
sievert = unit('Sv')
one_gray_in_sievert = sievert(Quantity(1, gray))
assert one_gray_in_sievert == Quantity(1, gray)
assert str(one_gray_in_sievert) == '1 Sv'
示例9: define_imperial_units
def define_imperial_units():
"""Define some common imperial units."""
assert unit("m").is_si() # Ensure SI units already defined
# scaled_unit measures
scaled_unit("inch", "cm", 2.54, name="inch")
scaled_unit("in", "cm", 2.54, name="inch") # 'in' is a python keyword
scaled_unit("ft", "inch", 12.0, name="foot")
scaled_unit("yd", "ft", 3.0, name="yard")
scaled_unit("fm", "ft", 6.0, name="fathom")
scaled_unit("rd", "yd", 5.5, name="rod")
scaled_unit("fur", "rd", 40.0, name="furlong")
scaled_unit("mi", "fur", 8.0, name="mile")
scaled_unit("lea", "mi", 3.0, name="leage")
# nautical scaled_unit measures
scaled_unit("NM", "m", 1852.0, name="nautical mile")
scaled_unit("cable", "NM", 0.1, name="cable length")
# chain measure
scaled_unit("li", "inch", 7.92, name="link")
scaled_unit("ch", "li", 100.0, name="chain")
# area measure
NamedComposedUnit("acre", ComposedUnit([unit("rd")] * 2, [], 160.0), name="acre")
# liquid measures
NamedComposedUnit("pt", ComposedUnit([unit("inch")] * 3, [], 28.875), name="pint")
scaled_unit("gi", "pt", 0.25, name="gills")
scaled_unit("qt", "pt", 2.0, name="quarts")
scaled_unit("gal", "qt", 4.0, name="gallons")
scaled_unit("fl oz", "pt", 1.0 / 16.0, name="fluid ounce")
scaled_unit("fl dr", "fl oz", 1.0 / 8.0, name="fluid drachm")
scaled_unit("minim", "fl dr", 1.0 / 60.0, name="minim")
# weight
scaled_unit("oz", "g", 28.375, name="ounce")
scaled_unit("lb", "oz", 16.0, name="pound")
scaled_unit("ton", "lb", 2000.0, name="ton")
scaled_unit("grain", "lb", 1.0 / 7000.0, name="grain")
scaled_unit("dr", "lb", 1.0 / 256.0, name="dram")
scaled_unit("cwt", "lb", 100.0, name="hundredweight")
scaled_unit("dwt", "grain", 24.0, name="pennyweight")
scaled_unit("oz t", "dwt", 20.0, name="ounce troy")
scaled_unit("lb t", "oz t", 12.0, name="pound troy")
# power
scaled_unit("hpl", "W", 746.9999, name="mechanical")
scaled_unit("hpm", "W", 735.49875, name="metric horsepower")
scaled_unit("hpe", "W", 746.0, name="electric horsepower")
# energy
scaled_unit("BTU", "J", 1055.056, name="ISO BTU", is_si=True)
示例10: test_volume
def test_volume():
"""Volume units should interchange correctly with cubed area units."""
litres = unit('L')
millilitres = unit('mL')
centimetres = unit('cm')
cm_cubed = centimetres * centimetres * centimetres
assert Quantity(1, litres) == Quantity(1000, millilitres)
assert Quantity(1, millilitres) == Quantity(1, cm_cubed)
示例11: test_valid_named_to_basic
def test_valid_named_to_basic():
"""Named units should convert to their basic equivalents"""
kilometre = unit('km')
one_km_in_m = unit('m')(Quantity(1, kilometre))
assert one_km_in_m == Quantity(1000, unit('m'))
assert one_km_in_m.unit == unit('m')
assert str(one_km_in_m) == '1000 m'
示例12: setUp
def setUp(self):
self.laminate = CathodeLaminate(mass_active_material=0.9,
mass_carbon=0.05,
mass_binder=0.05,
name="LMO-NEI")
self.electrode = CoinCellElectrode(total_mass=unit('mg')(15),
substrate_mass=unit('mg')(5),
laminate=self.laminate,
name="DummyElectrode",
diameter=unit('mm')(12.7))
示例13: test_composed_unit_repr
def test_composed_unit_repr():
"""Developer-friendly string representation of composed units."""
test_repr = (repr(unit('m') * unit('g') / unit('s')))
# non-deterministic
assert test_repr in [
"ComposedUnit([LeafUnit('g', True), " + "LeafUnit('m', True)], " + "[LeafUnit('s', True)], 1)",
"ComposedUnit([LeafUnit('m', True), " + "LeafUnit('g', True)], " + "[LeafUnit('s', True)], 1)"
]
示例14: test_good_named_add_w_mult
def test_good_named_add_w_mult():
"""A quantity with a named composed unit that carries a multiplier
should add to a composed unit that has a multiplier"""
mile = unit('mi').composed_unit
kilometre = unit('km')
assert within_epsilon(Quantity(1, mile) + Quantity(1, kilometre),
Quantity(1, kilometre) + Quantity(1, mile))
assert within_epsilon(Quantity(2609.344, unit('m')),
Quantity(1, kilometre) + Quantity(1, mile))
示例15: test_good_named_add_w_mults
def test_good_named_add_w_mults():
"""Two quantities with compatible but differently-named and
differently-multiplied units should add together."""
mile = unit('mi')
kilometre = unit('km')
assert within_epsilon(Quantity(1, mile) + Quantity(1, kilometre),
Quantity(1, kilometre) + Quantity(1, mile))
assert within_epsilon(Quantity(2609.344, unit('m')),
Quantity(1, kilometre) + Quantity(1, mile))