Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
gh-67790: Support basic formatting for Fraction#111320
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
gh-67790: Support basic formatting for Fraction#111320
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Fraction.__format__mdickinson commentedOct 25, 2023
@serhiy-storchaka@ericvsmith Would either of you be interested in reviewing? |
serhiy-storchaka commentedOct 25, 2023 • edited by AlexWaygood
Loading Uh oh!
There was an error while loading.Please reload this page.
edited by AlexWaygood
Uh oh!
There was an error while loading.Please reload this page.
|
serhiy-storchaka left a comment• edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
It is perhaps out of the scope of this PR or this issue, but it may be useful to provide options for:
- output as improper fraction: 3/2 as
1 1/2. always output a fraction slash: 3/1 as(already possible with the3/1.#modifier)- specify different symbol for fraction slash, e.g.
:,÷(U+00F7),⁄(U+2044),∕(U+2215),∶(U+2236).
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
mdickinson commentedOct 25, 2023 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
@serhiy-storchaka Thank you for reviewing!
Hmm, possibly. Now that I look at it, we're adding two ways to do exactly the same thing (that is, What do you think about simply dropping the
Yes, I think I'd rather leave these to a separate PR. |
serhiy-storchaka commentedOct 25, 2023
Looks fine to me. |
ericvsmith left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
LGTM
ericvsmith commentedOct 25, 2023
Actually, I think I'd require the "d", just in case we want to do somethings else entirely without it. But I don't feel strongly about it. OTOH, note that for ints, the 'd' has no effect. |
serhiy-storchaka commentedDec 1, 2023
@mdickinson It LGTM if drop 'd' and "integer-like". |
mdickinson commentedDec 3, 2023
Thanks@serhiy-storchaka and@ericvsmith for feedback.
Yes, I can see a case both ways, and part of me wants to require a presentation type for all non-trivial formatting. Ithink this is close enough to the obvious one way to do formatting for Fractions that I'm happy for it to be the "default" formatting, so not require a "d". I'll make that change. |
Uh oh!
There was an error while loading.Please reload this page.
ericvsmith left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
LGTM. Thanks, Mark!
https://build.opensuse.org/request/show/1140281by user dirkmueller + anag+factory- update to 1.16: * Formatting support was improved, following CPython 3.13a3 as ofpython/cpython#111320 * Add support for Python 3.13 by using Cython 3.0.8 and calling math.gcd().
https://build.opensuse.org/request/show/1140281by user dirkmueller + anag+factory- update to 1.16: * Formatting support was improved, following CPython 3.13a3 as ofpython/cpython#111320 * Add support for Python 3.13 by using Cython 3.0.8 and calling math.gcd().
https://build.opensuse.org/request/show/1140281by user dirkmueller + anag+factory- update to 1.16: * Formatting support was improved, following CPython 3.13a3 as ofpython/cpython#111320 * Add support for Python 3.13 by using Cython 3.0.8 and calling math.gcd().
PRpython#100161 added fancy float-style formatting for the Fraction type,but left us in a state where basic formatting for fractions (alignment,fill, minimum width, thousands separators) still wasn't supported.This PR adds that support.---------Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
PRpython#100161 added fancy float-style formatting for the Fraction type,but left us in a state where basic formatting for fractions (alignment,fill, minimum width, thousands separators) still wasn't supported.This PR adds that support.---------Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Uh oh!
There was an error while loading.Please reload this page.
PR#100161 added fancy float-style formatting for the
Fractiontype, but left us in a state where basic formatting for fractions (alignment, fill, thousands separators) still wasn't supported. For example, we can't currently specify a minimum width for a formatted fraction:This PR adds that basic formatting support, aiming for compatibility with
intformatting. The basic formatting is active either with presentation typed, or with no explicit presentation type. For example, on this branch:All of the above except
f"{y}"currently giveValueErroron main.Some details:
0isnot supported (though it's still fine to use0as a fill character in the normal way): it's not 100% clear what the semantics should be (should both the numerator and denominator be padded? Just the numerator?) or what value it would provide, so until someone comes up with valid use-cases that can help inform the semantics, I'm leaving this unsupported.#forces a slash and denominator in the formatted output, even when the output is integer-valued. By default, the slash and denominator are left off for integer-valued fractions.Pinging@ericvsmith for awareness.
📚 Documentation preview 📚:https://cpython-previews--111320.org.readthedocs.build/