How to use the barril.basic.fraction.FractionValue 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_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_fraction_scalar.py View on Github external
def testComparison():

    f1 = units.FractionScalar("length", value=FractionValue(10), unit="in")
    f3 = units.FractionScalar("volume", value=FractionValue(4), unit="m3")
    with pytest.raises(TypeError):
        f1 < f3

    f2 = units.FractionScalar("length", unit="in")
    f1 = f1.CreateCopy(value=FractionValue(250, (1, 2)), unit="m")
    f2 = f2.CreateCopy(value=FractionValue(220, (3, 4)), unit="m")

    assert f1 > f2
    assert f2 < f1
github ESSS / barril / src / barril / basic / fraction / _tests / test_fraction_value.py View on Github external
def testCopy():
    f = FractionValue(3, (5, 3))
    cf = copy.copy(f)
    assert f == cf
    assert f is not cf

    cf.fraction.numerator = 10
    cf.fraction.denominator = 4
    assert f == FractionValue(3, (5, 3))
github ESSS / barril / src / barril / units / _tests / test_fraction_scalar.py View on Github external
def testComparison():

    f1 = units.FractionScalar("length", value=FractionValue(10), unit="in")
    f3 = units.FractionScalar("volume", value=FractionValue(4), unit="m3")
    with pytest.raises(TypeError):
        f1 < f3

    f2 = units.FractionScalar("length", unit="in")
    f1 = f1.CreateCopy(value=FractionValue(250, (1, 2)), unit="m")
    f2 = f2.CreateCopy(value=FractionValue(220, (3, 4)), unit="m")

    assert f1 > f2
    assert f2 < f1
github ESSS / barril / src / barril / basic / fraction / _tests / test_fraction_value.py View on Github external
def testPartsArentNone():
    """
    FractionValue can't be initialized nor modified to have None as number or fraction part.
    """
    with pytest.raises(TypeError):
        FractionValue(1, None)
    with pytest.raises(TypeError):
        FractionValue(None, (0 / 1))
    with pytest.raises(TypeError):
        FractionValue(None, None)

    f = FractionValue(1, Fraction(0, 1))
    with pytest.raises(TypeError):
        f.SetNumber(None)
    with pytest.raises(TypeError):
        f.SetFraction(None)
github ESSS / barril / src / barril / basic / fraction / _tests / test_fraction_value.py View on Github external
def AssertCreateFromString(text, whole, fraction=None):
        assert (
            FractionValue.CreateFromString(text) == FractionValue(whole, fraction)
            if fraction is not None
            else FractionValue(whole)
        )
github ESSS / barril / src / barril / basic / fraction / _tests / test_fraction_value.py View on Github external
def testPartsArentNone():
    """
    FractionValue can't be initialized nor modified to have None as number or fraction part.
    """
    with pytest.raises(TypeError):
        FractionValue(1, None)
    with pytest.raises(TypeError):
        FractionValue(None, (0 / 1))
    with pytest.raises(TypeError):
        FractionValue(None, None)

    f = FractionValue(1, Fraction(0, 1))
    with pytest.raises(TypeError):
        f.SetNumber(None)
    with pytest.raises(TypeError):
        f.SetFraction(None)
github ESSS / barril / src / barril / units / _fraction_scalar.py View on Github external
"""
        # check if a quantity type str was passed
        if quantity.__class__ == str:
            # Note: actually ignoring the initial quantity type in this case because we
            # do the operation just using the from unit which may have any category (i.e.
            # the important thing is the quantity type, so, it can be created with the
            # default category).
            quantity = ObtainQuantity(from_unit)

        convert_to_quantity = ObtainQuantity(from_unit, quantity.GetComposingCategories())
        converted_number = convert_to_quantity.ConvertScalarValue(
            fraction_value.GetNumber(), to_unit
        )

        # convert the number
        result = FractionValue(number=converted_number)
        # convert fraction's numerator
        if fraction_value.GetFraction() is not None:
            converted_numerator = convert_to_quantity.ConvertScalarValue(
                fraction_value.GetFraction().numerator, to_unit
            )

            converted_fraction = copy.copy(fraction_value.GetFraction())
            converted_fraction.numerator = converted_numerator
            result.SetFraction(converted_fraction)
        return result
github ESSS / barril / src / barril / units / _fraction_scalar.py View on Github external
"""
        For internal use only. Is used to initialize the actual quantity.

        :type quantity: str or IQuantity
        :param quantity:
            The quantity of this scalar.

        :param FractionValue value:
            The initial value
        """
        # Considering fraction values values are easily coerced from float values (though it is
        # important to note the opposite is not true) if the input value is not a fraction already
        # try to convert value to float. This also makes this subclass SetValue interface compatible
        # with superclass interface.
        try:
            if type(value) != FractionValue:
                value = FractionValue(number=float(value))
        except Exception:
            # If not a fraction and coercion to float fails, use CheckType to provide a better error
            # message.
            CheckType(value, (FractionValue, float))

        self._value = value
        self._quantity = quantity
        self._unit_database = unit_database or UnitDatabase.GetSingleton()