Updating `repr()` function by vtavana · Pull Request #2067 · IntelPython/dpctl · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
33 changes: 23 additions & 10 deletions dpctl/tensor/_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@
}


def _move_to_next_line(string, s, line_width, prefix):
"""
Move string to next line if it doesn't fit in the current line.
"""
bottom_len = len(s) - (s.rfind("\n") + 1)
next_line = bottom_len + len(string) + 1 > line_width
string = ",\n" + " " * len(prefix) + string if next_line else ", " + string

return string


def _options_dict(
linewidth=None,
edgeitems=None,
Expand Down Expand Up @@ -463,16 +474,18 @@ def usm_ndarray_repr(
suffix=suffix,
)

if show_dtype:
dtype_str = "dtype={}".format(x.dtype.name)
bottom_len = len(s) - (s.rfind("\n") + 1)
next_line = bottom_len + len(dtype_str) + 1 > line_width
dtype_str = (
",\n" + " " * len(prefix) + dtype_str
if next_line
else ", " + dtype_str
)
if show_dtype or x.size == 0:
dtype_str = f"dtype={x.dtype.name}"
dtype_str = _move_to_next_line(dtype_str, s, line_width, prefix)
else:
dtype_str = ""

return prefix + s + dtype_str + suffix
options = get_print_options()
threshold = options["threshold"]
if (x.size == 0 and x.shape != (0,)) or x.size > threshold:
shape_str = f"shape={x.shape}"
shape_str = _move_to_next_line(shape_str, s, line_width, prefix)
else:
shape_str = ""

return prefix + s + shape_str + dtype_str + suffix
18 changes: 11 additions & 7 deletions dpctl/tests/test_usm_ndarray_print.py