Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged

Conversation

mdickinson
Copy link
Member

@mdickinsonmdickinson commentedOct 25, 2023
edited
Loading

PR#100161 added fancy float-style formatting for theFraction type, 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:

>>>format(Fraction(3,2),'20')Traceback (mostrecentcalllast):File"<stdin>",line1,in<module>File"/opt/local/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/fractions.py",line427,in__format__raiseValueError(ValueError:Invalidformatspecifier'20'forobjectof type'Fraction'

This PR adds that basic formatting support, aiming for compatibility withint formatting. The basic formatting is active either with presentation typed, or with no explicit presentation type. For example, on this branch:

>>>x=Fraction(103993,33102)>>>f"{x:.<20}"# minimum width, fill and alignment'103993/33102........'>>>f"{x:_}"# thousands separators'103_993/33_102'>>>f"{x:+}"# sign specification'+103993/33102'>>>f"{x:+d}"# explicit 'd' presentation type'+103993/33102'>>>y=Fraction(22)>>>f"{y}"'22'>>>f"{y:#}"# alternate flag '#' forces an explicit denominator'22/1'

All of the above exceptf"{y}" currently giveValueError on main.

Some details:

  • Minimum width, fill character, alignment, the sign flag, and thousands separators are supported in the obvious way.
  • The zero-fill flag0 isnot supported (though it's still fine to use0 as 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.
  • The alternate flag# 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/

@mdickinsonmdickinson changed the titlegh-67790: Support basic formatting inFraction.__format__gh-67790: Support basic formatting for FractionOct 25, 2023
@mdickinson
Copy link
MemberAuthor

@serhiy-storchaka@ericvsmith Would either of you be interested in reviewing?

@serhiy-storchaka
Copy link
Member

serhiy-storchaka commentedOct 25, 2023
edited by AlexWaygood
Loading

float.__format__ does not supportd. On other hand,%d in printf-like formatting accept float, but truncate it. Would not it cause confusion? Users may (wrongly) suppose that thed format specifier always format a number as a decimal integer.

Copy link
Member

@serhiy-storchakaserhiy-storchaka left a comment
edited
Loading

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 as1 1/2.
  • always output a fraction slash: 3/1 as3/1. (already possible with the# modifier)
  • specify different symbol for fraction slash, e.g.:,÷ (U+00F7), (U+2044), (U+2215), (U+2236).

@mdickinson
Copy link
MemberAuthor

mdickinson commentedOct 25, 2023
edited
Loading

@serhiy-storchaka Thank you for reviewing!

float.__format__ does not supportd. On other hand,%d in printf-like formatting accept float, but truncate it. Would not it cause confusion?

Hmm, possibly. Now that I look at it, we're adding two ways to do exactly the same thing (that is,format(f, s) andformat(f, s + 'd') do exactly the same thing wheres is a format specifier without a presentation type). That seems unnecessary.

What do you think about simply dropping thed support for now? So for exampleformat(f, '>.10') would still work butformat(f, '>.10d') wouldn't. Then we can still add support for ad presentation type in the future if that looks useful.

it may be useful to provide options for [...]

Yes, I think I'd rather leave these to a separate PR.

@serhiy-storchaka
Copy link
Member

What do you think about simply dropping thed support for now?

Looks fine to me.

Copy link
Member

@ericvsmithericvsmith left a comment

Choose a reason for hiding this comment

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

LGTM

@ericvsmith
Copy link
Member

What do you think about simply dropping thed support for now? So for exampleformat(f, '>.10') would still work butformat(f, '>.10d') wouldn't. Then we can still add support for ad presentation type in the future if that looks useful.

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
Copy link
Member

@mdickinson It LGTM if drop 'd' and "integer-like".

@mdickinson
Copy link
MemberAuthor

Thanks@serhiy-storchaka and@ericvsmith for feedback.

@ericvsmith

Actually, I think I'd require the "d", just in case we want to do somethings else entirely without it.

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.

Copy link
Member

@ericvsmithericvsmith left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks, Mark!

@mdickinsonmdickinson merged commitfe479fb intopython:mainDec 16, 2023
@mdickinsonmdickinson deleted the fraction-format-d-presentation-type branchDecember 16, 2023 10:58
scoder added a commit to scoder/quicktions that referenced this pull requestJan 10, 2024
bmwiedemann pushed a commit to bmwiedemann/openSUSE that referenced this pull requestJan 21, 2024
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().
bmwiedemann pushed a commit to bmwiedemann/openSUSE that referenced this pull requestFeb 7, 2024
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().
bmwiedemann pushed a commit to bmwiedemann/openSUSE that referenced this pull requestFeb 7, 2024
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().
aisk pushed a commit to aisk/cpython that referenced this pull requestFeb 11, 2024
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>
Glyphack pushed a commit to Glyphack/cpython that referenced this pull requestSep 2, 2024
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>
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@ericvsmithericvsmithericvsmith approved these changes

@serhiy-storchakaserhiy-storchakaserhiy-storchaka approved these changes

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@mdickinson@serhiy-storchaka@ericvsmith

[8]ページ先頭

©2009-2025 Movatter.jp