Skip to content

Commit

Permalink
Merge pull request #319 from airvzxf/bugfix/318/intfmt-string-values
Browse files Browse the repository at this point in the history
Bug fix for issue #318 | `intfmt`: error for strings which contain integer values
  • Loading branch information
astanin authored Sep 26, 2024
2 parents 8946da0 + 23b3cc6 commit 12a805b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tabulate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,17 @@ def _format(val, valtype, floatfmt, intfmt, missingval="", has_invisible=True):
if valtype is str:
return f"{val}"
elif valtype is int:
if isinstance(val, str):
val_striped = val.encode('unicode_escape').decode('utf-8')
colored = re.search(r'(\\[xX]+[0-9a-fA-F]+\[\d+[mM]+)([0-9.]+)(\\.*)$', val_striped)
if colored:
total_groups = len(colored.groups())
if total_groups == 3:
digits = colored.group(2)
if digits.isdigit():
val_new = colored.group(1) + format(int(digits), intfmt) + colored.group(3)
val = val_new.encode('utf-8').decode('unicode_escape')
intfmt = ""
return format(val, intfmt)
elif valtype is bytes:
try:
Expand Down
39 changes: 39 additions & 0 deletions test/test_output.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test output of the various forms of tabular data."""
from pytest import mark

import tabulate as tabulate_module
from common import assert_equal, raises, skip, check_warnings
Expand Down Expand Up @@ -2638,6 +2639,44 @@ def test_intfmt():
assert_equal(expected, result)


def test_intfmt_with_string_as_integer():
"Output: integer format"
result = tabulate([[82642], ["1500"], [2463]], intfmt=",", tablefmt="plain")
expected = "82,642\n 1500\n 2,463"
assert_equal(expected, result)


@mark.skip(reason="It detects all values as floats but there are strings and integers.")
def test_intfmt_with_string_with_floats():
"Output: integer format"
result = tabulate([[82000.38], ["1500.47"], ["2463"], [92165]], intfmt=",", tablefmt="plain")
expected = "82000.4\n 1500.47\n 2463\n92,165"
assert_equal(expected, result)


def test_intfmt_with_colors():
"Regression: Align ANSI-colored values as if they were colorless."
colortable = [
("\x1b[33mabc\x1b[0m", 42, "\x1b[31m42\x1b[0m"),
("\x1b[35mdef\x1b[0m", 987654321, "\x1b[32m987654321\x1b[0m"),
]
colorheaders = ("test", "\x1b[34mtest\x1b[0m", "test")
formatted = tabulate(colortable, colorheaders, "grid", intfmt=",")
expected = "\n".join(
[
"+--------+-------------+-------------+",
"| test | \x1b[34mtest\x1b[0m | test |",
"+========+=============+=============+",
"| \x1b[33mabc\x1b[0m | 42 | \x1b[31m42\x1b[0m |",
"+--------+-------------+-------------+",
"| \x1b[35mdef\x1b[0m | 987,654,321 | \x1b[32m987,654,321\x1b[0m |",
"+--------+-------------+-------------+",
]
)
print(f"expected: {expected!r}\n\ngot: {formatted!r}\n")
assert_equal(expected, formatted)


def test_empty_data_with_headers():
"Output: table with empty data and headers as firstrow"
expected = ""
Expand Down

0 comments on commit 12a805b

Please sign in to comment.