Skip to content

Shorter repr for attributes #1322

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 2 commits into from
Apr 3, 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
7 changes: 7 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ By `Henry S. Harrison <https://hsharrison.github.io>`_.
Note, the default is ``autoclose=False``, which is consistent with previous
xarray behavior. By `Phillip J. Wolfram <https://github.com/pwolfram>`_.

- The ``repr()`` of ``Dataset`` and ``DataArray`` attributes uses a similar
format to coordinates and variables, with vertically aligned entries
truncated to fit on a single line. Hopefully this will stop people writing
``data.attrs = {}`` and discarding metadata in notebooks for the sake of
cleaner output. The full metadata is still available as ``data.attrs``.
By `Zac Hatfield-Dodds <https://github.com/Zac-HD>`_.

Bug fixes
~~~~~~~~~
- ``rolling`` now keeps its original dimension order (:issue:`1125`).
Expand Down
11 changes: 9 additions & 2 deletions xarray/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,15 @@ def summarize_coord(name, var, col_width):


def summarize_attr(key, value, col_width=None):
# ignore col_width for now to more clearly distinguish attributes
return u' %s: %s' % (key, maybe_truncate(value))
"""Summary for __repr__ - use ``X.attrs[key]`` for full value."""
# Indent key and add ':', then right-pad if col_width is not None
k_str = u' %s:' % key
if col_width is not None:
k_str = pretty_print(k_str, col_width)
# Replace tabs and newlines, so we print on one line in known width
v_str = unicode_type(value).replace(u'\t', u'\\t').replace(u'\n', u'\\n')
# Finally, truncate to the desired display width
return maybe_truncate(u'%s %s' % (k_str, v_str), OPTIONS['display_width'])


EMPTY_REPR = u' *empty*'
Expand Down
2 changes: 1 addition & 1 deletion xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_repr(self):
other int64 0
Dimensions without coordinates: time
Attributes:
foo: bar""")
foo: bar""")
self.assertEqual(expected, repr(data_array))

def test_repr_multiindex(self):
Expand Down
4 changes: 2 additions & 2 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def test_repr(self):
var2 (dim1, dim2) float64 1.162 -1.097 -2.123 1.04 -0.4034 -0.126 ...
var3 (dim3, dim1) float64 0.5565 -0.2121 0.4563 1.545 -0.2397 0.1433 ...
Attributes:
foo: bar""") % data['dim3'].dtype
foo: bar""") % data['dim3'].dtype
actual = '\n'.join(x.rstrip() for x in repr(data).split('\n'))
print(actual)
self.assertEqual(expected, actual)
Expand Down Expand Up @@ -183,7 +183,7 @@ def test_unicode_data(self):
Data variables:
*empty*
Attributes:
å: ∑""" % u'ba®')
å: ∑""" % u'ba®')
actual = unicode_type(data)
self.assertEqual(expected, actual)

Expand Down
11 changes: 11 additions & 0 deletions xarray/tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@ def test_format_timestamp_out_of_bounds(self):
result = formatting.format_timestamp(date)
self.assertEqual(result, expected)

def test_attribute_repr(self):
short = formatting.summarize_attr(u'key', u'Short string')
long = formatting.summarize_attr(u'key', 100 * u'Very long string ')
newlines = formatting.summarize_attr(u'key', u'\n\n\n')
tabs = formatting.summarize_attr(u'key', u'\t\t\t')
self.assertEqual(short, ' key: Short string')
self.assertLessEqual(len(long), 80)
self.assertTrue(long.endswith(u'...'))
self.assertNotIn(u'\n', newlines)
self.assertNotIn(u'\t', tabs)


def test_set_numpy_options():
original_options = np.get_printoptions()
Expand Down
2 changes: 1 addition & 1 deletion xarray/tests/test_variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ def test_repr(self):
array([[1, 2, 3],
[4, 5, 6]])
Attributes:
foo: bar
foo: bar
""").strip()
self.assertEqual(expected, repr(v))

Expand Down