Skip to content
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

Refactor old %-formatting to use format() builtin #223

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

mwtoews
Copy link

@mwtoews mwtoews commented Dec 6, 2022

The old %-formatting is not ageing well in modern Python code, and has largely been superseded by format strings driven by the format() built-in. This PR pivots away from the % operator when used for string operations.

One notable change is to the behaviour of int_format and float_format, which have been changed to optionally allowed to end with alpha char, e.g. #08x or 8.3G. Previously, only float_format optionally ended with f.

- With int_format and float_format, allow these to optionally
  end with alpha char, e.g. 'x' or 'g'
- Previously, only float_format optionally ended with 'f'
@hugovk hugovk added the changelog: Changed For changes in existing functionality label Dec 6, 2022
@codecov
Copy link

codecov bot commented Dec 6, 2022

Codecov Report

Merging #223 (b0a2362) into master (32e788f) will increase coverage by 0.22%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #223      +/-   ##
==========================================
+ Coverage   94.38%   94.61%   +0.22%     
==========================================
  Files           5        5              
  Lines        2281     2303      +22     
==========================================
+ Hits         2153     2179      +26     
+ Misses        128      124       -4     
Flag Coverage Δ
macos-latest 94.57% <100.00%> (+0.18%) ⬆️
ubuntu-latest 94.57% <100.00%> (+0.18%) ⬆️
windows-latest 94.52% <100.00%> (+0.27%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/prettytable/prettytable.py 91.05% <100.00%> (+0.35%) ⬆️
tests/test_prettytable.py 100.00% <100.00%> (ø)
src/prettytable/colortable.py 100.00% <0.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link
Member

@hugovk hugovk left a comment

Choose a reason for hiding this comment

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

Thanks for this! Please could you add/adjust tests to cover the new alpha char endings?

src/prettytable/prettytable.py Show resolved Hide resolved
@mwtoews
Copy link
Author

mwtoews commented Dec 7, 2022

I've added a few different tests for a few different format codes. Not sure if something else was expected, so please note and I can pivot the last commit.

@mwtoews
Copy link
Author

mwtoews commented Dec 10, 2022

Additional tests for invalid formats are added.

Note that using format() is a bit more strict than the old % formatters, and will require all values of the column to be int or float types. E.g., consider:

"%3d" % 24.6  # ' 24'
format(24.6, "3d")  # ValueError: Unknown format code 'd' for object of type 'float'

this could have some implications for end-users that have mixed types in a column with either format code.

@hugovk
Copy link
Member

hugovk commented Dec 12, 2022

That could be a problem, we don't really want to introduce breaking changes here.

@mwtoews
Copy link
Author

mwtoews commented Dec 12, 2022

I'd hate to break things. One solution is to catch an exception, show a warning about mixing datatypes and fallback with %-formatting. Is there an example table that would have mixed types? Also, how is missing data encoded?

Update: I've been trying to make a breaking example, but I can't seem to create one. A field can have mixed types, and a definition for each int_format and float_format, and everything seems to work as expected. And I've found missing cells are None (as expected, formatted by none_format). If a breaking example is found, I have a pending commit to fall-back to int_format.__mod__(value) / float_format.__mod__(value).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: Changed For changes in existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants