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

Add header alignment attribute #183

Merged
merged 4 commits into from May 16, 2022

Conversation

myheroyuki
Copy link
Contributor

@myheroyuki myheroyuki commented May 16, 2022

Fixes #102

@codecov
Copy link

codecov bot commented May 16, 2022

Codecov Report

Merging #183 (1ab3eaa) into master (546a594) will increase coverage by 0.08%.
The diff coverage is 100.00%.

❗ Current head 1ab3eaa differs from pull request most recent head bb931ba. Consider uploading reports for the commit bb931ba to get more accurate results

@@            Coverage Diff             @@
##           master     #183      +/-   ##
==========================================
+ Coverage   94.03%   94.11%   +0.08%     
==========================================
  Files           5        5              
  Lines        2245     2295      +50     
==========================================
+ Hits         2111     2160      +49     
- Misses        134      135       +1     
Flag Coverage Δ
macos-latest 94.11% <100.00%> (+0.08%) ⬆️
ubuntu-latest 94.11% <100.00%> (+0.08%) ⬆️
windows-latest 93.98% <100.00%> (+0.08%) ⬆️

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

Impacted Files Coverage Δ
src/prettytable/prettytable.py 90.39% <100.00%> (+0.20%) ⬆️
tests/test_prettytable.py 99.87% <100.00%> (-0.13%) ⬇️

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 8ce5bd8...bb931ba. Read the comment docs.

@myheroyuki myheroyuki added the changelog: Added For new features label May 16, 2022
README.md Outdated Show resolved Hide resolved
@hugovk
Copy link
Member

hugovk commented May 16, 2022

Thank you!

@hugovk hugovk merged commit 01e8eb2 into jazzband:master May 16, 2022
@hugovk
Copy link
Member

hugovk commented Aug 30, 2022

I only just released this in 3.4.0 and noticed a possible bug:

With this code:

from prettytable import PrettyTable

headers = [
    "cycle",
    "release",
    "latest",
    "latest release",
    "support",
    "eol",
    "codename",
    "link",
]
data = [
    {
        "cycle": "22.04 LTS",
        "codename": "Jammy Jellyfish",
        "support": "2027-04-02",
        "eol": "2032-04-01",
        "latest": "22.04.1",
        "link": "https://wiki.ubuntu.com/JammyJellyfish/ReleaseNotes/",
        "release": "2022-04-21",
        "latest release": "2022-08-11",
    },
    {
        "cycle": "21.10",
        "codename": "Impish Indri",
        "support": "2022-07-31",
        "eol": "2022-07-31",
        "latest": "21.10",
        "link": "https://wiki.ubuntu.com/ImpishIndri/ReleaseNotes/",
        "release": "2021-10-14",
        "latest release": "2021-10-14",
    },
    {
        "cycle": "21.04",
        "codename": "Hirsute Hippo",
        "support": "2022-01-20",
        "eol": "2022-01-20",
        "latest": "21.04",
        "link": "https://wiki.ubuntu.com/HirsuteHippo/ReleaseNotes/",
        "release": "2021-04-22",
        "latest release": "2021-04-22",
    },
    {
        "cycle": "20.10",
        "codename": "Groovy Gorilla",
        "support": "2021-07-22",
        "eol": "2021-07-22",
        "latest": "20.10",
        "release": "2020-10-22",
        "latest release": "2020-10-22",
    },
    {
        "cycle": "20.04 LTS",
        "codename": "Focal Fossa",
        "support": "2025-04-02",
        "eol": "2030-04-01",
        "latest": "20.04.4",
        "release": "2020-04-23",
        "latest release": "2022-02-25",
    },
    {
        "cycle": "19.10",
        "codename": "Karmic Koala",
        "support": "2020-07-06",
        "eol": "2020-07-06",
        "latest": "19.10",
        "release": "2019-10-17",
        "latest release": "2019-10-17",
    },
    {
        "cycle": "18.04 LTS",
        "codename": "Bionic Beaver",
        "support": "2023-04-02",
        "eol": "2028-04-01",
        "latest": "18.04.6",
        "link": "https://wiki.ubuntu.com/BionicBeaver/ReleaseNotes",
        "release": "2018-04-26",
        "latest release": "2021-09-17",
    },
    {
        "cycle": "16.04 LTS",
        "codename": "Xenial Xerus",
        "support": "2021-04-02",
        "eol": "2026-04-01",
        "latest": "16.04.7",
        "release": "2016-04-21",
        "latest release": "2020-08-13",
    },
    {
        "cycle": "14.04 LTS",
        "codename": "Trusty Tahr",
        "support": "2019-04-02",
        "eol": "2024-04-01",
        "latest": "14.04.6",
        "release": "2014-04-17",
        "latest release": "2019-03-07",
    },
]

