Example #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
Example #2
0
def test_simple_scaling():
    Quantity.reset_prefs()
    with Quantity.prefs(
        spacer=None, show_label=None, label_fmt=None, label_fmt_full=None
    ):
        q=Quantity('1kg', scale=2)
        qs=Quantity('2ms')
        assert q.render() == '2 kg'
        assert qs.render() == '2 ms'
        assert q.render(scale=0.001) == '2 g'
        assert str(q.scale(0.001)) == '2 g'
        assert q.render(scale=qs) == '4 g'
        assert str(q.scale(qs)) == '4 g'
        with pytest.raises(KeyError) as exception:
            q.render(scale='fuzz')
        assert str(exception.value) == "unable to convert between 'fuzz' and 'g'."
        assert isinstance(exception.value, UnknownConversion)
        assert isinstance(exception.value, QuantiPhyError)
        assert isinstance(exception.value, KeyError)
        assert exception.value.args == ('fuzz', 'g')
        with pytest.raises(KeyError) as exception:
            q.scale('fuzz')
        assert str(exception.value) == "unable to convert between 'fuzz' and 'g'."
        assert isinstance(exception.value, UnknownConversion)
        assert isinstance(exception.value, QuantiPhyError)
        assert isinstance(exception.value, KeyError)
        assert exception.value.args == ('fuzz', 'g')

        q=Quantity('1', units='g', scale=1000)
        assert q.render() == '1 kg'
        assert q.render(scale=(0.0022046, 'lbs')) == '2.2046 lbs'
        assert str(q.scale((0.0022046, 'lbs'))) == '2.2046 lbs'

        q=Quantity('1', units='g', scale=qs)
        assert q.render() == '2 mg'

        q=Quantity('1', scale=(1000, 'g'))
        assert q.render() == '1 kg'
        assert q.render(scale=lambda v, u: (0.0022046*v, 'lbs')) == '2.2046 lbs'

        def dB(v, u):
            return 20*math.log(v, 10), 'dB'+u

        def adB(v, u):
            return pow(10, v/20), u[2:] if u.startswith('dB') else u

        q=Quantity('-40 dBV', scale=adB)
        assert q.render() == '10 mV'
        assert q.render(scale=dB) == '-40 dBV'
        assert str(q.scale(dB)) == '-40 dBV'
Example #3
0
def test_scale():
    Quantity.reset_prefs()
    secs = Quantity('86400 s')
    days = secs.scale('day')
    assert secs.render() == '86.4 ks'
    assert days.render() == '1 day'