jazzband / prettytable

Display tabular data in a visually appealing ASCII table format
https://pypi.org/project/PrettyTable/
Other
1.35k stars 154 forks source link

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

Open mwtoews opened 1 year ago

mwtoews commented 1 year ago

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.

codecov[bot] commented 1 year ago

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%) :arrow_up:
ubuntu-latest 94.57% <100.00%> (+0.18%) :arrow_up:
windows-latest 94.52% <100.00%> (+0.27%) :arrow_up:

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%) :arrow_up:
tests/test_prettytable.py 100.00% <100.00%> (ø)
src/prettytable/colortable.py 100.00% <0.00%> (ø)

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

mwtoews commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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

mwtoews commented 1 year ago

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).