8000 Improve conversions for fractions · Issue #88281 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

Improve conversions for fractions #88281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tecki mannequin opened this issue May 12, 2021 · 5 comments
Closed

Improve conversions for fractions #88281

tecki mannequin opened this issue May 12, 2021 · 5 comments
Labels
3.11 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@tecki
Copy link
Mannequin
tecki mannequin commented May 12, 2021
BPO 44115
Nosy @rhettinger, @mdickinson, @serhiy-storchaka, @tecki, @vedgar
PRs
  • bpo-44115: improve duck-typing of fractions #26064
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2021-05-12.13:09:37.632>
    labels = ['type-feature', 'library', '3.11']
    title = 'Improve conversions for fractions'
    updated_at = <Date 2021-05-15.04:21:37.665>
    user = 'https://github.com/tecki'

    bugs.python.org fields:

    activity = <Date 2021-05-15.04:21:37.665>
    actor = 'rhettinger'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2021-05-12.13:09:37.632>
    creator = 'Martin.Teichmann'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 44115
    keywords = ['patch']
    message_count = 4.0
    messages = ['393507', '393554', '393559', '393560']
    nosy_count = 5.0
    nosy_names = ['rhettinger', 'mark.dickinson', 'serhiy.storchaka', 'Martin.Teichmann', 'veky']
    pr_nums = ['26064']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue44115'
    versions = ['Python 3.11']

    @tecki
    Copy link
    Mannequin Author
    tecki mannequin commented May 12, 2021

    Currently, fraction.Fractions can be generated from floats via their as_integer_ratio method. This had been extended to also work with the Decimals class. It would be more generic - and IMHO more Pythonic - to just allow any data type, as long as it has an as_integer_ratio method.

    As an example, this allows numpy floats to be converted into fractions.

    @tecki tecki mannequin added 3.11 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels May 12, 2021
    @vedgar
    Copy link
    Mannequin
    vedgar mannequin commented May 13, 2021

    Absolutely. I think that's a big part of the reason that as_integer_ratio is there.

    @rhettinger
    Copy link
    Contributor

    Note for posterity: I tried out pattern matching here but it was a little tricky and it slowed down the code a bit. At at least it worked.

            if denominator is None:
                match numerator:
                    case int(x) if type(numerator) is int:
                        self._numerator = numerator
                        self._denominator = 1
                        return self
                    case numbers.Rational(numerator=numerator, denominator=denominator):
                        self._numerator = numerator
                        self._denominator = denominator
                        return self
                    case object(as_integer_ratio=_):
                        self._numerator, self._denominator = numerator.as_integer_ratio()
                        return self
                    case str(x):
                        m = _RATIONAL_FORMAT.match(numerator)
                        if m is None:
                            raise ValueError('Invalid literal for Fraction: %r' %
                                             numerator)
                        numerator = int(m.group('num') or '0')
                        denom = m.group('denom')
                        if denom:
                            denominator = int(denom)
                        else:
                            denominator = 1
                            decimal = m.group('decimal')
                            if decimal:
                                scale = 10**len(decimal)
                                numerator = numerator * scale + int(decimal)
                                denominator *= scale
                            exp = m.group('exp')
                            if exp:
                                exp = int(exp)
                                if exp >= 0:
                                    numerator *= 10**exp
                                else:
                                    denominator *= 10**-exp
                        if m.group('sign') == '-':
                            numerator = -numerator
                    case _:
                        raise TypeError("argument should be a string "
                                        "or a Rational instance")

    @serhiy-storchaka
    Copy link
    Member

    It was proposed before in bpo-37884.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @encukou
    Copy link
    Member
    encukou commented Mar 18, 2024

    Accepting any object with as_integer_ratio was rejected in #82017.
    If SageMath's use case is common enough, we might want to add a new method like __integer_ratio__ on exact numeric types, similar to __index__ for exact integer ones. But that would call for wider discussion on Discourse.

    @encukou encukou closed this as not planned Won't fix, can't repro, duplicate, stale Mar 18, 2024
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.11 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants
    0