Skip to content

Commit 3d43b32

Browse files
authored
Merge pull request #10129 from eric-wieser/remove-trailing-space
ENH: Strip trailing spaces from continuation in multiline arrayprint
2 parents 2c31091 + 6df0b6e commit 3d43b32

File tree

2 files changed

+78
-3
lines changed

2 files changed

+78
-3
lines changed

numpy/core/arrayprint.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -664,24 +664,28 @@ def _formatArray(a, format_function, rank, max_line_len, next_line_prefix,
664664
else:
665665
s = '['
666666
sep = separator.rstrip()
667+
line_sep = '\n'*max(rank-1, 1)
667668
for i in range(leading_items):
668669
if i > 0:
669670
s += next_line_prefix
670671
s += _formatArray(a[i], format_function, rank-1, max_line_len,
671672
" " + next_line_prefix, separator, edge_items,
672673
summary_insert, legacy)
673-
s = s.rstrip() + sep.rstrip() + '\n'*max(rank-1, 1)
674+
s = s.rstrip() + sep.rstrip() + line_sep
674675

675676
if summary_insert1:
676-
s += next_line_prefix + summary_insert1 + "\n"
677+
if legacy == '1.13':
678+
s += next_line_prefix + summary_insert1 + "\n"
679+
else:
680+
s += next_line_prefix + summary_insert1.strip() + line_sep
677681

678682
for i in range(trailing_items, 1, -1):
679683
if leading_items or i != trailing_items:
680684
s += next_line_prefix
681685
s += _formatArray(a[-i], format_function, rank-1, max_line_len,
682686
" " + next_line_prefix, separator, edge_items,
683687
summary_insert, legacy)
684-
s = s.rstrip() + sep.rstrip() + '\n'*max(rank-1, 1)
688+
s = s.rstrip() + sep.rstrip() + line_sep
685689
if leading_items or trailing_items > 1:
686690
s += next_line_prefix
687691
s += _formatArray(a[-1], format_function, rank-1, max_line_len,

numpy/core/tests/test_arrayprint.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,77 @@ def test_dtype_linewidth_wrapping(self):
509509
array(['1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1'],
510510
dtype='{}')""".format(styp)))
511511

512+
def test_edgeitems(self):
513+
np.set_printoptions(edgeitems=1, threshold=1)
514+
a = np.arange(27).reshape((3, 3, 3))
515+
assert_equal(
516+
repr(a),
517+
textwrap.dedent("""\
518+
array([[[ 0, ..., 2],
519+
...,
520+
[ 6, ..., 8]],
521+
522+
...,
523+
524+
[[18, ..., 20],
525+
...,
526+
[24, ..., 26]]])""")
527+
)
528+
529+
b = np.zeros((3, 3, 1, 1))
530+
assert_equal(
531+
repr(b),
532+
textwrap.dedent("""\
533+
array([[[[0.]],
534+
535+
...,
536+
537+
[[0.]]],
538+
539+
540+
...,
541+
542+
543+
[[[0.]],
544+
545+
...,
546+
547+
[[0.]]]])""")
548+
)
549+
550+
# 1.13 had extra trailing spaces, and was missing newlines
551+
np.set_printoptions(legacy='1.13')
552+
553+
assert_equal(
554+
repr(a),
555+
textwrap.dedent("""\
556+
array([[[ 0, ..., 2],
557+
...,
558+
[ 6, ..., 8]],
559+
560+
...,
561+
[[18, ..., 20],
562+
...,
563+
[24, ..., 26]]])""")
564+
)
565+
566+
assert_equal(
567+
repr(b),
568+
textwrap.dedent("""\
569+
array([[[[ 0.]],
570+
571+
...,
572+
[[ 0.]]],
573+
574+
575+
...,
576+
[[[ 0.]],
577+
578+
...,
579+
[[ 0.]]]])""")
580+
)
581+
582+
512583
def test_unicode_object_array():
513584
import sys
514585
if sys.version_info[0] >= 3:

0 commit comments

Comments
 (0)