Skip to content
This repository has been archived by the owner on Dec 1, 2023. It is now read-only.

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Oct 30, 2023
1 parent 1f06a3f commit b065607
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
26 changes: 16 additions & 10 deletions src/biocgenerics/show_as_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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`.
Expand All @@ -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:
Expand All @@ -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
Expand Down
18 changes: 13 additions & 5 deletions tests/test_show_as_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
)
)

0 comments on commit b065607

Please sign in to comment.