Skip to content

Commit 37256fe

Browse files
committed
Refactoring and renamed arguments
1 parent 25759c4 commit 37256fe

File tree

3 files changed

+40
-35
lines changed

3 files changed

+40
-35
lines changed

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ Convert Python lists to ASCII tables
2121
from table2ascii import table2ascii
2222

2323
output = table2ascii(
24-
header_row=["#", "G", "H", "R", "S"],
24+
header=["#", "G", "H", "R", "S"],
2525
body=[["1", "30", "40", "35", "30"], ["2", "30", "40", "35", "30"]],
26-
footer_row=["SUM", "130", "140", "135", "130"],
26+
footer=["SUM", "130", "140", "135", "130"],
2727
)
2828

2929
print(output)

Diff for: table2ascii/__init__.py

+28-25
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,30 @@ class TableToAscii:
1212

1313
def __init__(
1414
self,
15-
header_row: Optional[List],
15+
header: Optional[List],
1616
body: Optional[List[List]],
17-
footer_row: Optional[List],
17+
footer: Optional[List],
1818
):
1919
"""Validate arguments and initialize fields"""
2020
# check if columns in header are different from footer
21-
if header_row and footer_row and len(header_row) != len(footer_row):
21+
if header and footer and len(header) != len(footer):
2222
raise ValueError("Header row and footer row must have the same length")
2323
# check if columns in header are different from body
24-
if header_row and body and len(body) > 0 and len(header_row) != len(body[0]):
24+
if header and body and len(body) > 0 and len(header) != len(body[0]):
2525
raise ValueError("Header row and body rows must have the same length")
26+
# check if columns in header are different from body
27+
if footer and body and len(body) > 0 and len(footer) != len(body[0]):
28+
raise ValueError("Footer row and body rows must have the same length")
2629
# check if any rows in body have a different number of columns
2730
if body and len(body) and tuple(filter(lambda r: len(r) != len(body[0]), body)):
2831
raise ValueError("All rows in body must have the same length")
2932

3033
# initialize fields
31-
self.__header_row = header_row
34+
self.__header = header
3235
self.__body = body
33-
self.__footer_row = footer_row
34-
self.__columns = self.count_columns()
35-
self.__cell_widths = self.get_column_widths()
36+
self.__footer = footer
37+
self.__columns = self.__count_columns()
38+
self.__cell_widths = self.__get_column_widths()
3639

