8000 BUG: Extra space is inserted on first line for long elements by eric-wieser · Pull Request #10187 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Extra space is inserted on first line for long elements #10187

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 10, 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
12 changes: 6 additions & 6 deletions doc/release/1.14.0-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -265,26 +265,26 @@ In summary, the major changes are:
* Float arrays printed in scientific notation no longer use fixed-precision,
and now instead show the shortest unique representation.
* The ``str`` of floating-point scalars is no longer truncated in python2.

* For other data types:

* Non-finite complex scalars print like ``nanj`` instead of ``nan*j``.
* ``NaT`` values in datetime arrays are now properly aligned.
* Arrays and scalars of ``np.void`` datatype are now printed using hex
notation.

* For line-wrapping:

* The "dtype" part of ndarray reprs will now be printed on the next line
if there isn't space on the last line of array output.
* The ``linewidth`` format option is now always respected.
The `repr` or `str` of an array will never exceed this, unless a single
element is too wide.
* All but the last line of array strings will contain the same number of
elements.
* The last line of an array string will never have more elements than earlier
lines.

* An extra space is no longer inserted on the first line if the elements are
too wide.

* For summarization (the use of ``...`` to shorten long arrays):

* A trailing comma is no longer inserted for ``str``.
Expand All @@ -294,7 +294,7 @@ In summary, the major changes are:
order to summarize any but the last axis, newlines are now appended to that
line to match its leading newlines and a trailing space character is
removed.

* ``MaskedArray`` arrays now separate printed elements with commas, always
print the dtype, and correctly wrap the elements of long arrays to multiple
lines. If there is more than 1 dimension, the array attributes are now
Expand Down
22 changes: 16 additions & 6 deletions numpy/core/arrayprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,8 +622,14 @@ def array2string(a, max_line_width=None, precision=None,
return _array2string(a, options, separator, prefix)


def _extendLine(s, line, word, line_width, next_line_prefix):
if len(line) + len(word) > line_width:
def _extendLine(s, line, word, line_width, next_line_prefix, legacy):
needs_wrap = len(line) + len(word) > line_width
if legacy != '1.13':
s# don't wrap lines if it won't help
if len(line) <= len(next_line_prefix):
needs_wrap = False

if needs_wrap:
s += line.rstrip() + "\n"
line = next_line_prefix
line += word
Expand Down Expand Up @@ -682,26 +688,30 @@ def recurser(index, hanging_indent, curr_width):
line = hanging_indent
for i in range(leading_items):
word = recurser(index + (i,), next_hanging_indent, next_width)
s, line = _extendLine(s, line, word, elem_width, hanging_indent)
s, line = _extendLine(
s, line, word, elem_width, hanging_indent, legacy)
line += separator

if show_summary:
s, line = _extendLine(s, line, summary_insert, elem_width, hanging_indent)
s, line = _extendLine(
s, line, summary_insert, elem_width, hanging_indent, legacy)
if legacy == '1.13':
line += ", "
else:
line += separator

for i in range(trailing_items, 1, -1):
word = recurser(index + (-i,), next_hanging_indent, next_width)
s, line = _extendLine(s, line, word, elem_width, hanging_indent)
s, line = _extendLine(
s, line, word, elem_width, hanging_indent, legacy)
line += separator

if legacy == '1.13':
# width of the seperator is not considered on 1.13
elem_width = curr_width
word = recurser(index + (-1,), next_hanging_indent, next_width)
s, line = _extendLine(s, line, word, elem_width, hanging_indent)
s, line = _extendLine(
s, line, word, elem_width, hanging_indent, legacy)

s += line

Expand Down
11 changes: 11 additions & 0 deletions numpy/core/tests/test_arrayprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,17 @@ def make_str(a, width, **kw):
' 11\n'
' 11]]]')

def test_wide_element(self):
a = np.array(['xxxxx'])
assert_equal(
np.array2string(a, max_line_width=5),
"['xxxxx']"
)
assert_equal(
np.array2string(a, max_line_width=5, legacy='1.13'),
"[ 'xxxxx']"
)


class TestPrintOptions(object):
"""Test getting and setting global print options."""
Expand Down
0