diff --git a/src/biocgenerics/show_as_cell.py b/src/biocgenerics/show_as_cell.py index a7ead7a..cf282a9 100644 --- a/src/biocgenerics/show_as_cell.py +++ b/src/biocgenerics/show_as_cell.py @@ -4,13 +4,12 @@ @singledispatch def show_as_cell(x: Any, indices: Sequence[int]) -> List[str]: - """ - Show the contents of ``x`` as a cell of a table, typically for use in - the ``__repr__`` method of a class that contains ``x``. + """Show the contents of ``x`` as a cell of a table, typically for use in the ``__repr__`` method of a class that + contains ``x``. Args: - x: - Any object. By default, we assume that it can be treated as + x: + Any object. By default, we assume that it can be treated as a sequence, with a valid ``__getitem__`` method for an index. indices: @@ -34,12 +33,16 @@ def _get_max_width(col: List[str]): return width -def format_table(columns: List[Sequence[str]], floating_names: Optional[Sequence[str]] = None, sep: str = " ", window: Optional[int] = None): - """ - Pretty-print a table with wrapping columns. +def format_table( + columns: List[Sequence[str]], + floating_names: Optional[Sequence[str]] = None, + sep: str = " ", + window: Optional[int] = None, +): + """Pretty-print a table with wrapping columns. Args: - columns: + columns: List of list of strings, where each inner list is the same length. Strings are typically generated by :py:meth:`~show_as_cell`. @@ -59,6 +62,7 @@ def format_table(columns: List[Sequence[str]], floating_names: Optional[Sequence """ if window is None: import os + try: window = os.get_terminal_size().columns except: @@ -77,11 +81,13 @@ def format_table(columns: List[Sequence[str]], floating_names: Optional[Sequence floating_names = new_floating_names output = "" + def reinitialize(): if floating_names is None: - return [ "" ] * n + return [""] * n else: return floating_names[:] + contents = reinitialize() init = True used = floatwidth diff --git a/tests/test_show_as_cell.py b/tests/test_show_as_cell.py index be6375c..e2c937c 100644 --- a/tests/test_show_as_cell.py +++ b/tests/test_show_as_cell.py @@ -2,13 +2,21 @@ def test_show_as_cell(): - assert show_as_cell([1,2,3,4], range(4)) == ["1", "2", "3", "4"] - assert show_as_cell([1,2,3,4], [1,3]) == ["2", "4"] + assert show_as_cell([1, 2, 3, 4], range(4)) == ["1", "2", "3", "4"] + assert show_as_cell([1, 2, 3, 4], [1, 3]) == ["2", "4"] def test_format_table(): - contents = [ [ "asdasd", "1", "2", "3", "4" ], [""] + ["|"] * 4, ["asyudgausydga", "A", "B", "C", "D"] ] + contents = [ + ["asdasd", "1", "2", "3", "4"], + [""] + ["|"] * 4, + ["asyudgausydga", "A", "B", "C", "D"], + ] print(format_table(contents)) - print(format_table(contents, floating_names = [ "", "aarg", "boo", "ffoo", "stuff" ])) + print(format_table(contents, floating_names=["", "aarg", "boo", "ffoo", "stuff"])) print(format_table(contents, window=10)) - print(format_table(contents, window=10, floating_names = [ "", "AAAR", "BBBB", "XXX", "STUFF" ])) + print( + format_table( + contents, window=10, floating_names=["", "AAAR", "BBBB", "XXX", "STUFF"] + ) + )