How to use the barril.units.unit_database.UnitDatabase.GetSingleton 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_operations.py View on Github external
def testOperation():
    unit_database = UnitDatabase.GetSingleton()
    unit_database.CheckDefaultUnitDatabase()
    q = ObtainQuantity(unit="m", category="length")

    SCALAR_OPERATION = [operator.add, operator.sub, operator.truediv, operator.mul, operator.mod]
    SCALAR_TYPES = (int, float)

    for operation in SCALAR_OPERATION:
        for cast_type in SCALAR_TYPES:
            # Left
            q2 = operation(cast_type(2), q)
            assert q2.GetQuantityType() == "length"
            assert q2.GetUnit() == "m"

            # Right
            q2 = operation(q, cast_type(2))
            assert q2.GetQuantityType() == "length"
github ESSS / barril / src / barril / units / _tests / test_posc.py View on Github external
def testPoscPermeabilityLength(unit_database_posc):
    unit_database = UnitDatabase.GetSingleton()
    assert "volume" == unit_database.GetQuantityType("mD.ft")
    assert "permeability length" == unit_database.GetDefaultCategory("mD.ft")
github ESSS / barril / src / barril / units / _quantity.py View on Github external
:param category_to_unit_and_exps:
            This odict defines the category as well as the unit in a way that we can specify exponents.

        :param type resulting_class:
            Deprecated (no longer used).

        :param bool validate_category_and_units:
            If True, the category and units will be validated (otherwise, it'll just create it
            without trying to validate any value (note that it's meant to be used internally
            to make creating the quantity faster without having to actually validate it)

        :rtype: cls
        :returns:
            An instance that represents a quantity with the given categories, units and exponents.
        """
        unit_database = UnitDatabase.GetSingleton()

        if validate_category_and_units:
            assert hasattr(
                category_to_unit_and_exps, "items"
            ), "validate_category_and_units needs to be a dict"
            for category, (unit, _exp) in category_to_unit_and_exps.items():
                # will do the checkings needed for validation (category/unit)
                # so that we can later just store the internal information without
                # any other check.

                # changes to accomodate making operations with derived units (internally the
                # information must be a dict)
                if category.__class__ != str:
                    raise TypeError("Only str is accepted. %s is not." % category.__class__)

                # Getting the category should be enough to know that it's valid.
github ESSS / barril / src / barril / units / _array.py View on Github external
:param unit:
            A string representing the unit, if not defined
            the unit from the first Scalar on the sequence will be used.

        :param category:
            A string representing the category, if not defined
            the category from the first Scalar on the sequence will be used.
        """
        scalars = iter(scalars)
        try:
            first_scalar = next(scalars)
        except StopIteration:
            if unit is None and category is None:
                return cls.CreateEmptyArray()
            elif category is None:
                category = UnitDatabase.GetSingleton().GetDefaultCategory(unit)
                return cls(values=[], unit=unit, category=category)
            else:
                assert unit is None
                return cls(
                    values=[], unit=unit, category=category
                )  # This actually will raise an exception

        unit = unit or first_scalar.unit
        category = category or first_scalar.category
        values = [first_scalar.GetValue(unit)] + [scalar.GetValue(unit) for scalar in scalars]
        return cls(values=values, unit=unit, category=category)