Exemplo n.º 1
0
def test_aggregation():
    a = PhysicalQuantity(1, 'm')
    b = PhysicalQuantity(1, 's')
    c = a * b
    p = [
        a.unit.powers[i] + b.unit.powers[i] for i in range(len(a.unit.powers))
    ]
    assert p == c.unit.powers
Exemplo n.º 2
0
def test_calculation_2():
    """ test addition """
    g = dBQuantity(0, 'dBm')
    a = dBQuantity(0, 'dBm')
    ga = PhysicalQuantity(2, 'mW').dB
    assert a + g == ga
    assert g + a == ga
def test_complex_attr():
    """ test getting real/imaginary part 
        real/imag attributes of complex numbers always return floats...
    """
    a = (1.0 + 2.0j) * PhysicalQuantity(1, 'V')
    assert str(a.real) == '1.0 V'
    assert str(a.imag) == '2.0 V'
Exemplo n.º 4
0
def test_to_dict():
    a = PhysicalQuantity(1, 'm')
    d = a.unit.to_dict
    assert type(d) is dict
    assert 'base_exponents' in d.keys()
    assert 'factor' in d.keys()
    assert 'offset' in d.keys()
    assert 'name' in d.keys()
Exemplo n.º 5
0
def test_pow_3():
    """Offsets are not allowed"""
    addunit(
        PhysicalUnit('degX',
                     1., [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
                     offset=273.15))
    a = PhysicalQuantity(1, 'degX')
    a.unit.offset = 1.1
    a**2
Exemplo n.º 6
0
def test_addunit_1():
    addunit(
        PhysicalUnit('degC',
                     1., [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
                     offset=273.15,
                     url='https://en.wikipedia.org/wiki/Celsius',
                     verbosename='degrees Celsius'))
    a = PhysicalQuantity(1, 'degC')
    assert (type(a.unit) == PhysicalUnit)
Exemplo n.º 7
0
def test_to_json():
    a = PhysicalQuantity(1, 'm')
    j = a.unit.to_json
    u = json.loads(j)
    assert type(u) is dict
    assert 'PhysicalUnit' in u.keys()
    d = u['PhysicalUnit']
    assert 'base_exponents' in d.keys()
    assert 'factor' in d.keys()
    assert 'offset' in d.keys()
    assert 'name' in d.keys()
Exemplo n.º 8
0
def test_acres():
    a = PhysicalQuantity(1, 'acres')
    assert(a.base.unit.name == 'm**2')
    assert_almost_equal(a.to('m**2').value, 4046.8564224)
Exemplo n.º 9
0
def test_repr():
    a = PhysicalQuantity(1, 'm').unit
    b = a.__repr__()
    assert (b == '<PhysicalUnit m>')
Exemplo n.º 10
0
def test_pow_2():
    """Only integer exponents"""
    a = PhysicalQuantity(1, 'm').unit
    a**2.0
Exemplo n.º 11
0
def test_unit_multiplication_2():
    add_composite_unit('offsma', 1, 'm')
    a = PhysicalQuantity(1, 'offsma')
    b = PhysicalQuantity(1, 'm')
    a.unit.offset = 1
    a * b
Exemplo n.º 12
0
from PhysicalQuantities import PhysicalQuantity as PQ
rho = PQ('2.7E+3 kg/m**3')
kappa = PQ('200 W/(m*K)')
c = PQ('900 J/(K*kg)')
beta = kappa/(rho*c)
beta = PQ('%g m**2/s' % beta.getValue())
print beta
Exemplo n.º 13
0
def test_unit_inversion():
    a = PhysicalQuantity(1, 'm')
    b = 1 / a
    assert np.any(np.array(b.unit.powers) - np.array(a.unit.powers) == 0)
Exemplo n.º 14
0
def test_unit_multiplication_1():
    a = PhysicalQuantity(1, 'mm')
    b = PhysicalQuantity(1, 'cm')
    assert str((a * b).base.unit) == "m^2"
Exemplo n.º 15
0
def test_euro():
    a = PhysicalQuantity(1, 'Euro')
    assert_almost_equal(a.value, 1)
Exemplo n.º 16
0
def test_findunit_1():
    a = findunit('mm')
    b = PhysicalQuantity(1, 'mm').unit
    assert (a == b)
Exemplo n.º 17
0
def test_unit_division_1():
    a = PhysicalQuantity(1, 'mm')
    b = PhysicalQuantity(1, 'cm')
    assert type(a / b) == float
Exemplo n.º 18
0
def test_from_json():
    a = PhysicalQuantity(1, 'm')
    j = a.unit.to_json
    b = PhysicalUnit.from_json(j)
    assert a.unit == b
Exemplo n.º 19
0
def test_isphysicalunit():
    a = PhysicalQuantity(1, 'm')
    assert isphysicalunit(a.unit) is True
    assert isphysicalunit(1) is False
Exemplo n.º 20
0
def test_conversion_tuple_to_2():
    # raises UnitError
    a = PhysicalQuantity(1, 'm')
    b = PhysicalQuantity(1, 's')
    assert a.unit.conversion_tuple_to(b.unit) == (1000.0, 0.0)
Exemplo n.º 21
0
def test_conversion_tuple_to():
    a = PhysicalQuantity(2, 'm')
    b = PhysicalQuantity(3, 'mm')
    assert a.unit.conversion_tuple_to(b.unit) == (1000.0, 0.0)
Exemplo n.º 22
0
def test_conversion_factor_to():
    a = PhysicalQuantity(1, 'm')
    b = PhysicalQuantity(1, 'mm')
    assert a.unit.conversion_factor_to(b.unit) == 1000
Exemplo n.º 23
0
def test_add_composite_unit():
    add_composite_unit('test', 4.92892159375, 'cm**3')
    a = PhysicalQuantity(1, 'test')
    assert (type(a.unit) == PhysicalUnit)
Exemplo n.º 24
0
def test_latex_repr():
    a = PhysicalQuantity(1, 'm').unit
    b = a.latex
    assert (b == r'\text{m}')
Exemplo n.º 25
0
def test_convertvalue():
    a = PhysicalQuantity(1, 'm').unit
    b = PhysicalQuantity(1, 'mm').unit
    convertvalue([1], a, b)
Exemplo n.º 26
0
def test_gt_2():
    a = PhysicalQuantity(1, 'm').unit
    b = PhysicalQuantity(1, 'mm').unit
    assert (a > b)
Exemplo n.º 27
0
def test_unit_division_2():
    add_composite_unit('offsm', 1, 'm')
    a = PhysicalQuantity(1, 'offsm')
    b = PhysicalQuantity(1, 'm')
    a.unit.offset = 1
    a / b
Exemplo n.º 28
0
def test_le_1():
    """Only same units can be compared"""
    a = PhysicalQuantity(1, 'm').unit
    b = PhysicalQuantity(1, 's').unit
    assert (a <= b)
Exemplo n.º 29
0
def test_floordiv():
    a = PhysicalQuantity(8, 'Euro')
    b = PhysicalQuantity(2, 'Euro')
    assert a // b == 4
Exemplo n.º 30
0
def test_le_2():
    a = PhysicalQuantity(1, 'mm').unit
    b = PhysicalQuantity(1, 'm').unit
    assert (a <= b)
Exemplo n.º 31
0
from PhysicalQuantities import PhysicalQuantity as PQ

v = PQ('120 yd/min')  # velocity
t = PQ('1 h')  # time
s = v * t  # distance
print s  # s is string
s.convertToUnit('m')
print s
print s.getValue()  # florat
print s.getUnitName()  # string
v.convertToUnit('km/h')
print v
v.convertToUnit('m/s')
print v

c = PQ('1 cal/g/K')
c.convertToUnit('J/(g*K)')
print 'Specific heat capacity of water (at const. pressure):', c

d = PQ('1000 kg/m**3')
d.convertToUnit('oz/floz')
print d
d = PQ('1.05 oz/floz')
d.convertToUnit('kg/m**3')
print d
Exemplo n.º 32
0
def test_pow_1():
    """Only integer exponents"""
    a = PhysicalQuantity(1, 'm^2').unit
    b = PhysicalQuantity(1, 'm').unit
    assert (a**0.5 == b)
Exemplo n.º 33
0
def test_repr():
    a = PhysicalQuantity(1, 'm').unit
    b = a.__repr__()
    assert(b == '<PhysicalUnit m>')
Exemplo n.º 34
0
from PhysicalQuantities import PhysicalQuantity as PQ
v = PQ('120 yd/min')   # velocity
t = PQ('1 h')          # time
s = v*t                # distance
print s                # s is string
s.convertToUnit('m')
print s
print s.getValue()     # florat
print s.getUnitName()  # string
v.convertToUnit('km/h')
print v
v.convertToUnit('m/s')
print v

c = PQ('1 cal/g/K')
c.convertToUnit('J/(g*K)')
print 'Specific heat capacity of water (at const. pressure):', c

d = PQ('1000 kg/m**3')
d.convertToUnit('oz/floz')
print d
d = PQ('1.05 oz/floz')
d.convertToUnit('kg/m**3')
print d