Skip to content

gh-121797: Add class method Fraction.from_number() #121800

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

Merged
merged 5 commits into from
Oct 14, 2024

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Jul 15, 2024

It is an alternative constructor which only accepts a single numeric argument. Unlike to Fraction.from_float() and Fraction.from_decimal() it accepts any real numbers supported by the standard constructor (int, float, Decimal, Rational numbers).
Unlike to the standard constructor, it does not accept strings.


📚 Documentation preview 📚: https://cpython-previews--121800.org.readthedocs.build/

It is an alternative constructor which only accepts a single numeric argument.
Unlike to Fraction.from_float() and Fraction.from_decimal() it accepts any
real numbers supported by the standard constructor (int, float, Decimal,
Rational numbers).
Unlike to the standard constructor, it does not accept strings.
Copy link
Member

@mdickinson mdickinson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 for the general idea. I haven't reviewed line-by-line.

Copy link
Contributor

@skirpichev skirpichev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with one nitpick (feel free to ignore)

@@ -305,6 +306,28 @@ def __new__(cls, numerator=0, denominator=None):
self._denominator = denominator
return self

@classmethod
def from_number(cls, number):
Copy link
Contributor

@skirpichev skirpichev Oct 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, you can reuse this in the RationalFraction constructor, isn't? First three if/elif's share exactly same logic.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean the main Fraction constructor?

There is a different exception if the argument type is not supported. There is also a matter of performance.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant some helper:

@classmethod
def _from_number(cls, number):
    if type(number) is int:
        return cls._from_coprime_ints(number, 1)
    elif isinstance(number, numbers.Rational):
        return cls._from_coprime_ints(number.numerator, number.denominator)
    elif (isinstance(number, float) or
          (not isinstance(number, type) and
           hasattr(number, 'as_integer_ratio'))):
        return cls._from_coprime_ints(*number.as_integer_ratio())

Then you can reuse one in the constructor and in the from_number(), e.g.:

        if denominator is None:
            self = cls._from_number(numerator)
            if self is not None:
                return self

            elif isinstance(numerator, str):            
                ...

There is also a matter of performance.

I don't expect too much from extra class method call.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra function call and extra check.

This will not save much code. I'll leave this on future if we touch this code again.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it's up to you. PR has Mark approval, so it's ready to go.

@serhiy-storchaka serhiy-storchaka enabled auto-merge (squash) October 14, 2024 07:32
@serhiy-storchaka serhiy-storchaka merged commit b52c730 into python:main Oct 14, 2024
34 checks passed
@serhiy-storchaka serhiy-storchaka deleted the fraction-from-number branch October 21, 2024 13:34
scoder added a commit to scoder/quicktions that referenced this pull request Nov 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy