Skip to content

Commit

Permalink
Support unlimited max rows/columns
Browse files Browse the repository at this point in the history
  • Loading branch information
alugowski committed Sep 13, 2023
1 parent 5333fc7 commit 6240d31
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
24 changes: 17 additions & 7 deletions matrepr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@
@dataclass
class MatReprParams:
max_rows: int = 11
"""Maximum number of rows in output."""
"""
Maximum number of rows in output.
Set to None for unlimited.
"""

max_cols: int = 15
"""Maximum number of columns in output."""
"""
Maximum number of columns in output.
Set to None for unlimited.
"""

width_str: int = None
"""
Expand Down Expand Up @@ -140,18 +146,22 @@ def get(self, **kwargs):
ret.floatfmt_str = fmt_str
ret.floatfmt = lambda f: format(f, fmt_str)

# validate
ret._assert_one_of("cell_align", ['center', 'left', 'right'])
ret.max_rows = max(1, ret.max_rows)
ret.max_cols = max(1, ret.max_cols)

# Apply some default rules
if ret.max_rows is None:
ret.max_rows = 999_999_999
if ret.max_cols is None:
ret.max_cols = 999_999_999
if ret.title_latex is None:
ret.title_latex = ret.title

if ret.floatfmt_latex is None:
ret.floatfmt_latex = lambda f: python_scientific_to_latex_times10(ret.floatfmt(f))

# validate
ret._assert_one_of("cell_align", ['center', 'left', 'right'])
ret.max_rows = max(1, ret.max_rows)
ret.max_cols = max(1, ret.max_cols)

# compute automatic values
if kwargs.get("auto_width_str", False):
if "max_cols" not in kwargs and ret.width_str is None:
Expand Down
16 changes: 16 additions & 0 deletions tests/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ def test_contents_html(self):
for value in np.nditer(mat):
self.assertIn(f">{value}</td>", res)

def test_large_size(self):
mats = [
np.arange(1000),
np.arange(1000).reshape(25, 40),
]

for mat in mats:
res = to_str(mat, max_rows=None, max_cols=None)
self.assertNotIn("...", res)

res = to_html(mat, max_rows=None, max_cols=None)
self.assertNotIn("dot;", res)

res = to_latex(mat, max_rows=None, max_cols=None)
self.assertNotIn("dots", res)


if __name__ == '__main__':
unittest.main()

0 comments on commit 6240d31

Please sign in to comment.