Exemplo n.º 1
0
def testCopy():
    f = Fraction(5, 3)
    cf = copy.copy(f)
    assert f == cf
    assert not f != cf
    assert f is not cf

    assert f != Fraction(3, 5)
    assert not f == Fraction(3, 5)
Exemplo n.º 2
0
    def CreateFromString(cls,
                         text: str,
                         consider_locale: bool = True) -> "FractionValue":
        """
        Create a FractionValue from a string.

        :param text:
            The text with a fraction-value, that is, three integers separated by space and slash
            respectively.
            <value> <value>/<value>

        :param consider_locale:
            Consider the locale on converting string to float

        :returns:
            The fraction value

        :raises ValueError:
            If the text is not convertible to a FractionValue
        """
        text = str(text).strip()

        string_to_float_callable: Callable[[str], float]
        if consider_locale:
            string_to_float_callable = FloatFromString
        else:
            string_to_float_callable = float

        # First try to match only the fractional part.
        m = cls._FRACTION_PARTIAL_RE.match(text)
        number_match: Optional[str]
        if m is not None:
            number_match = None
        else:
            # If can't match a fraction try a mixed number
            m = cls._FRACTION_RE.match(text)
            if m is None:
                raise ValueError('Please enter a text in the form: "5 3/4"')
            number_match = m.group("float")

        if number_match is None:
            number = 0.0
        else:
            number = string_to_float_callable(number_match)

        if m.group("numerator") is not None and m.group(
                "denominator") is not None:
            numerator = string_to_float_callable(m.group("numerator"))
            denominator = locale.atoi(m.group("denominator"))
            fraction = Fraction(numerator, denominator)
        else:
            fraction = Fraction(0.0, 1.0)

        return FractionValue(number, fraction)
Exemplo n.º 3
0
def testBasicUsage():
    f = Fraction(5, 3)
    assert tuple(f) == (5, 3)
    assert len(f) == 2
    assert f[0] == 5
    assert f[1] == 3
    assert f.numerator == 5
    assert f.denominator == 3
    f.numerator = 10
    assert tuple(f) == (10, 3)
    f.denominator = 4
    assert tuple(f) == (5, 2)
Exemplo n.º 4
0
def testEquality():
    assert FractionValue(3, Fraction(5, 3)) == FractionValue(3, Fraction(5, 3))
    assert not FractionValue(3, Fraction(5, 3)) != FractionValue(3, Fraction(5, 3))

    assert FractionValue(3) == FractionValue(3)
    assert not FractionValue(3) != FractionValue(3)

    assert FractionValue(10, Fraction(5, 3)) != FractionValue(3, Fraction(5, 3))
    assert not FractionValue(10, Fraction(5, 3)) == FractionValue(3, Fraction(5, 3))

    assert FractionValue(10, (5, 3)) == FractionValue(10, Fraction(5, 3))
Exemplo n.º 5
0
def testStrFormat(mocker) -> None:
    """
    Scalar field behavior. In this test we make sure that the
    Fraction.__str__ method calls FormatFloat, which handles
    the locale properly.
    """
    # By default, the numbers are formatted using "%g"
    f = Fraction(5, 3)
    assert str(f), "5/3"

    # Test the use of FormatFloat | 5.6 is 28/5 | 5.6/3 is 28/15
    f = Fraction(5.6, 3)
    assert str(f) == "28/15"

    mocker.patch("barril.basic.format_float.FormatFloat", side_effect=lambda x, y: "X%.2fX" % y)
    assert str(f) == "X28.00X/X15.00X"
Exemplo n.º 6
0
def testStr():
    f = FractionValue(3, Fraction(5, 3))
    assert str(f) == "3 5/3"
    assert repr(f) == "FractionValue(3, 5/3)"

    f = FractionValue(3)
    assert str(f) == "3"
    assert repr(f) == "FractionValue(3, 0/1)"
Exemplo n.º 7
0
def testBasicUsage():
    f = FractionValue(3, Fraction(5, 3))
    assert f.number == 3
    assert f.fraction == Fraction(5, 3)

    f.number = 5.5
    f.fraction = Fraction(6, 5)
    assert f.number == 5.5
    assert f.fraction == Fraction(6, 5)

    with pytest.raises(TypeError):
        f.SetNumber("hello")
    with pytest.raises(TypeError):
        f.SetFraction("hello")
    with pytest.raises(ValueError):
        f.SetFraction((1, 2, 3))

    assert FractionValue(3).GetFraction() == Fraction(0, 1)
