예제 #1
0
def test_scale():
    Quantity.reset_prefs()
    Quantity.set_prefs(spacer=None,
                       show_label=None,
                       label_fmt=None,
                       label_fmt_full=None)
    q1 = Quantity('3ns')
    q2 = Quantity('2')
    v2 = 2
    assert str(q1.scale(q2)) == '6 ns'
    assert repr(q1.scale(q2)) == "Quantity('6 ns')"
    assert str(q1.scale(v2)) == '6 ns'
    assert repr(q1.scale(v2)) == "Quantity('6 ns')"

    q1.name = 'period'
    q3 = q1.scale(q2).name == 'period'
    q1.desc = 'duration of one cycle'
    assert q1.scale(q2).name == 'period'
    assert q1.scale(q2).desc == 'duration of one cycle'

    class Dollars(Quantity):
        units = '$'
        prec = 2
        form = 'fixed'
        show_commas = True
        minus = Quantity.minus_sign
        strip_zeros = False

    class Cents(Dollars):
        units = '¢'
        prec = 0

    print(UnitConversion(Dollars, Cents, 0.01))

    total = Dollars(1)
    assert str(total) == '$1.00'
    total = total.scale(1000000)
    assert str(total) == '$1,000,000.00'

    class WholeDollars(Dollars):
        prec = 0

    total = WholeDollars(20)
    assert str(total) == '$20'
    total = total.scale(1000000)
    assert str(total) == '$20,000,000'
    total = total.scale(5, Dollars)
    assert str(total) == '$100,000,000.00'

    total = total.scale(Cents)
    assert str(total) == '10,000,000,000 ¢'
    assert type(total) == Cents
예제 #2
0
def test_add():
    Quantity.set_prefs(spacer=None,
                       show_label=None,
                       label_fmt=None,
                       label_fmt_full=None)
    q1 = Quantity('1ns')
    q2 = Quantity('2ns')
    assert str(q1.add(q2)) == '3 ns'
    assert repr(q1.add(q2)) == "Quantity('3 ns')"
    assert str(q2.add(q1)) == '3 ns'
    assert repr(q2.add(q1)) == "Quantity('3 ns')"
    assert str(q1.add(q2, check_units=False)) == '3 ns'
    assert str(q1.add(q2, check_units=True)) == '3 ns'
    assert str(q1.add(q2, check_units='strict')) == '3 ns'
    assert str(q1.add(2e-9, check_units=False)) == '3 ns'
    assert str(q1.add(2e-9, check_units=True)) == '3 ns'
    with pytest.raises(TypeError) as exception:
        q1.add(2e-9, check_units='strict')
    q3 = Quantity('2nm')
    assert str(q1.add(q3, check_units=False)) == '3 ns'
    with pytest.raises(TypeError) as exception:
        q1.add(q3, check_units=True)
    with pytest.raises(TypeError) as exception:
        q1.add(q3, check_units='strict')

    q1.name = 'period'
    assert q1.add(q2).name == 'period'
    q1.desc = 'duration of one cycle'
    assert q1.add(q2).name == 'period'
    assert q1.add(q2).desc == 'duration of one cycle'

    class Dollars(Quantity):
        units = '$'
        prec = 2
        form = 'fixed'
        show_commas = True
        if sys.version_info.major == 3:
            minus = Quantity.minus_sign
        strip_zeros = False

    total = Dollars(0)
    assert str(total) == '$0.00'
    total = total.add(1000000)
    assert str(total) == '$1,000,000.00'
    total = total.add(Quantity('-2MHz'), check_units=False)
    if sys.version_info.major == 3:
        assert str(total) == '−$1,000,000.00'
    else:
        assert str(total) == '-$1,000,000.00'
    with pytest.raises(IncompatibleUnits) as exception:
        total.add(Quantity('-2MHz'), check_units=True)
    total = total.add(Quantity('$6M'), check_units=True)
    assert str(total) == '$5,000,000.00'

    class WholeDollars(Dollars):
        prec = 0

    total = WholeDollars(0)
    total.name = 'total'
    assert str(total) == '$0'
    total = total.add(1000000)
    assert str(total) == '$1,000,000'
    assert total.name == 'total'
    total = total.add(Quantity('-2MHz'), check_units=False)
    if sys.version_info.major == 3:
        assert str(total) == '−$1,000,000'
    else:
        assert str(total) == '-$1,000,000'
    assert total.name == 'total'
    with pytest.raises(IncompatibleUnits) as exception:
        total.add(Quantity('-2MHz'), check_units=True)
    total = total.add(Quantity('$6M'), check_units=True)
    assert str(total) == '$5,000,000'
    assert total.name == 'total'