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

Fix #135. Get rid of exceptions when converting values to numbers. #236

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ilya112358
Copy link
Contributor

@ilya112358 ilya112358 commented Jan 2, 2023

Fix #135

  • Add the test to check for exceptions
  • Fix for booleans to float conversion
  • Fix for booleans and strings formatted as integers
  • Recognize ansi colors for booleans and integers
  • Improve readability

Note: there is (and has been for a while as I understand it) an inconsistency in boolean representation in the columns which contain a mix of bools and numbers. Booleans were passed as text in case of unformatted integer column type, and converted into [0, 1] otherwise. For example:

>>> print(tabulate([[1.1, 1], [True, True]]))
---  ----
1.1     1
1    True
---  ----

This behavior is PRESERVED in the current fix, no breaking changes introduced, just "True" and True are handled in the same way now.

This pull request also fixes another issue (see below).

@ilya112358
Copy link
Contributor Author

So, another issue is that ansi color codes are recognized only for floats. Presumably, it doesn't matter for nones, strings and bytes but in case of bools and integers this leads to the same kind of exceptions.

>>> print(tabulate([[1000, 1.1],['\033[31m1000\033[0m', '\033[31mTrue\033[0m']], intfmt=','))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Ibo\Python\python-tabulate\tabulate\__init__.py", line 2176, in tabulate
    cols = [
           ^
  File "C:\Users\Ibo\Python\python-tabulate\tabulate\__init__.py", line 2177, in <listcomp>
    [_format(v, ct, fl_fmt, int_fmt, miss_v, has_invisible) for v in c]
  File "C:\Users\Ibo\Python\python-tabulate\tabulate\__init__.py", line 2177, in <listcomp>
    [_format(v, ct, fl_fmt, int_fmt, miss_v, has_invisible) for v in c]
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ibo\Python\python-tabulate\tabulate\__init__.py", line 1231, in _format
    return format(val, intfmt)
           ^^^^^^^^^^^^^^^^^^^
ValueError: Cannot specify ',' with 's'.

This should be fixed now.
image

@ilya112358 ilya112358 marked this pull request as ready for review January 5, 2023 11:22
@airknits
Copy link

airknits commented Jun 5, 2023

Running into the same issue--can we please get a review for this PR? 🙏

@Adamou02
Copy link

Please PR this, I have the same issue here (#289)

@jimmyzzxhlh
Copy link

+1 on making this fix. Can we get a review?

@astanin
Copy link
Owner

astanin commented Sep 26, 2024

This PR still breaks test_mixed_bools

----------------------------- Captured stdout call -----------------------------
Expected:
---  -----  -----
1.1   1000  1,000
1.0   True      1
0.0  False      0
---  -----  -----

Got:
---  -----  -----
1.1   1000   1000
1.0   True      1
0.0  False  False
---  -----  -----

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ValueError: could not convert string to float: 'True'
5 participants