Exemplo n.º 8
0
def testStrFormat():
    """
    Scalar field behavior. In this test we make sure that the
    Fraction.__str__ method calls FormatFloat, which handles
    the locale properly.
    """
    import barril.basic.format_float

    # By default, the numbers are formatted using "%g"
    f = Fraction(5, 3)
    assert six.text_type(f), "5/3"

    # Test the use of FormatFloat | 5.6 is 28/5 | 5.6/3 is 28/15
    f = Fraction(5.6, 3)
    assert six.text_type(f) == "28/15"

    original_format_float = barril.basic.format_float.FormatFloat
    barril.basic.format_float.FormatFloat = lambda x, y: "X%.2fX" % y
    try:
        assert six.text_type(f) == "X28.00X/X15.00X"
    finally:
        barril.basic.format_float.FormatFloat = original_format_float

    assert six.text_type(f) == "28/15"
Exemplo n.º 9
0
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)
Exemplo n.º 10
0
    def SetFraction(self, fraction: Union[Fraction, Tuple[float,
                                                          float]]) -> None:
        """
        Sets the fractional part of this object.

        :param fraction:
            The fraction for this FractionValue object. A pair (numerator, denominator)
            is also accepted.
        """
        CheckType(fraction, (Fraction, tuple))

        # convert a tuple
        if isinstance(fraction, tuple):
            if len(fraction) != 2:
                raise ValueError(
                    f"Expected a tuple (numerator, denominator), got {fraction!r}"
                )
            fraction = Fraction(*fraction)

        self._fraction = fraction
Exemplo n.º 11
0
    def SetFraction(self, fraction):
        """
        Sets the fractional part of this object.

        :type fraction: Fraction or tuple(float, float)
        :param fraction:
            The fraction for this FractionValue object. A pair (numerator, denominator)
            is also accepted.
        """
        CheckType(fraction, (Fraction, tuple))

        # convert a tuple
        if isinstance(fraction, tuple):
            if len(fraction) != 2:
                raise ValueError(
                    "Expected a tuple (numerator, denominator), got %r" %
                    (fraction, ))
            fraction = Fraction(*fraction)

        self._fraction = fraction
Exemplo n.º 12
0
def testDefault():
    f = FractionValue()
    assert f.number == 0.0
    assert f.fraction == Fraction(0, 1)
Exemplo n.º 13
0
def testOperations():
    # float operator
    assert float(Fraction(5, 3)) == 5.0 / 3.0

    # sum
    assert Fraction(5, 3) + Fraction(2, 3) == Fraction(7, 3)
    assert Fraction(5, 3) + 5.0 == Fraction(20, 3)
    assert 5.0 + Fraction(5, 3) == Fraction(20, 3)

    # sub
    assert Fraction(5, 3) - Fraction(2, 3) == Fraction(3, 3)
    assert Fraction(5, 3) - 1.0 == Fraction(2, 3)
    assert 1.0 - Fraction(5, 3) == Fraction(-2, 3)

    # mul
    assert Fraction(5, 3) * Fraction(4, 3) == Fraction(20, 9)
    assert 3 * Fraction(5, 3) == Fraction(15, 3)
    assert Fraction(5, 3) * 3 == Fraction(15, 3)

    # div
    assert Fraction(5, 3) / Fraction(5, 3) == Fraction(15, 15)
    assert Fraction(5, 3) / 3 == Fraction(5, 9)
    assert 3 / Fraction(5, 3) == Fraction(9, 5)

    # inv
    assert Fraction(5, 3).inv() == Fraction(3, 5)

    # neg
    assert -Fraction(5, 3) == Fraction(-5, 3)
Exemplo n.º 14
0
def testReduce():
    f = Fraction(5, 3)
    f[0] = 15
    assert tuple(f) == (5, 1)
Exemplo n.º 15
0
def testStr():
    assert six.text_type(Fraction(5, 3)) == "5/3"
    assert repr(Fraction(5, 3)) == "Fraction(5, 3)"

    assert six.text_type(Fraction(3 / 1000, 4)) == "3/4000"
Exemplo n.º 16
0
def testLong():
    f = Fraction(20000000000000000, None)
    assert tuple(f) == (20000000000000000, 1)
Exemplo n.º 17
0
def testStr() -> None:
    assert str(Fraction(5, 3)) == "5/3"
    assert repr(Fraction(5, 3)) == "Fraction(5, 3)"

    assert str(Fraction(3 / 1000, 4)) == "3/4000"
Exemplo n.º 18
0
def testFloat():
    assert float(FractionValue(3, Fraction(5, 3))) == 3 + 5 / 3.0
    assert float(FractionValue(3)) == 3.0