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

Align Issue with add_autoindex #199

Open
myheroyuki opened this issue Sep 21, 2022 · 1 comment
Open

Align Issue with add_autoindex #199

myheroyuki opened this issue Sep 21, 2022 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@myheroyuki
Copy link
Contributor

When trying to fix issues with header_align I noticed an existing issue with how the alignment is set in add_autoindex. The alignment for the index column is set based on the existing values of self.align and self.valign. This would be fine if those values were strings, but they can be dicts as well. In this case the align[<index_fieldname>] value gets set to a dict, creating an issue with embedded dictionaries.

What did you do?

I created a table with align values and then added an auto_index column.

What did you expect to happen?

I expected the index column to be properly aligned.

What actually happened?

The index column was not properly aligned when calling get_string. When calling get_html_string the program crashed with an TypeError: unhashable type: 'dict' error.

Please include code that reproduces the issue.

import prettytable
from prettytable import PrettyTable


def helper_table(rows=3):
    t = PrettyTable(["Field 1", "Field 2", "Field 3"])
    v = 1
    for row in range(rows):
        # Some have spaces, some not, to help test padding columns of different widths
        t.add_row([f"value {v*100}", f"value {v+1*100}", f"value {v+2*100}"])
        v += 3
    return t



t = helper_table(rows=3)
t.format = True
t.align = "r"
t.align["Field 1"] = "l"
t.valign["Field 1"] = "T"
t.add_autoindex("I")

print(t.get_string())
print(t.get_html_string())

The output of running this is shown below.

+------+-----------+-----------+-----------+
|  I   | Field 1   |   Field 2 |   Field 3 |
+------+-----------+-----------+-----------+
| 1000 | value 100 | value 101 | value 201 |
| 1001 | value 400 | value 104 | value 204 |
| 1002 | value 700 | value 107 | value 207 |
+------+-----------+-----------+-----------+
Traceback (most recent call last):
  File "tests/testy.py", line 24, in <module>
    print(t.get_html_string())
  File "/home/user/repo/prettytable/venv/lib/python3.8/site-packages/prettytable-3.2.1.dev44-py3.8.egg/prettytable/prettytable.py", line 2079, in get_html_string
  File "/home/user/repo/prettytable/venv/lib/python3.8/site-packages/prettytable-3.2.1.dev44-py3.8.egg/prettytable/prettytable.py", line 2199, in _get_formatted_html_string
TypeError: unhashable type: 'dict'
@myheroyuki myheroyuki added the bug Something isn't working label Sep 21, 2022
@myheroyuki myheroyuki self-assigned this Sep 21, 2022
@myheroyuki
Copy link
Contributor Author

@hugovk I noticed in the original add_autoindex pull request (#106) you pushed for automatically using the existing alignment values instead of having the user be able to set them specifically in this function. Do you still agree with that?

Either way, this could be solved by simply checking the type of self.align before setting the new alignment to be the same, which is probably the best short-term solution.

On the other hand, I've noticed alignment seems to cause a fair number of issues in this project, and the way it is implemented is not the most intuitive. Perhaps it would be better to refactor the way alignment works internally? This could be done as part of the work needed to add the separate header_align attribute from issue #102

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant