Пример #1
0
def test_misc():
    Quantity.set_prefs(spacer=None,
                       show_label=None,
                       label_fmt=None,
                       label_fmt_full=None)
    q = Quantity(1420405751.786, 'Hz')
    assert q.render(form='eng', show_units=False) == '1.4204e9'

    t = Quantity('1420405751.786 Hz').as_tuple()
    assert t == (1420405751.786, 'Hz')

    t = Quantity('1420405751.786 Hz').render(form='si',
                                             show_units=True,
                                             prec='full')
    assert t == '1.420405751786 GHz'

    s = Quantity('1420405751.786 Hz').render(form='eng',
                                             show_units=True,
                                             prec='full')
    assert s == '1.420405751786e9 Hz'

    f = float(Quantity('1420405751.786 Hz'))
    assert f == 1420405751.786

    t = Quantity('1420405751.786 Hz').render(form='si', show_units=False)
    assert t == '1.4204G'

    s = Quantity('1420405751.786 Hz').render(form='eng', show_units=False)
    assert s == '1.4204e9'

    s = Quantity(1420405751.786, 'Hz').render(form='eng',
                                              show_units=False,
                                              prec='full')
    assert s == '1.420405751786e9'

    f = Quantity('14204.05751786MHz').render(form='si',
                                             show_units=False,
                                             prec='full')
    assert f == '14.20405751786G'

    q = Quantity('1420405751.786 Hz', units='HZ').render()
    assert q == '1.4204 GHZ'

    q = Quantity('1420405751.786 Hz')
    assert q.is_nan() is None

    q = Quantity('1420405751.786 Hz')
    assert q.is_infinite() is None

    q = Quantity('NaN Hz')
    assert q.is_nan() == 'NaN'

    q = Quantity('NaN Hz')
    q.nan = 'nan'
    assert q.is_nan() == 'nan'

    q = Quantity('NaN Hz')
    assert q.is_infinite() is None

    q = Quantity('inf Hz')
    assert q.is_nan() is None

    q = Quantity('inf Hz')
    assert q.is_infinite() == 'inf'

    q = Quantity('inf Hz')
    q.inf = '∞'
    assert q.is_infinite() == '∞'

    q = Quantity('∞ Hz')
    assert q.is_infinite() == 'inf'

    q = Quantity('$∞')
    assert q.is_infinite() == 'inf'

    q = Quantity('∞Ω')
    assert q.is_infinite() == 'inf'

    # check the various formats for assignment recognition
    q = Quantity('f_hy = 1420405751.786 Hz -- frequency of hydrogen line')
    assert q.render(
        show_label='f') == 'f_hy = 1.4204 GHz -- frequency of hydrogen line'
    assert q.name == 'f_hy'
    assert q.desc == 'frequency of hydrogen line'

    q = Quantity('f_hy: 1420405751.786 Hz # frequency of hydrogen line')
    assert q.render(
        show_label='f') == 'f_hy = 1.4204 GHz -- frequency of hydrogen line'
    assert q.name == 'f_hy'
    assert q.desc == 'frequency of hydrogen line'

    q = Quantity('f_hy = 1420405751.786 Hz // frequency of hydrogen line')
    assert q.render(
        show_label='f') == 'f_hy = 1.4204 GHz -- frequency of hydrogen line'
    assert q.name == 'f_hy'
    assert q.desc == 'frequency of hydrogen line'

    q = Quantity('f_hy = 1420405751.786 Hz')
    assert q.render(show_label='f') == 'f_hy = 1.4204 GHz'
    assert q.name == 'f_hy'
    assert q.desc == ''

    q = Quantity('1420405751.786 Hz // frequency of hydrogen line')
    assert q.render(show_label='f') == '1.4204 GHz'
    assert q.name == ''
    assert q.desc == 'frequency of hydrogen line'

    q = Quantity('1420405751.786 Hz')
    assert q.render(show_label='f') == '1.4204 GHz'
    assert q.name == ''
    assert q.desc == ''

    if py3:
        # check tight_units
        q = Quantity('90°')
        assert q.render() == '90°'

        q = Quantity('80°F')
        assert q.render() == '80 °F'

        q = Quantity('80°F')
        assert q.render() == '80 °F'
        q.tight_units = '''' % ° ' " ′ ″ °F °C '''.split()
        assert q.render() == '80°F'
Пример #2
0
def test_misc():
    Quantity.reset_prefs()
    Quantity.set_prefs(spacer=None,
                       show_label=None,
                       label_fmt=None,
                       label_fmt_full=None)
    q = Quantity(1420405751.786, 'Hz')
    assert q.render(form='eng', show_units=False) == '1.4204e9'

    t = Quantity('1420405751.786 Hz').as_tuple()
    assert t == (1420405751.786, 'Hz')

    t = Quantity('1420405751.786 Hz').render(form='si',
                                             show_units=True,
                                             prec='full')
    assert t == '1.420405751786 GHz'

    s = Quantity('1420405751.786 Hz').render(form='eng',
                                             show_units=True,
                                             prec='full')
    assert s == '1.420405751786e9 Hz'

    f = float(Quantity('1420405751.786 Hz'))
    assert f == 1420405751.786

    t = Quantity('1420405751.786 Hz').render(form='si', show_units=False)
    assert t == '1.4204G'

    s = Quantity('1420405751.786 Hz').render(form='eng', show_units=False)
    assert s == '1.4204e9'

    s = Quantity(1420405751.786, 'Hz').render(form='eng',
                                              show_units=False,
                                              prec='full')
    assert s == '1.420405751786e9'

    f = Quantity('14204.05751786MHz').render(form='si',
                                             show_units=False,
                                             prec='full')
    assert f == '14.20405751786G'

    q = Quantity('1420405751.786 Hz', units='HZ').render()
    assert q == '1.4204 GHZ'

    q = Quantity('1420405751.786 Hz')
    assert q.is_nan() is None

    q = Quantity('1420405751.786 Hz')
    assert q.is_infinite() is None

    q = Quantity('NaN Hz')
    assert q.is_nan() == 'NaN'

    q = Quantity('NaN Hz')
    q.nan = 'nan'
    assert q.is_nan() == 'nan'

    q = Quantity('NaN Hz')
    assert q.is_infinite() is None

    q = Quantity('inf Hz')
    assert q.is_nan() is None

    q = Quantity('inf Hz')
    assert q.is_infinite() == 'inf'

    q = Quantity('inf Hz')
    q.inf = '∞'
    assert q.is_infinite() == '∞'

    q = Quantity('∞ Hz')
    assert q.is_infinite() == 'inf'

    q = Quantity('$∞')
    assert q.is_infinite() == 'inf'

    q = Quantity('∞Ω')
    assert q.is_infinite() == 'inf'

    # check the various formats for assignment recognition
    q = Quantity('f_hy = 1420405751.786 Hz — frequency of hydrogen line')
    assert q.render(
        show_label='f') == 'f_hy = 1.4204 GHz — frequency of hydrogen line'
    assert q.name == 'f_hy'
    assert q.desc == 'frequency of hydrogen line'

    q = Quantity('f_hy: 1420405751.786 Hz # frequency of hydrogen line')
    assert q.render(
        show_label='f') == 'f_hy = 1.4204 GHz — frequency of hydrogen line'
    assert q.name == 'f_hy'
    assert q.desc == 'frequency of hydrogen line'

    q = Quantity('f_hy = 1420405751.786 Hz // frequency of hydrogen line')
    assert q.render(
        show_label='f') == 'f_hy = 1.4204 GHz — frequency of hydrogen line'
    assert q.name == 'f_hy'
    assert q.desc == 'frequency of hydrogen line'

    q = Quantity('f_hy = 1420405751.786 Hz')
    assert q.render(show_label='f') == 'f_hy = 1.4204 GHz'
    assert q.name == 'f_hy'
    assert q.desc == ''

    q = Quantity('1420405751.786 Hz // frequency of hydrogen line')
    assert q.render(show_label='f') == '1.4204 GHz'
    assert q.name == ''
    assert q.desc == 'frequency of hydrogen line'

    q = Quantity('1420405751.786 Hz')
    assert q.render(show_label='f') == '1.4204 GHz'
    assert q.name == ''
    assert q.desc == ''

    # check some exceptions
    with pytest.raises(ValueError) as exception:
        q = Quantity('f_hy = 1420405751;786 Hz')
    assert str(exception.value) == '1420405751;786 Hz: not a valid number.'
    assert isinstance(exception.value, InvalidNumber)
    assert isinstance(exception.value, QuantiPhyError)
    assert isinstance(exception.value, ValueError)
    assert exception.value.args == ('1420405751;786 Hz', )

    # check some exceptions
    with pytest.raises(ValueError) as exception:
        q = Quantity('# nan')
    assert str(exception.value) == '# nan: not a valid number.'
    assert isinstance(exception.value, InvalidNumber)
    assert isinstance(exception.value, QuantiPhyError)
    assert isinstance(exception.value, ValueError)
    assert exception.value.args == ('# nan', )

    # check tight_units
    q = Quantity('90°')
    assert q.render() == '90°'

    q = Quantity('80°F')
    assert q.render() == '80 °F'

    q = Quantity('80°F')
    assert q.render() == '80 °F'
    q.tight_units = '''' % ° ' " ′ ″ °F °C '''.split()
    assert q.render() == '80°F'