3740
"""
3841
╔═════╦═══════════════════════╗ ABBBBBCBBBBBDBBBBBDBBBBBDBBBBBE
@@ -69,30 +72,30 @@ def __init__(
6972
"bottom_right_corner": "╝", # V
7073
}
7174

72-
def count_columns(self) -> int:
75+
def __count_columns(self) -> int:
7376
"""Get the number of columns in the table
7477
based on the provided header, footer, and body lists.
7578
"""
76-
if self.__header_row:
77-
return len(self.__header_row)
78-
if self.__footer_row:
79-
return len(self.__footer_row)
79+
if self.__header:
80+
return len(self.__header)
81+
if self.__footer:
82+
return len(self.__footer)
8083
if self.__body and len(self.__body) > 0:
8184
return len(self.__body[0])
8285
return 0
8386

84-
def get_column_widths(self) -> List[int]:
87+
def __get_column_widths(self) -> List[int]:
8588
"""Get the minimum number of characters needed for the values
8689
in each column in the table with 1 space of padding on each side.
8790
"""
8891
col_counts = []
8992
for i in range(self.__columns):
9093
# number of characters in column of i of header, each body row, and footer
91-
header_size = len(self.__header_row[i]) if self.__header_row else 0
94+
header_size = len(self.__header[i]) if self.__header else 0
9295
body_size = (
9396
map(lambda row, i=i: len(row[i]), self.__body) if self.__body else [0]
9497
)
95-
footer_size = len(self.__footer_row[i]) if self.__footer_row else 0
98+
footer_size = len(self.__footer[i]) if self.__footer else 0
9699
# get the max and add 2 for padding each side with a space
97100
col_counts.append(max(header_size, *body_size, footer_size) + 2)
98101
return col_counts
@@ -176,7 +179,7 @@ def __header_row_to_ascii(self) -> str:
176179
first_col_sep=self.__parts["first_col_sep"],
177180
column_seperator=self.__parts["middle_edge"],
178181
right_edge=self.__parts["left_and_right_edge"],
179-
filler=self.__header_row,
182+
filler=self.__header,
180183
)
181184

182185
def __footer_row_to_ascii(self) -> str:
@@ -186,7 +189,7 @@ def __footer_row_to_ascii(self) -> str:
186189
first_col_sep=self.__parts["first_col_sep"],
187190
column_seperator=self.__parts["middle_edge"],
188191
right_edge=self.__parts["left_and_right_edge"],
189-
filler=self.__footer_row,
192+
filler=self.__footer,
190193
)
191194

192195
def __header_sep_to_ascii(self) -> str:
@@ -225,14 +228,14 @@ def to_ascii(self) -> str:
225228
# top row of table
226229
table = self.__top_edge_to_ascii()
227230
# add table header
228-
if self.__header_row:
231+
if self.__header:
229232
table += self.__header_row_to_ascii()
230233
table += self.__header_sep_to_ascii()
231234
# add table body
232235
if self.__body:
233236
table += self.__body_to_ascii()
234237
# add table footer
235-
if self.__footer_row:
238+
if self.__footer:
236239
table += self.__footer_sep_to_ascii()
237240
table += self.__footer_row_to_ascii()
238241
# bottom row of table
@@ -242,15 +245,15 @@ def to_ascii(self) -> str:
242245

243246

244247
def table2ascii(
245-
header_row: Optional[List] = None,
248+
header: Optional[List] = None,
246249
body: Optional[List[List]] = None,
247-
footer_row: Optional[List] = None,
250+
footer: Optional[List] = None,
248251
) -> str:
249252
"""Convert a 2D Python table to ASCII text
250253
251254
### Arguments
252-
:param header_row: :class:`Optional[List]` List of column values in the table's header row
255+
:param header: :class:`Optional[List]` List of column values in the table's header row
253256
:param body: :class:`Optional[List[List]]` 2-dimensional list of values in the table's body
254-
:param footer_row: :class:`Optional[List]` List of column values in the table's footer row
257+
:param footer: :class:`Optional[List]` List of column values in the table's footer row
255258
"""
256-
return TableToAscii(header_row, body, footer_row).to_ascii()
259+
return TableToAscii(header, body, footer).to_ascii()

Diff for: tests/test_convert.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
from table2ascii import table2ascii as t2a
22

3+
import pytest
4+
35

46
def test_header_body_footer():
57
text = t2a(
6-
header_row=["#", "G", "H", "R", "S"],
8+
header=["#", "G", "H", "R", "S"],
79
body=[["1", "30", "40", "35", "30"], ["2", "30", "40", "35", "30"]],
8-
footer_row=["SUM", "130", "140", "135", "130"],
10+
footer=["SUM", "130", "140", "135", "130"],
911
)
1012
expected = (
1113
"╔═════╦═══════════════════════╗\n"
@@ -23,7 +25,7 @@ def test_header_body_footer():
2325
def test_body_footer():
2426
text = t2a(
2527
body=[["1", "30", "40", "35", "30"], ["2", "30", "40", "35", "30"]],
26-
footer_row=["SUM", "130", "140", "135", "130"],
28+
footer=["SUM", "130", "140", "135", "130"],
2729
)
2830
expected = (
2931
"╔═════╦═══════════════════════╗\n"
@@ -38,7 +40,7 @@ def test_body_footer():
3840

3941
def test_header_body():
4042
text = t2a(
41-
header_row=["#", "G", "H", "R", "S"],
43+
header=["#", "G", "H", "R", "S"],
4244
body=[["1", "30", "40", "35", "30"], ["2", "30", "40", "35", "30"]],
4345
)
4446
expected = (
@@ -54,8 +56,8 @@ def test_header_body():
5456

5557
def test_header_footer():
5658
text = t2a(
57-
header_row=["#", "G", "H", "R", "S"],
58-
footer_row=["SUM", "130", "140", "135", "130"],
59+
header=["#", "G", "H", "R", "S"],
60+
footer=["SUM", "130", "140", "135", "130"],
5961
)
6062
expected = (
6163
"╔═════╦═══════════════════════╗\n"
@@ -70,7 +72,7 @@ def test_header_footer():
7072

7173
def test_header():
7274
text = t2a(
73-
header_row=["#", "G", "H", "R", "S"],
75+
header=["#", "G", "H", "R", "S"],
7476
)
7577
expected = (
7678
"╔═══╦═══════════════╗\n"
@@ -96,7 +98,7 @@ def test_body():
9698

9799
def test_footer():
98100
text = t2a(
99-
footer_row=["SUM", "130", "140", "135", "130"],
101+
footer=["SUM", "130", "140", "135", "130"],
100102
)
101103
expected = (
102104
"╔═════╦═══════════════════════╗\n"

0 commit comments

Comments
 (0)