def __init__(self, val, unc, unit, symbol=None): if issubclass(type(val), AbsPhysicsQuantity): self.val = copy(val.val) self.unc = copy(val.unc) elif isinstance(val, str) and unc == 0: self.val = ureg.Quantity(val) self.unc = ureg.Quantity(0., self.val.units) elif not isinstance(val, ureg.Quantity): self.val = val * ureg.Quantity(unit) self.unc = unc * ureg.Quantity(unit) else: self.val = val if isinstance(unc, ureg.Quantity): self.unc = unc else: self.unc = ureg.Quantity(unc, self.val.units) self._symbol = symbol
def __mul__(self, other): if isinstance(other, Number): return self.__scalar_mul__(other) elif isinstance(other, self.__class__): return self.__class__(self.val * other.val, self.unc * other.unc) elif issubclass(type(other), AbsPhysicsQuantity): tmp = deepcopy(self) tmp.val = self.val * other.val tmp.unc = ureg.Quantity(-1., self.val.units * other.val.units) return tmp
def __setstate__(self, state): self.val = ureg.Quantity(state['val'].magnitude, state['val'].units) self.unc = ureg.Quantity(state['unc'].magnitude, state['unc'].units) self._symbol = state["_symbol"]
def test_generic_multiplication(self): generic_quantity = PQ.create_generic("foo", 10., 2., ureg.Bq) new_value = generic_quantity * 5. self.assertTrue(new_value.val, ureg.Quantity(50., ureg.Bq))
def test_generic_pq(self): generic_quantity = PQ.create_generic("foo", 10., 2., ureg.Bq) self.assertEqual(generic_quantity.__class__.__name__, "foo") self.assertEqual(generic_quantity.val, ureg.Quantity(10., ureg.Bq)) self.assertEqual(generic_quantity.unc, ureg.Quantity(2., ureg.Bq))
def test_production_yield_create(self): prod_yield = PQ.ProductionYield(10., 2.) res = ureg.Quantity(10., 1./ureg.second) self.assertEqual(prod_yield.val, res)