Return Real number x to exact (numerator, denominator) pair. >>> _exact_ratio(0.25) (1, 4) x is expected to be an int, Fraction, Decimal or float.
(x)
| 1595 | |
| 1596 | |
| 1597 | def _exact_ratio(x): |
| 1598 | """Return Real number x to exact (numerator, denominator) pair. |
| 1599 | |
| 1600 | >>> _exact_ratio(0.25) |
| 1601 | (1, 4) |
| 1602 | |
| 1603 | x is expected to be an int, Fraction, Decimal or float. |
| 1604 | |
| 1605 | """ |
| 1606 | try: |
| 1607 | return x.as_integer_ratio() |
| 1608 | except AttributeError: |
| 1609 | pass |
| 1610 | except (OverflowError, ValueError): |
| 1611 | # float NAN or INF. |
| 1612 | assert not _isfinite(x) |
| 1613 | return (x, None) |
| 1614 | |
| 1615 | try: |
| 1616 | # x may be an Integral ABC. |
| 1617 | return (x.numerator, x.denominator) |
| 1618 | except AttributeError: |
| 1619 | msg = f"can't convert type '{type(x).__name__}' to numerator/denominator" |
| 1620 | raise TypeError(msg) |
| 1621 | |
| 1622 | |
| 1623 | def _convert(value, T): |
searching dependent graphs…