示例#1
0
def test_good_sub_w_mult():
    """Two quantities with same units should sub together when
    they have the same multiplier"""

    moon = ComposedUnit([unit('day')], [], multiplier=28)
    lunar_cycle = ComposedUnit([unit('day')], [], multiplier=28)

    assert (Quantity(2, moon) - Quantity(1, lunar_cycle) == Quantity(1, moon))

    assert (Quantity(2, lunar_cycle) - Quantity(1, moon) == Quantity(
        1, lunar_cycle))

    assert Quantity(1, moon) == Quantity(1, lunar_cycle)
示例#2
0
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)
示例#3
0
文件: __init__.py 项目: jyn514/units
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)
示例#4
0
def test_good_add_w_mults():
    """Two quantities with compatible units should add together
    even when they have different multipliers"""

    mile = ComposedUnit([unit('m')], [], multiplier=1609.344)
    kilometre = ComposedUnit([unit('m')], [], multiplier=1000)

    m_on_left = Quantity(1, mile) + Quantity(1, kilometre)
    km_on_left = Quantity(1, kilometre) + Quantity(1, mile)
    manual_sum = Quantity(2609.344, unit('m'))

    assert within_epsilon(m_on_left, km_on_left)
    assert within_epsilon(km_on_left, m_on_left)
    assert within_epsilon(manual_sum, m_on_left)
    assert within_epsilon(manual_sum, km_on_left)
    assert within_epsilon(m_on_left, manual_sum)
    assert within_epsilon(km_on_left, manual_sum)
示例#5
0
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))
示例#6
0
文件: leaf_unit.py 项目: jyn514/units
 def __pow__(self, exponent):
     return ComposedUnit([self] * exponent, [], 1)
示例#7
0
文件: leaf_unit.py 项目: jyn514/units
 def invert(self):
     """Return (this unit)^-1"""
     return ComposedUnit([], [self])
示例#8
0
文件: leaf_unit.py 项目: jyn514/units
 def __truediv__(self, other):
     if hasattr(other, "invert"):
         return other.invert() * self
     else:
         return ComposedUnit([self], [other])
示例#9
0
文件: leaf_unit.py 项目: jyn514/units
 def __mul__(self, other):
     if hasattr(other, "numer"):
         return other * self
     else:
         return ComposedUnit([self, other], [])
示例#10
0
def test_unbox_to_num():
    """Test that composed units collapse properly to numbers."""
    assert ComposedUnit([unit('m')], [unit('m')], 8) == 8
示例#11
0
def test_unbox_to_leaf():
    """Test that composed units collaple properly to leaf units."""
    assert ComposedUnit([unit('m')], []) == unit('m')
示例#12
0
 def __truediv__(self, other):
     return ComposedUnit([self], [other])
示例#13
0
 def __mul__(self, other):
     return ComposedUnit([self, other], [])
示例#14
0
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
示例#15
0
文件: __init__.py 项目: jyn514/units
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)
示例#16
0
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]
FLAT_QUANTITIES = sum(QUANTITIES, [])


def less_than(quant1, quant2):
    """Binary function to call the operator"""
    return quant1 < quant2


def less_than_or_eq(quant1, quant2):
    """Binary function to call the operator"""
    return quant1 <= quant2