x = PrettyTable()

for header in headers:
    col_data = [row[header] if header in row else "" for row in data]
    x.add_column(header, col_data)
    if header in ("cycle", "latest", "link"):
        x.align[header] = "l"

print(x)

Run with 3.3.0:

$ pip install --quiet prettytable==3.3.0 && python 1.py
+-----------+------------+---------+----------------+------------+------------+-----------------+------------------------------------------------------+
| cycle     |  release   | latest  | latest release |  support   |    eol     |     codename    | link                                                 |
+-----------+------------+---------+----------------+------------+------------+-----------------+------------------------------------------------------+
| 22.04 LTS | 2022-04-21 | 22.04.1 |   2022-08-11   | 2027-04-02 | 2032-04-01 | Jammy Jellyfish | https://wiki.ubuntu.com/JammyJellyfish/ReleaseNotes/ |
| 21.10     | 2021-10-14 | 21.10   |   2021-10-14   | 2022-07-31 | 2022-07-31 |   Impish Indri  | https://wiki.ubuntu.com/ImpishIndri/ReleaseNotes/    |
| 21.04     | 2021-04-22 | 21.04   |   2021-04-22   | 2022-01-20 | 2022-01-20 |  Hirsute Hippo  | https://wiki.ubuntu.com/HirsuteHippo/ReleaseNotes/   |
| 20.10     | 2020-10-22 | 20.10   |   2020-10-22   | 2021-07-22 | 2021-07-22 |  Groovy Gorilla |                                                      |
| 20.04 LTS | 2020-04-23 | 20.04.4 |   2022-02-25   | 2025-04-02 | 2030-04-01 |   Focal Fossa   |                                                      |
| 19.10     | 2019-10-17 | 19.10   |   2019-10-17   | 2020-07-06 | 2020-07-06 |   Karmic Koala  |                                                      |
| 18.04 LTS | 2018-04-26 | 18.04.6 |   2021-09-17   | 2023-04-02 | 2028-04-01 |  Bionic Beaver  | https://wiki.ubuntu.com/BionicBeaver/ReleaseNotes    |
| 16.04 LTS | 2016-04-21 | 16.04.7 |   2020-08-13   | 2021-04-02 | 2026-04-01 |   Xenial Xerus  |                                                      |
| 14.04 LTS | 2014-04-17 | 14.04.6 |   2019-03-07   | 2019-04-02 | 2024-04-01 |   Trusty Tahr   |                                                      |
+-----------+------------+---------+----------------+------------+------------+-----------------+------------------------------------------------------+

And with 3.4.0:

pip install --quiet prettytable==3.4.0 && python 1.py
+-----------+------------+---------+----------------+------------+------------+-----------------+------------------------------------------------------+
|   cycle   |  release   |  latest | latest release |  support   |    eol     |     codename    |                         link                         |
+-----------+------------+---------+----------------+------------+------------+-----------------+------------------------------------------------------+
| 22.04 LTS | 2022-04-21 | 22.04.1 |   2022-08-11   | 2027-04-02 | 2032-04-01 | Jammy Jellyfish | https://wiki.ubuntu.com/JammyJellyfish/ReleaseNotes/ |
| 21.10     | 2021-10-14 | 21.10   |   2021-10-14   | 2022-07-31 | 2022-07-31 |   Impish Indri  | https://wiki.ubuntu.com/ImpishIndri/ReleaseNotes/    |
| 21.04     | 2021-04-22 | 21.04   |   2021-04-22   | 2022-01-20 | 2022-01-20 |  Hirsute Hippo  | https://wiki.ubuntu.com/HirsuteHippo/ReleaseNotes/   |
| 20.10     | 2020-10-22 | 20.10   |   2020-10-22   | 2021-07-22 | 2021-07-22 |  Groovy Gorilla |                                                      |
| 20.04 LTS | 2020-04-23 | 20.04.4 |   2022-02-25   | 2025-04-02 | 2030-04-01 |   Focal Fossa   |                                                      |
| 19.10     | 2019-10-17 | 19.10   |   2019-10-17   | 2020-07-06 | 2020-07-06 |   Karmic Koala  |                                                      |
| 18.04 LTS | 2018-04-26 | 18.04.6 |   2021-09-17   | 2023-04-02 | 2028-04-01 |  Bionic Beaver  | https://wiki.ubuntu.com/BionicBeaver/ReleaseNotes    |
| 16.04 LTS | 2016-04-21 | 16.04.7 |   2020-08-13   | 2021-04-02 | 2026-04-01 |   Xenial Xerus  |                                                      |
| 14.04 LTS | 2014-04-17 | 14.04.6 |   2019-03-07   | 2019-04-02 | 2024-04-01 |   Trusty Tahr   |                                                      |
+-----------+------------+---------+----------------+------------+------------+-----------------+------------------------------------------------------+

