Ejemplo n.º 1
0
def test_good_named_sub():
    """Two quantities with the same named complex units should sub together"""

    furlong = unit('fur')

    assert (Quantity(4, furlong) - Quantity(2, furlong) == Quantity(
        2, furlong))
Ejemplo n.º 2
0
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))
Ejemplo n.º 3
0
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.00 m'
Ejemplo n.º 4
0
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'))
Ejemplo n.º 5
0
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))
Ejemplo n.º 6
0
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))
Ejemplo n.º 7
0
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')))
Ejemplo n.º 8
0
def test_valid_named_to_composed():
    """Named units should convert to the composed equivalents."""

    hertz = unit('Hz')
    one_hz_in_per_second = hertz.composed_unit(Quantity(1, hertz))

    assert one_hz_in_per_second == Quantity(1, hertz)
    assert str(one_hz_in_per_second) == '1.00 1 / s'
Ejemplo n.º 9
0
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.00 km'
Ejemplo n.º 10
0
def test_valid_composed_to_named():
    """Composed units should convert to named equivalents."""
    hertz = unit('Hz')
    per_second = hertz.composed_unit

    one_hz = hertz(Quantity(1, per_second))

    assert one_hz == Quantity(1, hertz)
    assert str(one_hz) == '1.00 Hz'
Ejemplo n.º 11
0
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.00 m'
Ejemplo n.º 12
0
def test_good_composed_add():
    """Two quantities with the same complex units should add together"""

    assert (Quantity(2,
                     unit('m') / unit('s')) +
            Quantity(3,
                     unit('m') / unit('s')) == Quantity(
                         5,
                         unit('m') / unit('s')))
Ejemplo n.º 13
0
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.00 Sv'
Ejemplo n.º 14
0
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)
Ejemplo n.º 15
0
 def __call__(self, quantity):
     """Overload the function call operator to convert units."""
     if not hasattr(quantity, 'unit'):
         return Quantity(quantity, self)
     elif compatible(self, quantity.unit):
         return Quantity(
             quantity.num * quantity.unit.squeeze() / self.squeeze(), self)
     else:
         raise IncompatibleUnitsError()
Ejemplo n.º 16
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))
Ejemplo n.º 17
0
 def translate(self, source_quantity, target_unit=None):
     if not isinstance(source_quantity, Quantity):
         source_quantity = Quantity(source_quantity, self.source_unit)
     target_unit = target_unit or self.target_unit
     converter_source_quantity = strict_convert_to_unit(
         source_quantity, self.source_unit)
     converter_target_quantity = Quantity(
         self.forward_converter(converter_source_quantity),
         self.target_unit)
     target_quantity = strict_convert_to_unit(converter_target_quantity,
                                              target_unit)
     return target_quantity
Ejemplo n.º 18
0
def test_valid_composed_to_composed():
    """Valid composed units in terms of others."""
    metric_vel = unit('km') / unit('h')
    imp_vel = unit('mi') / unit('h')

    highway_kph = Quantity(100, metric_vel)
    highway_mph = Quantity(62.1371192237334, imp_vel)

    assert str(highway_kph) == '100.00 km / h'
    assert str(highway_mph) == '62.14 mi / h'

    assert within_epsilon(imp_vel(highway_kph), highway_mph)

    assert str(imp_vel(highway_kph)) == '62.14 mi / h'
Ejemplo n.º 19
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)
Ejemplo n.º 20
0
def test_pow():
    """Exponentiation of quantities."""
    REGISTRY.clear()
    define_units()

    m_unit = unit('m')
    m_quant = Quantity(2, m_unit)

    assert (m_quant ** 2 ==
            m_quant * m_quant ==
            pow(m_quant, 2))

    cm_unit = unit('cm')
    cm_quant = Quantity(2, cm_unit)

    assert within_epsilon(cm_quant ** 2, cm_quant * cm_quant)
    assert within_epsilon(cm_quant ** 2, pow(cm_quant, 2))
Ejemplo n.º 21
0
def test_good_mixed_sub():
    """Two quantities with the same units should sub together
    even if one is named"""

    gray = unit('Gy')

    sievert = unit('Sv').composed_unit

    assert (Quantity(4, gray) - Quantity(2, sievert) == Quantity(2, gray))

    assert (Quantity(4, sievert) - Quantity(2, gray) == Quantity(2, sievert))

    assert Quantity(2, sievert) == Quantity(2, gray)
Ejemplo n.º 22
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)
Ejemplo n.º 23
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)
Ejemplo n.º 24
0
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))
Ejemplo n.º 25
0
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))
Ejemplo n.º 26
0
def test_simple_multiply_quantity():
    """Simple multiplication of quantities"""
    assert (Quantity(2, unit('m')) *
            Quantity(2, unit('s')) ==
            Quantity(4, unit('m') * unit('s')))

    assert (Quantity(2, unit('s')) *
            Quantity(2, unit('m')) ==
            Quantity(4, unit('m') * unit('s')))
Ejemplo n.º 27
0
def test_good_sub_w_mults():
    """Two quantities with compatible units should sub together
    even when they have different multipliers"""

    mile = unit('mi').composed_unit
    kilometre = unit('km').composed_unit

    m_on_left = Quantity(1, mile) - Quantity(1, kilometre)
    km_on_left = Quantity(1, kilometre) - Quantity(1, mile)
    m_on_left_diff = Quantity(609.344, unit('m'))
    km_on_left_diff = Quantity(-609.344, unit('m'))

    assert within_epsilon(m_on_left, m_on_left_diff)
    assert within_epsilon(km_on_left, km_on_left_diff)
    assert within_epsilon(m_on_left_diff, -km_on_left_diff)
Ejemplo n.º 28
0
def subtract_bad():
    """Incompatible leaf units should not subtract from one another."""

    metres = Quantity(2, unit('m'))
    seconds = Quantity(2, unit('s'))
    return metres - seconds
Ejemplo n.º 29
0
def test_good_simple_sub():
    """Should be able to subtract compatible Quantities."""

    assert (Quantity(4, unit('m')) - Quantity(2, unit('m')) == Quantity(
        2, unit('m')))
Ejemplo n.º 30
0
def add_bad():
    """Incompatible leaf units should not add together."""

    metres = Quantity(2, unit('m'))
    seconds = Quantity(2, unit('s'))
    return metres + seconds