Skip to content

Commit 2f0b933

Browse files
authored
Merge pull request #23 from DenverCoder1/stringify-ints
2 parents 58fd31e + 922b9fc commit 2f0b933

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

Diff for: table2ascii/table_to_ascii.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from math import ceil, floor
2-
from typing import List, Optional, Union
2+
from typing import Any, List, Optional, Union
33

44
from .alignment import Alignment
55
from .options import Options
@@ -93,27 +93,30 @@ def __auto_column_widths(self) -> List[int]:
9393
column_widths = []
9494
for i in range(self.__columns):
9595
# number of characters in column of i of header, each body row, and footer
96-
header_size = len(self.__header[i]) if self.__header else 0
96+
header_size = len(str(self.__header[i])) if self.__header else 0
9797
body_size = (
98-
map(lambda row, i=i: len(row[i]), self.__body) if self.__body else [0]
98+
map(lambda row, i=i: len(str(row[i])), self.__body)
99+
if self.__body
100+
else [0]
99101
)
100-
footer_size = len(self.__footer[i]) if self.__footer else 0
102+
footer_size = len(str(self.__footer[i])) if self.__footer else 0
101103
# get the max and add 2 for padding each side with a space
102104
column_widths.append(max(header_size, *body_size, footer_size) + 2)
103105
return column_widths
104106

105-
def __pad(self, text: str, width: int, alignment: Alignment) -> str:
107+
def __pad(self, cell_value: Any, width: int, alignment: Alignment) -> str:
106108
"""
107109
Pad a string of text to a given width with specified alignment
108110
109111
Args:
110-
text (str): The text to pad
112+
cell_value (Any): The text in the cell to pad
111113
width (int): The width in characters to pad to
112114
alignment (Alignment): The alignment to use
113115
114116
Returns:
115117
str: The padded text
116118
"""
119+
text = str(cell_value)
117120
if alignment == Alignment.LEFT:
118121
# pad with spaces on the end
119122
return f" {text} " + (" " * (width - len(text) - 2))
@@ -152,7 +155,7 @@ def __row_to_ascii(
152155
if isinstance(filler, str)
153156
# otherwise, use the column content
154157
else self.__pad(
155-
str(filler[i]), self.__column_widths[i], self.__alignments[i]
158+
filler[i], self.__column_widths[i], self.__alignments[i]
156159
)
157160
)
158161
# column seperator

Diff for: tests/test_alignments.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from table2ascii import table2ascii as t2a, Alignment
1+
from table2ascii import alignment, table2ascii as t2a, Alignment
22

33
import pytest
44

@@ -44,3 +44,24 @@ def test_invalid_alignments():
4444
first_col_heading=True,
4545
alignments=[9999, -1, Alignment.RIGHT, Alignment.CENTER, Alignment.RIGHT],
4646
)
47+
48+
49+
def test_alignment_numeric_data():
50+
text = t2a(
51+
header=[1, "G", "H", "R", "S"],
52+
body=[[1, 2, 3, 4, 5]],
53+
footer=["A", "B", 1, 2, 3],
54+
column_widths=[4, 5, 5, 4, 5],
55+
alignments=[Alignment.RIGHT] + [Alignment.CENTER] * 4,
56+
first_col_heading=True,
57+
)
58+
expected = (
59+
"╔════╦══════════════════════╗\n"
60+
"║ 1 ║ G H R S ║\n"
61+
"╟────╫──────────────────────╢\n"
62+
"║ 1 ║ 2 3 4 5 ║\n"
63+
"╟────╫──────────────────────╢\n"
64+
"║ A ║ B 1 2 3 ║\n"
65+
"╚════╩══════════════════════╝"
66+
)
67+
assert text == expected

Diff for: tests/test_convert.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from table2ascii import table2ascii as t2a
1+
from table2ascii import alignment, table2ascii as t2a
22

33
import pytest
44

@@ -179,3 +179,23 @@ def test_empty_body():
179179
"╚═══╩═══════════════╝"
180180
)
181181
assert text == expected
182+
183+
184+
def test_numeric_data():
185+
text = t2a(
186+
header=[1, "G", "H", "R", "S"],
187+
body=[[1, 2, 3, 4, 5]],
188+
footer=["A", "B", 1, 2, 3],
189+
column_widths=[4, 5, 5, 4, 5],
190+
first_col_heading=True,
191+
)
192+
expected = (
193+
"╔════╦══════════════════════╗\n"
194+
"║ 1 ║ G H R S ║\n"
195+
"╟────╫──────────────────────╢\n"
196+
"║ 1 ║ 2 3 4 5 ║\n"
197+
"╟────╫──────────────────────╢\n"
198+
"║ A ║ B 1 2 3 ║\n"
199+
"╚════╩══════════════════════╝"
200+
)
201+
assert text == expected

0 commit comments

Comments
 (0)