GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-67790: Support basic formatting for Fraction by mdickinson · Pull Request #111320 · python/cpython

Conversation

@mdickinson

@mdickinson mdickinson commented Oct 25, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

PR #100161 added fancy float-style formatting for the Fraction 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 (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/fractions.py", line 427, in __format__
    raise ValueError(
ValueError: Invalid format specifier '20' for object of type 'Fraction'

This PR adds that basic formatting support, aiming for compatibility with int formatting. The basic formatting is active either with presentation type d, 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 except f"{y}" currently give ValueError on main.

Some details:

  • Minimum width, fill character, alignment, the sign flag, and thousands separators are supported in the obvious way.
  • The zero-fill flag 0 is not supported (though it's still fine to use 0 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.

  • Issue: Implement __format__ for Fraction #67790

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
mdickinson added 3 commits October 25, 2023 11:48
@mdickinson
pythongh-67790: Add integer-style formatting for Fraction type
130c717
@mdickinson
Add docs
13a60d7
@mdickinson
Add news entry
751211a
@bedevere-app bedevere-app Bot mentioned this pull request Oct 25, 2023
Implement __format__ for Fraction #67790
Closed
@bedevere-app bedevere-app Bot added the awaiting core review label Oct 25, 2023
@mdickinson
Add PR number
51aa87e
@mdickinson mdickinson changed the title gh-67790: Support basic formatting in Fraction.__format__ gh-67790: Support basic formatting for Fraction Oct 25, 2023
@mdickinson
Restore original line spacing in docs
5c7ac04
@mdickinson mdickinson mentioned this pull request Oct 25, 2023
gh-67790: Add integer-style formatting for fractions.Fraction mdickinson/cpython#2
Closed
3 tasks done
@mdickinson

mdickinson commented Oct 25, 2023

Copy link
Copy Markdown
Member Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

mdickinson added 2 commits October 25, 2023 15:16
@mdickinson
Remove 'float-style', since it's now too specific
3e7b6b6
@mdickinson
Tighten doc wording
acfa57c
@serhiy-storchaka

serhiy-storchaka commented Oct 25, 2023 •
edited by AlexWaygood
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

serhiy-storchaka
serhiy-storchaka reviewed Oct 25, 2023
View reviewed changes

@serhiy-storchaka serhiy-storchaka left a comment •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
serhiy-storchaka
serhiy-storchaka approved these changes Oct 25, 2023
View reviewed changes
Comment thread Lib/fractions.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/whatsnew/3.13.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Oct 25, 2023
@mdickinson

mdickinson commented Oct 25, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

@serhiy-storchaka Thank you for reviewing!

float.__format__ does not support d. 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) and format(f, s + 'd') do exactly the same thing where s is a format specifier without a presentation type). That seems unnecessary.

What do you think about simply dropping the d support for now? So for example format(f, '>.10') would still work but format(f, '>.10d') wouldn't. Then we can still add support for a d 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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka

serhiy-storchaka commented Oct 25, 2023

Copy link
Copy Markdown
Member

What do you think about simply dropping the d support for now?

Looks fine to me.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ericvsmith
ericvsmith approved these changes Oct 25, 2023
View reviewed changes

@ericvsmith ericvsmith left a comment

Copy link
Copy Markdown
Member

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ericvsmith

ericvsmith commented Oct 25, 2023

Copy link
Copy Markdown
Member

What do you think about simply dropping the d support for now? So for example format(f, '>.10') would still work but format(f, '>.10d') wouldn't. Then we can still add support for a d 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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@serhiy-storchaka
Merge branch 'main' into fraction-format-d-presentation-type
43eff82
@serhiy-storchaka

serhiy-storchaka commented Dec 1, 2023

Copy link
Copy Markdown
Member

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mdickinson

mdickinson commented Dec 3, 2023

Copy link
Copy Markdown
Member Author

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. I think 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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

mdickinson added 3 commits December 3, 2023 09:55
@mdickinson
Remove support for 'd' presentation type; don't refer to integer-style
8228182
@mdickinson
Fix single backtick
2fc7d56
@mdickinson
Fix grammar - 'or' is better than 'and' here
0df75d8
serhiy-storchaka
serhiy-storchaka approved these changes Dec 3, 2023
View reviewed changes
Comment thread Doc/library/fractions.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

mdickinson added 2 commits December 3, 2023 10:15
@mdickinson
Tweak wording
9cdc408
@mdickinson
Update invalid format tests
30260f9
@mdickinson
Fix a long line in the .rst source
e736e8e
ericvsmith
ericvsmith approved these changes Dec 6, 2023
View reviewed changes

@ericvsmith ericvsmith left a comment

Copy link
Copy Markdown
Member

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!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@serhiy-storchaka
Merge branch 'main' into fraction-format-d-presentation-type
6ed788a
@mdickinson
mdickinson merged commit fe479fb into python:main Dec 16, 2023
@bedevere-app bedevere-app Bot removed the awaiting merge label Dec 16, 2023
@mdickinson
mdickinson deleted the fraction-format-d-presentation-type branch December 16, 2023 10:58
scoder added a commit to scoder/quicktions that referenced this pull request Jan 10, 2024
@scoder
Implement more general string formatting for Fraction, following CPyt…
10c9793
…hon 3.13.

See python/cpython#111320
bmwiedemann pushed a commit to bmwiedemann/openSUSE that referenced this pull request Jan 21, 2024
dirkmueller @bmwiedemann
Update python-quicktions to version 1.16 / rev 10 via SR 1140281
fa750b5
https://build.opensuse.org/request/show/1140281
by user dirkmueller + anag+factory
- update to 1.16:
  * Formatting support was improved, following CPython 3.13a3 as
    of python/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 request Feb 7, 2024
dirkmueller @bmwiedemann
Update python-quicktions to version 1.16 / rev 10 via SR 1140281
e8d2fb1
https://build.opensuse.org/request/show/1140281
by user dirkmueller + anag+factory
- update to 1.16:
  * Formatting support was improved, following CPython 3.13a3 as
    of python/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 request Feb 7, 2024
dirkmueller @bmwiedemann
Update python-quicktions to version 1.16 / rev 10 via SR 1140281
ce9d548
https://build.opensuse.org/request/show/1140281
by user dirkmueller + anag+factory
- update to 1.16:
  * Formatting support was improved, following CPython 3.13a3 as
    of python/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 request Feb 11, 2024
@mdickinson @serhiy-storchaka @aisk
pythongh-67790: Support basic formatting for Fraction (python#111320)
d385fe6
PR python#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 <[email protected]>
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
@mdickinson @serhiy-storchaka @Glyphack
pythongh-67790: Support basic formatting for Fraction (python#111320)
f8d56b9
PR python#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 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ericvsmith ericvsmith ericvsmith approved these changes
@serhiy-storchaka serhiy-storchaka serhiy-storchaka approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@mdickinson @serhiy-storchaka @ericvsmith
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.