How to use the barril.units.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_unit_database.py View on Github external
def testFindCase(unit_database_custom_conversion):
    unit_database = units.UnitDatabase()
    # add some units for testing
    unit_database.AddUnitBase("temperature", "mang1", "mA")
    unit_database.AddUnit("temperature", "mang2", "Ma", frombase="%f", tobase="%f")
    unit_database.AddUnit("temperature", "Celsius", "C", frombase="%f", tobase="%f")
    unit_database.AddUnit("temperature", "mang3", "MA", frombase="%f", tobase="%f")

    unit_database.AddCategory("my", "temperature")
    with pytest.raises(AssertionError):
        unit_database.FindUnitCase("my", "ma")
    assert "C" == unit_database.FindUnitCase("my", "c")
    assert "C" == unit_database.FindUnitCase("my", "C")
github ESSS / barril / src / barril / units / _tests / test_fraction_scalar.py View on Github external
def testFractionScalarConversion():
    db = units.UnitDatabase()
    db.AddUnit("length", "milimeters", "mm", "%f * 1000.0", "%f / 1000.0")
    db.AddUnitBase("length", "meters", "m")

    f = units.FractionScalar("length", value=FractionValue(3, (1, 2)), unit="m")

    converted = db.Convert("length", "m", "mm", f.value)
    assert converted == FractionValue(3500)
github ESSS / barril / src / barril / fixtures.py View on Github external
def CreateUnitDatabaseCustomConversion():
    from barril.units import UNKNOWN_QUANTITY_TYPE, UNKNOWN_UNIT

    unit_database = units.UnitDatabase()
    # add some units for testing
    unit_database.AddUnit(UNKNOWN_QUANTITY_TYPE, UNKNOWN_UNIT, UNKNOWN_UNIT, "%f", "%f")
    unit_database.AddUnit("length", "milimeters", "mm", "%f * 1000.0", "%f / 1000.0")
    unit_database.AddUnitBase("length", "meters", "m")
    unit_database.AddUnit("length", "centimeters", "cm", "%f * 100.0", "%f / 100.0")
    unit_database.AddUnit("length", "kilometers", "km", "%f / 1000.0", "%f * 1000.0")
    unit_database.AddUnit("length", "miles", "mi", "%f / 1609.347", "%f * 1609.347")
    unit_database.AddUnit("length", "inches", "in", "%f / 0.0254", "%f * 0.0254")
    unit_database.AddUnit(
        "length", "micrometers", "µm", "%f * 1000000.0", "%f / 1000000.0"
    )
    unit_database.AddUnitBase("temperature", "Celcius", "ºC")
    unit_database.AddCategory("length", "length", valid_units=["m", "mm", "cm", "km"])

    def C_to_F(x):
        return x * 1.8 + 32
github ESSS / barril / src / barril / fixtures.py View on Github external
def CreateUnitDatabaseStartUnits():
    """
    :rtype: UnitDatabase
    :returns:
        Returns a unit database with length and time quantity types
    """
    unit_database = units.UnitDatabase()

    unit_database.AddUnitBase("length", "meters", "m")
    unit_database.AddUnit("length", "kilometers", "km", "%f / 1000.0", "%f * 1000.0")
    unit_database.AddUnit("length", "centimeters", "cm", "%f * 100.0", "%f / 100.0")
    unit_database.AddCategory("length", "length")

    return unit_database
github ESSS / barril / src / barril / fixtures.py View on Github external
def CreateUnitDatabasePoscLen(fill_categories=True):
    """
    :rtype: UnitDatabase
    :returns:
        Returns a unit database with "length" quantity type only

    :param bool fill_categories:
        If a category "length" should also be created
    """
    unit_database = units.UnitDatabase()
    unit_database.AddUnitBase("length", "meters", "m")
    unit_database.AddUnit("length", "kilometers", "km", "%f / 1000.0", "%f * 1000.0")

    if fill_categories:
        unit_database.AddCategory(category="length", quantity_type="length")
    return unit_database
github ESSS / barril / docs / _extension / list_all_units.py View on Github external
def run(self):
        rst = ViewList()
        unit_database = UnitDatabase.GetSingleton()
        source = "fakefile.rst"

        # Create the rst content
        for category in sorted(unit_database.IterCategories(), key=str.casefold):
            title = unit_database.GetCategoryInfo(category).caption.title()
            rst.append(f".. rubric:: {title}", source)
            rst.append(f"", source)

            for unit in sorted(
                unit_database.GetCategoryInfo(category).valid_units_set, key=str.casefold
            ):
                name = unit_database.unit_to_unit_info[unit].name
                rst.append(f'- ``"{unit}"`` ({name})', source)

            rst.append(f" ", source)
github ESSS / barril / src / barril / fixtures.py View on Github external
def unit_database_posc():
    """
    Fixture to be used whenever a test needs a clean UnitDatabase. When using this fixture, it's
    safe to call UnitDatabase.GetSingleton().
    """
    unit_database = units.UnitDatabase()
    unit_database.FillUnitDatabaseWithPosc(unit_database)
    units.UnitDatabase.PushSingleton(unit_database)

    yield unit_database

    units.UnitDatabase.PopSingleton()