Notice the different header alignment:

< | cycle     |  release   | latest  | latest release |  support   |    eol     |     codename    | link                                                 |
---
> |   cycle   |  release   |  latest | latest release |  support   |    eol     |     codename    |                         link                         |

So the code had set:

    if header in ("cycle", "latest", "link"):
        x.align[header] = "l"

In 3.3.0, the data and header were left-aligned.

In 3.4.0, only the data was left-aligned, the header was centre-aligned.

Is this a bug?

Should x.header_align override x.align?

But if there's no x.header_align, should the x.align behaviour not change from 3.3.0?

@osfrickler
Copy link

We noticed this change in OpenStack testing, too, and I think that default behavior should not change in a minor release, so I'd prefer this to be treated as a bug and get fixed.

@SeanMooney
Copy link

this is the openstack nova bug https://bugs.launchpad.net/nova/+bug/1988482

currently, it's blocking our stable branch for the last release.
we are working around it by pinning for now but ideally, this would be addressed
so that the default behaviour did not change outside of a major release.

currently I'm not sure how else we could "fix" this in oepnstack as we cannot raise minium on already released branches and see cant simply update the test as that would then fail with the older release of prettytable.

@SeanMooney
Copy link

actully i think i understand how to work around this in OpenStack so i have proposed
https://review.opendev.org/c/openstack/nova/+/855658 to manuly hardcode left alignment for now.

we can backport that as a workaround.

this is still likely a bug but we can maintain the old behavior by just setting both align and header_align

@hugovk
Copy link
Member

hugovk commented Sep 2, 2022

I'll revert this and make a bug fix release.

Then this PR can be re-submitted, and we'll need extra tests to make sure things work with both header_align and align, with just one, and with neither.

Also the default header_align="c" in add_column needs checking:

def add_column(self, fieldname, column, align="c", valign="t", header_align="c"):

@hugovk
Copy link
Member

hugovk commented Sep 2, 2022

3.4.1 released:

https://pypi.org/project/prettytable/3.4.1/

@osfrickler
Copy link

Thx for the fast update, 3.4.1 works without failure for us

openstack-mirroring pushed a commit to openstack/requirements that referenced this pull request Sep 3, 2022
That release contains a formatting change that causes nova tests to
fail[0]. The change has been reverted [1], so using 3.4.1 instead works
fine.

[0] https://bugs.launchpad.net/nova/+bug/1988482
[1] jazzband/prettytable#183

Change-Id: I979320cef3ed8c8bee75061b4fcb4c1323cc83a9
openstack-mirroring pushed a commit to openstack/openstack that referenced this pull request Sep 3, 2022
* Update requirements from branch 'master'
  to efffcd29d4d59a953e99963c402cbe956018d4ec
  - Merge "Block prettytable 3.4.0"
  - Block prettytable 3.4.0
    
    That release contains a formatting change that causes nova tests to
    fail[0]. The change has been reverted [1], so using 3.4.1 instead works
    fine.
    
    [0] https://bugs.launchpad.net/nova/+bug/1988482
    [1] jazzband/prettytable#183
    
    Change-Id: I979320cef3ed8c8bee75061b4fcb4c1323cc83a9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog: Added For new features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add header_align argument
4 participants