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
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'
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()
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
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)
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()
def test_acres(): a = PhysicalQuantity(1, 'acres') assert(a.base.unit.name == 'm**2') assert_almost_equal(a.to('m**2').value, 4046.8564224)
def test_repr(): a = PhysicalQuantity(1, 'm').unit b = a.__repr__() assert (b == '<PhysicalUnit m>')
def test_pow_2(): """Only integer exponents""" a = PhysicalQuantity(1, 'm').unit a**2.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
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
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)
def test_unit_multiplication_1(): a = PhysicalQuantity(1, 'mm') b = PhysicalQuantity(1, 'cm') assert str((a * b).base.unit) == "m^2"
def test_euro(): a = PhysicalQuantity(1, 'Euro') assert_almost_equal(a.value, 1)
def test_findunit_1(): a = findunit('mm') b = PhysicalQuantity(1, 'mm').unit assert (a == b)
def test_unit_division_1(): a = PhysicalQuantity(1, 'mm') b = PhysicalQuantity(1, 'cm') assert type(a / b) == float
def test_from_json(): a = PhysicalQuantity(1, 'm') j = a.unit.to_json b = PhysicalUnit.from_json(j) assert a.unit == b
def test_isphysicalunit(): a = PhysicalQuantity(1, 'm') assert isphysicalunit(a.unit) is True assert isphysicalunit(1) is False
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)
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)
def test_conversion_factor_to(): a = PhysicalQuantity(1, 'm') b = PhysicalQuantity(1, 'mm') assert a.unit.conversion_factor_to(b.unit) == 1000
def test_add_composite_unit(): add_composite_unit('test', 4.92892159375, 'cm**3') a = PhysicalQuantity(1, 'test') assert (type(a.unit) == PhysicalUnit)
def test_latex_repr(): a = PhysicalQuantity(1, 'm').unit b = a.latex assert (b == r'\text{m}')
def test_convertvalue(): a = PhysicalQuantity(1, 'm').unit b = PhysicalQuantity(1, 'mm').unit convertvalue([1], a, b)
def test_gt_2(): a = PhysicalQuantity(1, 'm').unit b = PhysicalQuantity(1, 'mm').unit assert (a > b)
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
def test_le_1(): """Only same units can be compared""" a = PhysicalQuantity(1, 'm').unit b = PhysicalQuantity(1, 's').unit assert (a <= b)
def test_floordiv(): a = PhysicalQuantity(8, 'Euro') b = PhysicalQuantity(2, 'Euro') assert a // b == 4
def test_le_2(): a = PhysicalQuantity(1, 'mm').unit b = PhysicalQuantity(1, 'm').unit assert (a <= b)
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
def test_pow_1(): """Only integer exponents""" a = PhysicalQuantity(1, 'm^2').unit b = PhysicalQuantity(1, 'm').unit assert (a**0.5 == b)
def test_repr(): a = PhysicalQuantity(1, 'm').unit b = a.__repr__() assert(b == '<PhysicalUnit m>')
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