How to use the barril.units.ObtainQuantity 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_read_only_quantity.py View on Github external
def testReadOnlyQuantity(unit_database_empty):
    unit_database = unit_database_empty

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

    read_only_quantity = ObtainQuantity("m", "length")
    with pytest.raises(AttributeError):
        read_only_quantity.SetUnit("cm")

    # When creating a copy of a read only quantity we'll make it not read only anymore!
    copy = read_only_quantity.MakeCopy(Quantity)
    assert copy.GetUnitDatabase() is unit_database
github ESSS / barril / src / barril / units / _tests / test_fixedarray.py View on Github external
def testDefaultValues(unit_database_start_units):
    # Not raises exception because by default validation is False on the copy operation
    array = units.FixedArray(3, "length")
    assert array.values == [0.0, 0.0, 0.0]

    array = units.FixedArray(3, ObtainQuantity("m"))
    assert array.values == [0.0, 0.0, 0.0]

    with pytest.raises(AssertionError):
        units.FixedArray(3, ObtainQuantity("m"), unit="m")
github ESSS / barril / src / barril / units / _tests / test_array.py View on Github external
def testCreateWithQuantity():
    q = ObtainQuantity("m", "length")
    a1 = units.Array(q, values=[(1000, 1000), (300, 300)])
    a2 = a1.CreateCopy(unit="km")

    assert a1.values == [(1000, 1000), (300, 300)]
    assert a2.values == [(1, 1), (0.3, 0.3)]
github ESSS / barril / src / barril / units / _tests / test_quantity.py View on Github external
def testHash():
    quantity1 = ObtainQuantity("m")
    quantity2 = ObtainQuantity("m", "length")
    assert hash(quantity1) == hash(quantity2)
    assert quantity1 == quantity2

    unknown1 = ObtainQuantity(UNKNOWN_UNIT, unknown_unit_caption="foo")
    unknown2 = ObtainQuantity(UNKNOWN_UNIT, unknown_unit_caption="foo_bar")
    unknown3 = ObtainQuantity(UNKNOWN_UNIT, unknown_unit_caption="foo")
    assert hash(unknown1) == hash(unknown3)
    assert unknown1 == unknown3

    assert unknown1 != unknown2
github ESSS / barril / src / barril / units / _tests / test_unit_database.py View on Github external
def testDefaultUnitWhenNoneIsPassed(unit_database_custom_conversion):
    unit_database = unit_database_custom_conversion
    unit_database.AddCategory("my category", "length", ["mm"], default_unit="mm")
    # Quantity without specifying unit, must use category's default unit and not raise
    default_when_none = ObtainQuantity(None, "my category")
    assert ObtainQuantity("mm", "my category") == default_when_none
github ESSS / barril / src / barril / curve / _tests / test_curve.py View on Github external
def testCurves(unit_database):
    import numpy

    r = ObtainQuantity("m", "length")

    values10 = Array(r, values=numpy.array(list(range(10)), dtype=numpy.int32))
    values9 = Array(r, values=numpy.array(list(range(9)), dtype=numpy.int32))

    domain9 = Array("time", values=numpy.array(list(range(9)), dtype=numpy.int32), unit="s")
    domain10 = Array("time", values=numpy.array(list(range(10)), dtype=numpy.int32), unit="s")

    with pytest.raises(ValueError):
        Curve(values10, domain9)

    c = Curve(values10, domain10)

    with pytest.raises(ValueError):
        c.SetDomain(domain9)
    with pytest.raises(ValueError):
        c.SetImage(values9)
github ESSS / barril / src / barril / units / _tests / test_posc.py View on Github external
def testPower(unit_database_posc):
    unit_database = unit_database_posc
    assert "volt ampere" == ObtainQuantity("VA").GetQuantityType()
    assert "volt ampere reactive" == ObtainQuantity("VAr").GetQuantityType()

    assert unit_database.Convert("force", "N", "kN", 56.0) == 0.056
    assert (
        approx(
            abs(
                unit_database.Convert("force", [("N", 2)], [("kN", 2)], -(56.0 * 56))
                - -(0.056 * 0.056)
            ),
            7,
        )
        == 0
    )
github ESSS / barril / src / barril / units / _tests / test_scalar.py View on Github external
when default_value is not defined
    """
    db = unit_database_len_pressure

    db.AddCategory(category="my length", quantity_type="length", min_value=100.0, max_value=200.0)

    # if the default value is not defined, the scalar should not try to set the initial/first
    # value of the new instance to 0.0, but it should assume the minimum allowed value.
    # should not raise ValueError
    length = Scalar("my length")
    assert length.GetValue() == 100.0

    length = Scalar(ObtainQuantity("m", "my length"))
    assert length.GetValue() == 100.0

    length = Scalar(ObtainQuantity("m"))
    assert length.GetValue() == 0.0

    # invalid default value (< min)
    with pytest.raises(AssertionError):
        db.AddCategory(
            category="my pressure",
            quantity_type="pressure",
            default_value=50.0,
            min_value=100.0,
            max_value=200.0,
        )

    # invalid default value (> max)
    with pytest.raises(AssertionError):
        db.AddCategory(
            category="my pressure",