How to use the barril.units.unit_database.UnitDatabase function in barril

To help you get started, we’ve selected a few barril examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ESSS / barril / src / barril / units / _tests / test_quantity.py View on Github external
def testQuantityInit(mocker):

    # 1: cache it
    ObtainQuantity("m", "length")

    # 2: check
    calls = [0]
    original = UnitDatabase.GetSingleton

    def _New(*args, **kwargs):
        calls[0] += 1
        return original()

    mocker.patch.object(UnitDatabase, "GetSingleton")
    UnitDatabase.GetSingleton.return_value = _New()

    ObtainQuantity("m", "length")
    assert calls[0] == 1
github ESSS / barril / src / barril / units / _tests / test_scalar_and_quantity.py View on Github external
def GetUnitDatabase(self):
        return UnitDatabase.GetSingleton()
github ESSS / barril / src / barril / units / _tests / test_scalar_refactor.py View on Github external
def _ObtainQuantity(unit, category=None):
    if not category:
        unit_database = UnitDatabase.GetSingleton()
        category = unit_database.GetDefaultCategory(unit)
        if not category:
            raise InvalidUnitError(unit)

    key = (category, unit)
    quantity = _quantities_cache.get(key)
    if quantity is None:
        quantity = _quantities_cache.setdefault(key, _SimpleQuantity(category, unit))
    return quantity
github ESSS / barril / src / barril / units / _tests / test_posc2.py View on Github external
def testMegagramPerCubicMeterToKilogramPerCubicMeter():
    unit_database = UnitDatabase.CreateDefaultSingleton()
    expected = 1000
    obtained = unit_database.Convert("density", "Mg/m3", "kg/m3", 1)
    assert obtained == expected
github ESSS / barril / src / barril / units / _tests / test_posc2.py View on Github external
def testDivisionError():
    unit_database = UnitDatabase.CreateDefaultSingleton()
    for category, category_info in unit_database.categories_to_quantity_types.items():
        base_unit = category_info.default_unit
        for unit in unit_database.GetValidUnits(category):
            for i in [-1, 0, 1]:
                try:
                    unit_database.Convert(category, base_unit, unit, i)
                except Exception as e:
                    raise TypeError(f"Error converting: from: {base_unit} to: {unit}") from e

                try:
                    unit_database.Convert(category, unit, base_unit, i)
                except Exception as e:
                    raise TypeError(f"Error converting: from: {unit} to: {base_unit}") from e
github ESSS / barril / src / barril / units / _tests / test_unit_database.py View on Github external
def testDiscoverCloseUnitMatches():
    unit_database = UnitDatabase.CreateDefaultSingleton()
    assert unit_database.FindSimilarUnitMatches("kg/day") == ["kg/d"]
    assert unit_database.FindSimilarUnitMatches("bbls/d") == ["bbl/d", "bbl/d2"]
    assert unit_database.FindSimilarUnitMatches("mg/l") == ["mg/L"]
github ESSS / barril / src / barril / units / _quantity.py View on Github external
"""
    :type unit: str or OrderedDict(str -> list(str, int))
    :param unit:
        Either the string representing the unit or an ordered dict with the composing unit
        information (if composing all the info, including the category will be received in this
        parameter).

    :param str category:
        The category for the quantity. If not given it's gotten based on the unit passed.

    :param str unknown_unit_caption:
        The caption for the unit (used if unknown).

    :rtype Quantity:
    """
    unit_database = UnitDatabase.GetSingleton()
    quantities_cache = unit_database.quantities_cache

    if unit.__class__ in (list, tuple):
        # It may be a derived unit with list(tuple(str, int)) -- in which case the category
        # must also be a list (of the same size)
        if len(unit) == 1 and unit[0][1] == 1:
            # Although passed as composing, it's a simple case
            unit = unit[0][0]
            if category.__class__ in (list, tuple):
                category = category[0]
        else:
            assert category.__class__ in (list, tuple)
            unit = OrderedDict((cat, unit_and_exp) for (cat, unit_and_exp) in zip(category, unit))
            category = None

    if hasattr(unit, "items"):
github ESSS / barril / src / barril / units / _abstractvaluewithquantity.py View on Github external
def __init__(self, category, value=None, unit=None):

        unit_database = UnitDatabase.GetSingleton()
        if category.__class__ == _Quantity:
            quantity = category

            assert unit is None, "If quantity is given, the unit must not!"

            if value is None:
                value = self._GetDefaultValue(quantity.GetCategoryInfo())

        else:
            if category.__class__ != str:
                # Support for creating a scalar as:
                # Scalar(10, 'm')
                # Scalar(10, 'm', 'length')
                value, unit, category = category, value, unit

            if value is None or unit is None:
github ESSS / barril / src / barril / fixtures.py View on Github external
def unit_database():
    """
    Fixture to be used whenever a test needs a clean UnitDatabase. When using this fixture, it's
    safe to call UnitDatabase.GetSingleton().
    """
    from barril.units.unit_database import UnitDatabase

    yield UnitDatabase.PushSingleton()
    UnitDatabase.PopSingleton()