def test_issue66b(self): ureg = UnitRegistry() self.assertEqual( ureg.get_base_units(ureg.kelvin), (1.0, ureg.Unit(UnitsContainer({"kelvin": 1}))), ) self.assertEqual( ureg.get_base_units(ureg.degC), (1.0, ureg.Unit(UnitsContainer({"kelvin": 1}))), )
def test_attach_units_given_unit_objs(self, example_unitless_da): orig = example_unitless_da ureg = UnitRegistry(force_ndarray=True) result = orig.pint.quantify(ureg.Unit("m"), unit_registry=ureg) assert_array_equal(result.data.magnitude, orig.data) assert result.data.units == ureg.Unit("m")
def make_resizable(self): self._magnitude = ResizableArray(self._magnitude) def append(self, item): mag = item.value_in(self.units) self._magnitude.append(mag) def extend(self, items): from . import array mags = array(items).value_in(self.units) self._magnitude.append(mags) # monkeypatch pint's unit registry to return BuckyballQuantities ureg.Quantity = MdtQuantity ureg.Unit = MdtUnit # These synonyms are here solely so that we can write descriptive docstrings # TODO: use typing module to turn these into real abstract types class Scalar(MdtQuantity): """ A scalar quantity (i.e., a single floating point number) with attached units """ def __init__(self, *args): raise NotImplementedError('This is an abstract class - use MdtQuantity instead') class Vector(MdtQuantity): """ A vector quantity (i.e., a list of floats) with attached units, which behaves like a 1-dimensional numpy array """
ureg = UnitRegistry() # Because of unpickling: class Quantity(_Quantity): _REGISTRY = ureg force_ndarray = False class Unit(_Unit): _REGISTRY = ureg class Measurement(_Measurement, Quantity): _REGISTRY = ureg force_ndarray = False ureg.Quantity = Quantity ureg.Unit = Unit ureg.Measurement = Measurement ureg.define("classical_electron_radius = e^2/(4*pi*m_e*epsilon_0*c^2) = r_e") ureg.define("percent = 1e-2*count = %") ureg.define(u"permille = 1e-3*count = \u2030") ureg.define("ppm = 1e-6*count") ureg.define("ppb = 1e-9*count") ureg.define("particles_per_mol = avogadro_number/mol")
def append(self, item): from .tools import array try: mag = item.value_in(self.units) except AttributeError: # handles lists of quantities mag = array(item).value_in(self.units) self._magnitude.append(mag) def extend(self, items): from . import array mags = array(items).value_in(self.units) self._magnitude.append(mags) # monkeypatch pint's unit registry to return BuckyballQuantities ureg.Quantity = MdtQuantity ureg.Unit = MdtUnit # These synonyms are here solely so that we can write descriptive docstrings # TODO: use typing module to turn these into real abstract types, with dimensional parameterization class Scalar(MdtQuantity): """ A scalar quantity (i.e., a single floating point number) with attached units """ def __init__(self, *args): raise NotImplementedError('This is an abstract class - use MdtQuantity instead') class Vector(MdtQuantity): """ A vector quantity (i.e., a list of floats) with attached units that behaves like a 1-dimensional numpy array with units """
def test_unit_format_babel(): ureg = UnitRegistry(fmt_locale="fr_FR") volume = ureg.Unit("ml") assert volume.format_babel() == "millilitre"