jazzband / prettytable

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

Set the length of each column #126

Open zzzkorn opened 3 years ago

zzzkorn commented 3 years ago

Set the length of each column with a separator '. For example:

x = PrettyTable()
x.field_names = ["Country'50", "Capital", "is_russia", "that's mine"]
x.add_row(["Russia", "Moscow", True, True])
x.add_rows([["Argentina", "Buenos Aires", False, False], ["Jamaica", "Kingston", False, False]])
x.add_column("Starts with A'77", [False, True, False])
x.add_column("Can't do this'30", [False, True, False])

print(x)
hugovk commented 3 years ago

Thanks for the contribution, but I think this could break existing data that just happens to have an apostrophe before a number.

zzzkorn commented 3 years ago

Only the number after the ' at the end of the line is taken into account. We can also use a less common separator.

For example:

x = PrettyTable()
x.field_names = ["Country'500 '30", "Capital", "is_russia", "that's mine"]

+--------------------------------+---------+-----------+-------------+
|          Country'500           | Capital | is_russia | that's mine |
+--------------------------------+---------+-----------+-------------+
+--------------------------------+---------+-----------+-------------+
hugovk commented 3 years ago

We have no idea what characters people are using in their data.

Would you like to propose an alternative API that doesn't involve interpreting user data?

How about an API like x.set_widths([50, None, ....])?

And/or add a width parameter to add_column and/or add_rows?

How would it handle data that is longer than the requested width? Cropping the data? Or is it a minimum width?

Changes would require unit tests to cover different use cases, plus documentation in README and docstrings.

(See also https://github.com/jazzband/prettytable/issues/116.)

zzzkorn commented 3 years ago

Ok, I'll take care of the x.set_widths() method implementation and related tasks.

codecov[bot] commented 3 years ago

Codecov Report

Merging #126 (37440e0) into master (0a8f5b9) will increase coverage by 0.57%. The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #126      +/-   ##
==========================================
+ Coverage   91.07%   91.65%   +0.57%     
==========================================
  Files           3        3              
  Lines        1703     1809     +106     
==========================================
+ Hits         1551     1658     +107     
+ Misses        152      151       -1     
Flag Coverage Δ
GHA_Ubuntu 91.65% <100.00%> (+0.57%) :arrow_up:
GHA_Windows 91.65% <100.00%> (+0.57%) :arrow_up:
GHA_macOS 91.65% <100.00%> (+0.57%) :arrow_up:

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

Impacted Files Coverage Δ
src/prettytable/prettytable.py 87.63% <100.00%> (+0.34%) :arrow_up:
tests/test_prettytable.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 0a8f5b9...37440e0. Read the comment docs.

hugovk commented 2 years ago

How does this work with min_width and max_width for the whole table?

They're documented in the docstring, but not the README: https://github.com/jazzband/prettytable/pull/98/files

I'd guess these column min widths should override the table one? Some tests mixing would be useful.

zzzkorn commented 2 years ago

Ok i will test this

zzzkorn commented 2 years ago

The _compute_widths() method subtracts the column width, taking into account the minimum and maximum widths. This width is started with the columns_min_width and the maximum value is taken.