8000 ENH: add suffix option to array2str, wraps properly by ahaldane · Pull Request #10176 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: add suffix option to array2str, wraps properly #10176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2017
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
36 changes: 23 additions & 13 deletions numpy/core/arrayprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,8 @@ def _array2string(a, options, separator=' ', prefix=""):
def array2string(a, max_line_width=None, precision=None,
suppress_small=None, separator=' ', prefix="",
style=np._NoValue, formatter=None, threshold=None,
edgeitems=None, sign=None, floatmode=None, **kwarg):
edgeitems=None, sign=None, floatmode=None, suffix="",
**kwarg):
"""
Return a string representation of an array.

Expand All @@ -477,11 +478,14 @@ def array2string(a, max_line_width=None, precision=None,
separator : str, optional
Inserted between elements.
prefix : str, optional
An array is typically printed as::
suffix: str, optional
The length of the prefix and suffix strings are used to respectively
align and wrap the output. An array is typically printed as::

'prefix(' + array2string(a) + ')'
prefix + array2string(a) + suffix

The length of the prefix string is used to align the output correctly.
The output is left-padded by the length of the prefix string, and
wrapping is forced at the column ``max_line_width - len(suffix)``.
style : _NoValue, optional
Has no effect, do not use.

Expand Down Expand Up @@ -608,6 +612,9 @@ def array2string(a, max_line_width=None, precision=None,
" except in 1.13 'legacy' mode",
DeprecationWarning, stacklevel=3)

if options['legacy'] != '1.13':
options['linewidth'] -= len(suffix)

# treat as a null array if any of shape elements == 0
if a.size == 0:
return "[]"
Expand Down Expand Up @@ -1293,36 +1300,39 @@ def array_repr(arr, max_line_width=None, precision=None, suppress_small=None):
else:
class_name = "array"

skipdtype = dtype_is_implied(arr.dtype) and arr.size > 0

prefix = class_name + "("
suffix = ")" if skipdtype else ","

if (_format_options['legacy'] == '1.13' and
arr.shape == () and not arr.dtype.names):
lst = repr(arr.item())
elif arr.size > 0 or arr.shape == (0,):
lst = array2string(arr, max_line_width, precision, suppress_small,
', ', class_name + "(")
', ', prefix, suffix=suffix)
else: # show zero-length shape unless it is (0,)
lst = "[], shape=%s" % (repr(arr.shape),)

skipdtype = dtype_is_implied(arr.dtype) and arr.size > 0
arr_str = prefix + lst + suffix

if skipdtype:
return "%s(%s)" % (class_name, lst)
typename = dtype_short_repr(arr.dtype)
return arr_str

prefix = "{}({},".format(class_name, lst)
suffix = "dtype={})".format(typename)
dtype_str = "dtype={})".format(dtype_short_repr(arr.dtype))

# compute whether we should put dtype on a new line: Do so if adding the
# dtype would extend the last line past max_line_width.
# Note: This line gives the correct result even when rfind returns -1.
last_line_len = len(prefix) - (prefix.rfind('\n') + 1)
last_line_len = len(arr_str) - (arr_str.rfind('\n') + 1)
spacer = " "
if _format_options['legacy'] == '1.13':
if issubclass(arr.dtype.type, flexible):
spacer = '\n' + ' '*len(class_name + "(")
elif last_line_len + len(suffix) + 1 > max_line_width:
elif last_line_len + len(dtype_str) + 1 > max_line_width:
spacer = '\n' + ' '*len(class_name + "(")

return prefix + spacer + suffix
return arr_str + spacer + dtype_str

def array_str(a, max_line_width=None, precision=None, suppress_small=None):
"""
Expand Down
19 changes: 19 additions & 0 deletions numpy/core/tests/test_arrayprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,25 @@ def test_linewidth_repr(self):
2, 2, 2, 2])""")
)

a = np.full(8, fill_value=2)

np.set_printoptions(linewidth=18, legacy=False)
assert_equal(
repr(a),
textwrap.dedent("""\
array([2, 2, 2,
2, 2, 2,
2, 2])""")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing we prefer this output to the following:

array([2, 2, 2, 2,
       2, 2, 2, 2
       ])

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I say yes !

)

np.set_printoptions(linewidth=18, legacy='1.13')
assert_equal(
repr(a),
textwrap.dedent("""\
array([2, 2, 2, 2,
2, 2, 2, 2])""")
)

def test_linewidth_str(self):
a = np.full(18, fill_value=2)
np.set_printoptions(linewidth=18)
Expand Down
0