How to use the barril.units.Scalar 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_scalar.py View on Github external
db.AddUnitBase("length", "meters", "m")
    db.AddUnit("length", "centimeters", "cm", "x * 100.0", "x / 100.0")

    db.AddCategory(
        "my length",
        "length",
        ["cm", "m"],
        default_unit="m",
        default_value=18.5,
        min_value=-30.0,
        max_value=100.0,
        is_min_exclusive=True,
        is_max_exclusive=False,
    )

    s1 = Scalar("my length")
    assert s1.value == 18.5
    assert s1.unit == "m"

    # test maximum boundary
    s2 = Scalar("my length", 100.0, "m")
    assert s2.value == 100.0
    assert s2.unit == "m"

    s3 = Scalar("my length", 500.0, "cm")
    assert s3.value == 500.0
    assert s3.unit == "cm"

    # higher than maximum value
    another = Scalar("my length", 120.0, "m")
    assert not another.IsValid()
    with pytest.raises(ValueError):
github ESSS / barril / src / barril / units / _tests / test_posc_additional_units.py View on Github external
def testHertzPerSecond():
    from barril.units import Scalar

    assert approx(Scalar(1, "rpm").GetValue("Hz")) == 1 / 60.0
    assert approx(Scalar(1, "Hz").GetValue("rpm")) == 60.0
    assert approx(Scalar(1, "Hz/s").GetValue("rpm/s")) == 60.0
    assert approx(Scalar(1, "rpm/s").GetValue("Hz/s")) == 1 / 60.0
github ESSS / barril / src / barril / units / _tests / test_posc.py View on Github external
def testPoscAngularAcceleration(unit_database_posc):
    default = units.Scalar(1, "rad/s2")
    assert default.GetQuantityType() == "angular acceleration"

    assert approx(abs(default.value - 1.0), 7) == 0
    assert approx(abs(default.GetValue("dega/s2") - 57.29578778556937), 7) == 0
    assert approx(abs(default.GetValue("dega/min2") - (1.0 * 3600.0) * 57.29578778556937), 7) == 0

    assert approx(abs(default.GetValue("rev/s2") - 0.15915494309644432), 7) == 0
    assert approx(abs(default.GetValue("rev/min2") - 0.15915494309644432 * 3600.0), 7) == 0
    assert approx(abs(default.GetValue("rpm/s") - 9.549296585786658), 7) == 0
github ESSS / barril / src / barril / units / _tests / test_scalar.py View on Github external
def testScalarHashEq():
    scalar1 = Scalar("length", 10, "m")
    scalar2 = Scalar("length", 10, "m")
    scalar3 = Scalar("length", 10, "cm")
    scalar4 = Scalar("length", scalar2.GetValue("cm"), "cm")

    assert scalar1 == scalar2
    assert hash(scalar1) == hash(scalar2)
    assert hash(scalar1) == hash(scalar1)
    assert scalar2 != scalar3
    assert hash(scalar2) != hash(scalar3)
    assert scalar3 != scalar4
    assert hash(scalar4) != hash(scalar3)
github ESSS / barril / src / barril / units / _tests / test_legacy_unit.py View on Github external
def testCreateScalarUnitsError():
    from barril.units.unit_database import UnitsError

    with raises(UnitsError, match="Unable to get default category for: foo/d"):
        _ = Scalar(1.0, "foo/d")
github ESSS / barril / src / barril / units / _tests / test_posc.py View on Github external
def testPoscTemperature(unit_database_posc):
    u1 = units.Scalar("thermodynamic temperature", 100, "degC")
    u2 = units.Scalar("thermodynamic temperature", 100, "degF")
    u3 = units.Scalar("thermodynamic temperature", 0, "degC")
    u4 = units.Scalar("thermodynamic temperature", 0, "degC")
    u5 = units.Scalar("thermodynamic temperature", 235, "degF")
    u6 = units.Scalar("thermodynamic temperature", 64, "degC")
    assert u1.GetQuantityType() == u2.GetQuantityType()
    assert "temperature" == u2.GetQuantityType()
    assert u1.unit != u2.unit
    assert u3 == u4
    # u1.unit = 'K'  # from C to K
    assert approx(abs(u1.GetValue("K") - 373.15), 7) == 0
    # u2.unit = 'K'  # from F to K
    assert approx(abs(u2.GetValue("K") - 310.927777777), 7) == 0
    # u3.unit = 'degF'  # from C to F
    assert u3.GetValue("degF") == 32.0
    # C to F, F to C
    assert approx(abs(u5.GetValue("degC") - 112.7777777777), 7) == 0
    assert approx(abs(u6.GetValue("degF") - 147.2), 7) == 0
    # now return u3.unit from F to C and compare u3.value with u4.value
    # sanity-check
    assert u3.GetValue("degC") == u4.value
github ESSS / barril / src / barril / units / _tests / test_scalar.py View on Github external
def testScalarPickle(unit_database_posc):

    import pickle

    simple_scalar = Scalar("length", 10, "m")
    simple_scalar2 = pickle.loads(pickle.dumps(simple_scalar))
    assert simple_scalar == simple_scalar2

    complex_scalar = Scalar("length", 10, "m") * Scalar("time", 5, "s")
    complex_scalar2 = pickle.loads(pickle.dumps(complex_scalar))
    assert complex_scalar == complex_scalar2
github ESSS / barril / src / barril / units / _tests / test_posc2.py View on Github external
def testPoscWithoutFillCategories(unit_database_posc_len_no_category):
    unit_database = unit_database_posc_len_no_category

    unit_database.AddCategory("my_len", "length")

    # this category does not exist because we didn't fill the categories
    with pytest.raises(UnitsError):
        units.Scalar("length", 100, "km")

    u1 = units.Scalar("my_len", 100, "km")
    u2 = units.Scalar("my_len", 100000, "m")
    assert approx(abs(u1.value - u2.GetValue("km")), 7) == 0
github ESSS / barril / src / barril / units / _tests / test_scalar.py View on Github external
def testDefaultValue(unit_database_len_pressure):
    """
        Scalar constructor considers the minimum and maximum values
        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)
github ESSS / barril / src / barril / units / _tests / test_posc_additional_units.py View on Github external
def testPerMicrometre(db):
    per_micrometre = units.Scalar("per length", 1.0, "1/um")
    per_metre = per_micrometre.CreateCopy(unit="1/m")
    per_inch = per_micrometre.CreateCopy(unit="1/in")

    assert per_micrometre.GetValue("1/m") == 10 ** 6
    assert approx(per_metre.GetValue()) == 10 ** 6
    assert approx(per_inch.GetValue()) == 25400
    assert per_metre == units.Scalar("per length", 10 ** 6, "1/m")
    assert per_inch == units.Scalar("per length", 25400.0, "1/in")