How to use barril - 10 common examples

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_fraction_scalar.py View on Github external
assert not another.IsValid()
    assert another.GetValue("m") == FractionValue(3000)
    assert another.GetUnit() == "m"
    assert another.GetFormatted() == "3000 [m]"

    # By default the validation will be performed, and in this cases will raise ValueError.
    another_2 = scalar.CreateCopy(value=FractionValue(5000))
    assert not another_2.IsValid()

    another_3 = units.FractionScalar("another-length", unit="m", value=FractionValue(5000))
    assert not another_3.IsValid()

    # Performing copy between invalid fraction scalars. The validation is not performed on copy.
    copied = another.Copy()
    assert not copied.IsValid()
    assert copied.GetValue("m") == FractionValue(3000)
    assert copied.GetUnit() == "m"
    assert copied.GetFormatted() == "3000 [m]"
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 / 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_derived_quantities.py View on Github external
def testDerivedQuantities(unit_database_len_time):
    # define a simple quantity
    ObtainQuantity(unit="s", category="Time")  # see if it works
    ObtainQuantity(unit="m", category="Table size")  # see if it works
    q3 = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 2]), ("Time", ["s", -1])]))
    q4 = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 2]), ("Time", ["s", -2])]))
    q5 = Quantity.CreateDerived(
        OrderedDict([("Table size", ["m", 1]), ("City size", ["m", 1]), ("Time", ["s", -2])])
    )
    q6 = Quantity.CreateDerived(OrderedDict([("Time", ["s", -2])]))
    q7 = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 1]), ("Time", ["s", 2])]))

    with pytest.raises(InvalidUnitError):
        Quantity.CreateDerived(
            OrderedDict(
                [("Table size", ["invalid", 1]), ("City size", ["m", 1]), ("Time", ["s", -2])]
            )
        )

    assert "(Table size) ** 2 / Time" == q3.GetCategory()
    assert "m2/s" == q3.GetUnit()
github ESSS / barril / src / barril / units / _tests / test_derived_quantities.py View on Github external
def testDerivedQuantities(unit_database_len_time):
    # define a simple quantity
    ObtainQuantity(unit="s", category="Time")  # see if it works
    ObtainQuantity(unit="m", category="Table size")  # see if it works
    q3 = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 2]), ("Time", ["s", -1])]))
    q4 = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 2]), ("Time", ["s", -2])]))
    q5 = Quantity.CreateDerived(
        OrderedDict([("Table size", ["m", 1]), ("City size", ["m", 1]), ("Time", ["s", -2])])
    )
    q6 = Quantity.CreateDerived(OrderedDict([("Time", ["s", -2])]))
    q7 = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 1]), ("Time", ["s", 2])]))

    with pytest.raises(InvalidUnitError):
        Quantity.CreateDerived(
            OrderedDict(
                [("Table size", ["invalid", 1]), ("City size", ["m", 1]), ("Time", ["s", -2])]
            )
        )

    assert "(Table size) ** 2 / Time" == q3.GetCategory()
    assert "m2/s" == q3.GetUnit()
github ESSS / barril / src / barril / units / _tests / test_derived_quantities.py View on Github external
def testDerivedQuantities(unit_database_len_time):
    # define a simple quantity
    ObtainQuantity(unit="s", category="Time")  # see if it works
    ObtainQuantity(unit="m", category="Table size")  # see if it works
    q3 = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 2]), ("Time", ["s", -1])]))
    q4 = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 2]), ("Time", ["s", -2])]))
    q5 = Quantity.CreateDerived(
        OrderedDict([("Table size", ["m", 1]), ("City size", ["m", 1]), ("Time", ["s", -2])])
    )
    q6 = Quantity.CreateDerived(OrderedDict([("Time", ["s", -2])]))
    q7 = Quantity.CreateDerived(OrderedDict([("Table size", ["m", 1]), ("Time", ["s", 2])]))

    with pytest.raises(InvalidUnitError):
        Quantity.CreateDerived(
            OrderedDict(
                [("Table size", ["invalid", 1]), ("City size", ["m", 1]), ("Time", ["s", -2])]
            )
        )

    assert "(Table size) ** 2 / Time" == q3.GetCategory()
    assert "m2/s" == q3.GetUnit()

    assert "(Table size) ** 2 / (Time) ** 2" == q4.GetCategory()
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 / 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