Skip to content

Commit a3981f5

Browse files
committed
Enhance list_columns() function to accept different input types
1 parent 6ab2a58 commit a3981f5

2 files changed

Lines changed: 66 additions & 20 deletions

File tree

src/tests/test_output.py

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@
1212

1313
# ## Python StdLib Imports ----
1414
import logging
15-
from typing import Union
15+
from typing import Generator, Literal, Union
1616
from unittest import TestCase
1717

1818
# ## Python Third Party Imports ----
1919
import pytest
2020
import requests
21+
from parameterized import parameterized
2122
from typeguard import TypeCheckError
2223

2324
# ## Local First Party Imports ----
24-
from toolbox_python.collection_types import str_list
25+
from toolbox_python.collection_types import str_list, str_set, str_tuple
2526
from toolbox_python.output import list_columns, print_or_log_output
2627

2728

@@ -188,12 +189,11 @@ def get_list_of_words(num_words: int = 100) -> str_list:
188189
words: str_list = response.content.decode().splitlines()
189190
return words[:num_words]
190191

191-
def test_1(self) -> None:
192+
def test_1_defaults(self) -> None:
192193
output: Union[str, None] = list_columns(
193194
self.get_list_of_words(4 * 5),
194195
print_output=False,
195196
)
196-
197197
expected: str = "\n".join(
198198
[
199199
"a abandoned able abraham ",
@@ -205,15 +205,15 @@ def test_1(self) -> None:
205205
)
206206
assert output == expected
207207

208-
def test_2(self) -> None:
208+
def test_2_no_printing(self) -> None:
209209
output: Union[str, None] = list_columns(
210210
self.get_list_of_words(3),
211211
print_output=False,
212212
)
213213
expected = """a aa aaa """
214214
assert output == expected
215215

216-
def test_3(self) -> None:
216+
def test_3_columnwise(self) -> None:
217217
output: Union[str, None] = list_columns(
218218
self.get_list_of_words(5),
219219
cols_wide=2,
@@ -229,18 +229,60 @@ def test_3(self) -> None:
229229
)
230230
assert output == expected
231231

232-
def test_4(self) -> None:
232+
def test_4_print_output(self) -> None:
233233
list_columns(
234-
self.get_list_of_words(4 * 3),
235-
columnwise=False,
236-
cols_wide=4,
234+
self.get_list_of_words(4 * 5),
237235
print_output=True,
238236
)
239237
output: str_list = self.capsys.readouterr().out.split("\n")
240238
expected: str_list = [
241-
"a aa aaa aaron ",
242-
"ab abandoned abc aberdeen ",
243-
"abilities ability able aboriginal ",
239+
"a abandoned able abraham ",
240+
"aa abc aboriginal abroad ",
241+
"aaa aberdeen abortion abs ",
242+
"aaron abilities about absence ",
243+
"ab ability above absent ",
244244
"",
245245
]
246246
assert output == expected
247+
248+
def test_5_rowwise(self) -> None:
249+
output = list_columns(
250+
self.get_list_of_words(4 * 3),
251+
columnwise=False,
252+
cols_wide=4,
253+
print_output=False,
254+
)
255+
expected: str = "\n".join(
256+
[
257+
"a aa aaa aaron ",
258+
"ab abandoned abc aberdeen ",
259+
"abilities ability able aboriginal ",
260+
]
261+
)
262+
assert output == expected
263+
264+
@parameterized.expand([("list"), ("tuple"), ("set"), ("generator")])
265+
def test_6_types(self, input_type: Literal["list", "tuple", "set", "generator"]) -> None:
266+
words: str_list = self.get_list_of_words(4 * 3)
267+
expected: str = "\n".join(
268+
[
269+
"a aaron abc ability ",
270+
"aa ab aberdeen able ",
271+
"aaa abandoned abilities aboriginal ",
272+
]
273+
)
274+
if input_type == "list":
275+
input_data: str_list = list(words)
276+
elif input_type == "tuple":
277+
input_data: str_tuple = tuple(words)
278+
elif input_type == "set":
279+
input_data: str_set = set(words)
280+
expected = "\n".join(set(expected.splitlines()))
281+
elif input_type == "generator":
282+
input_data: Generator = (word for word in words)
283+
output: Union[str, None] = list_columns(input_data)
284+
if input_type != "set":
285+
assert output == expected
286+
else:
287+
for word in words:
288+
assert word in output

src/toolbox_python/output.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,20 @@
4141
# ## Python StdLib Imports ----
4242
from logging import Logger, _nameToLevel
4343
from math import ceil
44-
from typing import Any, Literal, Optional, Union
44+
from typing import Any, Generator, Literal, Optional, Union
4545

4646
# ## Python Third Party Imports ----
4747
from typeguard import typechecked
4848

4949
# ## Local First Party Imports ----
5050
from toolbox_python.checkers import is_type
51-
from toolbox_python.collection_types import log_levels, str_list
51+
from toolbox_python.collection_types import (
52+
any_list,
53+
any_set,
54+
any_tuple,
55+
log_levels,
56+
str_list,
57+
)
5258

5359

5460
# ---------------------------------------------------------------------------- #
@@ -230,11 +236,11 @@ def print_or_log_output(
230236

231237
@typechecked
232238
def list_columns(
233-
obj: list,
239+
obj: Union[any_list, any_set, any_tuple, Generator],
234240
cols_wide: int = 4,
235241
columnwise: bool = True,
236242
gap: int = 4,
237-
print_output: bool = True,
243+
print_output: bool = False,
238244
) -> Optional[str]:
239245
"""
240246
!!! note Summary
@@ -376,9 +382,7 @@ def list_columns(
376382
]
377383
if columnwise:
378384
if len(segmented_list[-1]) != cols_wide:
379-
segmented_list[-1].extend(
380-
[""] * (len(string_list) - len(segmented_list[-1]))
381-
)
385+
segmented_list[-1].extend([""] * (len(string_list) - len(segmented_list[-1])))
382386
combined_list: Union[list[str_list], Any] = zip(*segmented_list)
383387
else:
384388
combined_list = segmented_list

0 commit comments

Comments
 (0)