Skip to content

Commit 9eb9b91

Browse files
committed
Merge pull request #338 from shoyer/truncate-long-atrs
Truncate long attributes when printing datasets
2 parents 3eb015b + 5b8dbef commit 9eb9b91

File tree

5 files changed

+26
-6
lines changed

5 files changed

+26
-6
lines changed

doc/api.rst

+2
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ Comparisons
7777

7878
Dataset.equals
7979
Dataset.identical
80+
Dataset.broadcast_equals
8081

8182
Indexing
8283
--------
@@ -236,6 +237,7 @@ Comparisons
236237

237238
DataArray.equals
238239
DataArray.identical
240+
DataArray.broadcast_equals
239241

240242
IO / Conversion
241243
===============

doc/examples/monthly-means.rst

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _monthly means example:
2+
13
Calculating Seasonal Averages from Timeseries of Monthly Means
24
==============================================================
35

doc/whats-new.rst

+10-5
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,13 @@ Enhancements
116116
- Consolidated the functionality of ``dumps`` (writing a dataset to a netCDF3
117117
bytestring) into :py:meth:`~xray.Dataset.to_netcdf` (:issue:`333`).
118118
- :py:meth:`~xray.Dataset.to_netcdf` now supports writing to groups in netCDF4
119-
files (:issue:`333`). It also now has a full docstring -- you should read it!
119+
files (:issue:`333`). It also finally has a full docstring -- you should read
120+
it!
120121
- :py:func:`~xray.open_dataset` and :py:meth:`~xray.Dataset.to_netcdf` now
121122
work on netCDF4 files when netcdf4-python is not installed as long as scipy
122123
is available (:issue:`333`).
123-
- The new :py:meth:`xray.Dataset.drop <Dataset.drop>` and
124-
:py:meth:`xray.DataArray.drop <DataArray.drop>` methods makes it easy to drop
124+
- The new :py:meth:`Dataset.drop <xray.Dataset.drop>` and
125+
:py:meth:`DataArray.drop <xray.DataArray.drop>` methods makes it easy to drop
125126
explicitly listed variables or index labels:
126127

127128
.. ipython:: python
@@ -134,9 +135,13 @@ Enhancements
134135
arr = xray.DataArray([1, 2, 3], coords=[('x', list('abc'))])
135136
arr.drop(['a', 'c'], dim='x')
136137
137-
- The :py:meth:`~xray.Dataset.broadcast_equals` has been added to correspond to
138+
- :py:meth:`~xray.Dataset.broadcast_equals` has been added to correspond to
138139
the new ``compat`` option.
139-
- TODO: added a documentation example by Joe Hamman.
140+
- Long attributes are now truncated at 500 characters when printing a dataset
141+
(:issue:`338`). This should make things more convenient for working with
142+
datasets interactively.
143+
- Added a new documentation example, :ref:`monthly means example`. Thanks Joe
144+
Hamman!
140145

141146
Bug fixes
142147
~~~~~~~~~

xray/core/formatting.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,16 @@ def summarize_coord(name, var, col_width):
143143
return _summarize_var_or_coord(name, var, col_width, show_values, marker)
144144

145145

146+
def _maybe_truncate(obj, maxlen=500):
147+
s = str(obj)
148+
if len(s) > maxlen:
149+
s = s[:(maxlen - 3)] + '...'
150+
return s
151+
152+
146153
def summarize_attr(key, value, col_width=None):
147154
# ignore col_width for now to more clearly distinguish attributes
148-
return ' %s: %s' % (key, value)
155+
return ' %s: %s' % (key, _maybe_truncate(value))
149156

150157

151158
EMPTY_REPR = ' *empty*'

xray/test/test_dataset.py

+4
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ def test_repr(self):
103103
print(actual)
104104
self.assertEqual(expected, actual)
105105

106+
# verify long attributes are truncated
107+
data = Dataset(attrs={'foo': 'bar' * 1000})
108+
self.assertTrue(len(repr(data)) < 1000)
109+
106110
def test_constructor(self):
107111
x1 = ('x', 2 * np.arange(100))
108112
x2 = ('x', np.arange(1000))

0 commit comments

Comments
 (0)