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

Alignment alteration #207

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

Conversation

myheroyuki
Copy link
Contributor

Here's a possible solution to a batch of issues with column alignment (#202, #199 , #102, #111). The root of these issues seemed to be the fact that the align attribute was filling two roles. It was a global alignment, set as a string, and also a per-column alignment, set as a dictionary. I fixed this by creating a new Alignment class that is basically a dictionary that contains a variable that stores the global alignment value.

The main reason I decided to break it out into it's own single class was that having it as a class allowed me to validate the attributes when the user sets it per column using square brackets. It still functions the same way for the user when they set it, update it, print it, or iterate over it, but it changes the return type to be an instance of Alignment rather than dict.

I'd really like to get some comments on this one with some ideas on how I can improve it. I don't love the solution I came up with here, but other things I thought of either would change things from the user's perspective, not address all the issues, or make the code more confusing.

@myheroyuki myheroyuki added the changelog: Added For new features label Oct 18, 2022
@codecov
Copy link

codecov bot commented Oct 18, 2022

Codecov Report

Merging #207 (96d6a4e) into main (ed5d66c) will increase coverage by 0.61%.
The diff coverage is 97.71%.

@@            Coverage Diff             @@
##             main     #207      +/-   ##
==========================================
+ Coverage   94.38%   95.00%   +0.61%     
==========================================
  Files           5        5              
  Lines        2281     2421     +140     
==========================================
+ Hits         2153     2300     +147     
+ Misses        128      121       -7     
Flag Coverage Δ
macos-latest 94.96% <97.71%> (+0.70%) ⬆️
ubuntu-latest 94.96% <97.71%> (+0.61%) ⬆️
windows-latest 94.91% <97.71%> (+0.61%) ⬆️

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

Impacted Files Coverage Δ
src/prettytable/prettytable.py 91.60% <96.03%> (+0.91%) ⬆️
tests/test_colortable.py 100.00% <100.00%> (ø)
tests/test_prettytable.py 100.00% <100.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 the PR! Here's some review suggestions.

##############################


def validate_align(val):
Copy link
Member

Choose a reason for hiding this comment

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

Should these three validators be private/internal and not public?

And let's add type hints.

Suggested change
def validate_align(val):
def _validate_align(val: str) -> None:


def validate_align(val):
try:
assert val in ["l", "c", "r"]
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
assert val in ["l", "c", "r"]
assert val in ("l", "c", "r")

raise ValueError(f"Alignment {val} is invalid, use l, c or r")


def validate_valign(val):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def validate_valign(val):
def _validate_valign(val: str | None) -> None:


def validate_valign(val):
try:
assert val in ["t", "m", "b", None]
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
assert val in ["t", "m", "b", None]
assert val in ("t", "m", "b", None)

raise ValueError(f"Alignment {val} is invalid, use t, m, b or None")


def validate_header_align(val):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
def validate_header_align(val):
def _validate_header_align(val: str | None) -> None:

# A dict extension to manage field alignment. Used to:
# Have global alingment sepreate from field alignments.
# Validate values when setting for individual fields
class Alignment(dict):
Copy link
Member

Choose a reason for hiding this comment

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

Also private/internal?

Suggested change
class Alignment(dict):
class _Alignment(dict):

Comment on lines 130 to 131
dictrepr = dict.__repr__(self)
return dictrepr
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
dictrepr = dict.__repr__(self)
return dictrepr
return dict.__repr__(self)

Please also add a test case for this method.

elif field in self._align:
align = self._align[field]
else:
align = self._align.alignment
Copy link
Member

Choose a reason for hiding this comment

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

Please can you add a test case to cover this line?

for field in t.valign:
assert t.valign[field] == "t"
for field in t.header_align:
assert t.header_align[field] == None
Copy link
Member

Choose a reason for hiding this comment

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

Lint fix:

Suggested change
assert t.header_align[field] == None
assert t.header_align[field] is None

t = helper_table()
t.field_names = ["L", "C", "R"]

assert t.header_align["L"] == None
Copy link
Member

Choose a reason for hiding this comment

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

Lint fix:

Suggested change
assert t.header_align["L"] == None
assert t.header_align["L"] is None

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.

None yet

2 participants