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))
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))
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))
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')))
def named_unit(symbol, numer, denom, multiplier=1, is_si=True): """Shortcut to create and return a new named unit.""" numer_units = [unit(x) for x in numer] denom_units = [unit(x) for x in denom] return NamedComposedUnit( symbol, ComposedUnit(numer_units, denom_units, multiplier), is_si)
def test_good_named_add(): """Two quantities with the same named complex units should add together""" furlong = NamedComposedUnit('furlong', ComposedUnit([unit('m')], [], multiplier=201.168), is_si=False) assert (Quantity(2, furlong) + Quantity(2, furlong) == Quantity( 4, furlong))
def test_good_mixed_add(): """Two quantities with the same units should add together even if one is named""" gray = NamedComposedUnit('gray', ComposedUnit([unit('J'), unit('kg')], []), is_si=True) sievert = ComposedUnit([unit('J'), unit('kg')], []) assert (Quantity(2, gray) + Quantity(2, sievert) == Quantity(4, gray)) assert (Quantity(2, sievert) + Quantity(2, gray) == Quantity(4, sievert)) assert Quantity(2, sievert) == Quantity(2, gray)
def define_computer_units(): """Define some units for technology. >>> define_units() >>> unit('GiB')(200) > unit('GB')(200) # bastard marketers True """ NamedComposedUnit('flop', unit('operation') / unit('s'), is_si=True) scaled_unit('B', 'bit', 8, is_si=True) # byte scaled_unit('KiB', 'B', 1024) scaled_unit('MiB', 'KiB', 1024) scaled_unit('GiB', 'MiB', 1024) scaled_unit('TiB', 'GiB', 1024) scaled_unit('PiB', 'TiB', 1024)
def define_volumes(): """Define some common kitchen volumes. >>> from units.quantity import Quantity >>> define_base_si_units() >>> define_volumes() >>> one_litre = Quantity(520, unit('mL')) + Quantity(2, unit('cups')) >>> one_litre == Quantity(1, unit('L')) True """ # Dangerous unit, 3L gives a long int. assert unit('m').is_si() NamedComposedUnit("L", unit("dm") ** 3, is_si=True) scaled_unit('tsp', 'mL', 5) scaled_unit('tbsp', 'mL', 15) scaled_unit('cups', 'mL', 240)
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) # 'in' is a python keyword scaled_unit('ft', 'inch', 12) # foot scaled_unit('yd', 'ft', 3) # yard scaled_unit('fathom', 'ft', 6) scaled_unit('rd', 'yd', 5.5) # rod scaled_unit('fur', 'rd', 40) # furlong scaled_unit('mi', 'fur', 8) # mile scaled_unit('league', 'mi', 3) # nautical scaled_unit measures scaled_unit('NM', 'm', 1852) # Nautical mile scaled_unit('cable', 'NM', 0.1) # chain measure scaled_unit('li', 'inch', 7.92) # link scaled_unit('ch', 'li', 100) # chain # area measure NamedComposedUnit('acre', ComposedUnit([unit('rd'), unit('rd')], [], 160)) # liquid measures NamedComposedUnit('pt', ComposedUnit([unit('inch')] * 3, [], 28.875)) # pint scaled_unit('gi', 'pt', 0.25) # gills scaled_unit('qt', 'pt', 2) # quarts scaled_unit('gal', 'qt', 4) # gallons scaled_unit('fl oz', 'pt', 1.0 / 16) scaled_unit('fl dr', 'fl oz', 1.0 / 8) scaled_unit('minim', 'fl dr', 1.0 / 60) # weight scaled_unit('oz', 'g', 28.375) scaled_unit('lb', 'oz', 16) scaled_unit('ton', 'lb', 2000) scaled_unit('grain', 'lb', 1.0 / 7000) scaled_unit('dr', 'lb', 1.0 / 256) # dram scaled_unit('cwt', 'lb', 100) # hundredweight scaled_unit('dwt', 'grain', 24) # pennyweight scaled_unit('oz t', 'dwt', 20) # ounce troy scaled_unit('lb t', 'oz t', 12) # pound troy # power scaled_unit('hpl', 'W', 746.9999) # mechanical scaled_unit('hpm', 'W', 735.49875) # metric horsepower scaled_unit('hpe', 'W', 746) # electric horsepower. # energy scaled_unit('BTU', 'J', 1055.056, is_si=True) # ISO BTU
def scaled_unit(new_symbol, base_symbol, multiplier, is_si=False): """Shortcut to create and return a new unit that is a scaled_unit multiplication of another.""" return NamedComposedUnit(new_symbol, ComposedUnit([unit(base_symbol)], [], multiplier), is_si)
try: import pytest except ImportError: pass # pylint: enable=F0401,C0321 from units import unit from units.composed_unit import ComposedUnit from units.exception import IncompatibleUnitsError from units.named_composed_unit import NamedComposedUnit from units.quantity import Quantity from units.registry import REGISTRY CVEL = unit('m') / unit('s') VEL = NamedComposedUnit("VEL", CVEL) COMPATIBLE_QUANTITIES = [[Quantity(0, VEL), Quantity(1, VEL)], [Quantity(0, CVEL), Quantity(1, CVEL)]] ALL_UNITS = [ unit('m') / unit('s'), unit('m') * unit('s'), unit('m'), unit('s'), NamedComposedUnit("Hz", ComposedUnit([], [unit('s')])), NamedComposedUnit("L", ComposedUnit([unit('m')] * 3, [], multiplier=0.001)) ] QUANTITIES = [[Quantity(n, u) for n in [0, 1]] for u in ALL_UNITS]