Skip to content

Commit

Permalink
Merge pull request #338 from shoyer/truncate-long-atrs
Browse files Browse the repository at this point in the history
Truncate long attributes when printing datasets
  • Loading branch information
shoyer committed Feb 26, 2015
2 parents 3eb015b + 5b8dbef commit 9eb9b91
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
2 changes: 2 additions & 0 deletions doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Comparisons

Dataset.equals
Dataset.identical
Dataset.broadcast_equals

Indexing
--------
Expand Down Expand Up @@ -236,6 +237,7 @@ Comparisons

DataArray.equals
DataArray.identical
DataArray.broadcast_equals

IO / Conversion
===============
Expand Down
2 changes: 2 additions & 0 deletions doc/examples/monthly-means.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.. _monthly means example:

Calculating Seasonal Averages from Timeseries of Monthly Means
==============================================================

Expand Down
15 changes: 10 additions & 5 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,13 @@ Enhancements
- Consolidated the functionality of ``dumps`` (writing a dataset to a netCDF3
bytestring) into :py:meth:`~xray.Dataset.to_netcdf` (:issue:`333`).
- :py:meth:`~xray.Dataset.to_netcdf` now supports writing to groups in netCDF4
files (:issue:`333`). It also now has a full docstring -- you should read it!
files (:issue:`333`). It also finally has a full docstring -- you should read
it!
- :py:func:`~xray.open_dataset` and :py:meth:`~xray.Dataset.to_netcdf` now
work on netCDF4 files when netcdf4-python is not installed as long as scipy
is available (:issue:`333`).
- The new :py:meth:`xray.Dataset.drop <Dataset.drop>` and
:py:meth:`xray.DataArray.drop <DataArray.drop>` methods makes it easy to drop
- The new :py:meth:`Dataset.drop <xray.Dataset.drop>` and
:py:meth:`DataArray.drop <xray.DataArray.drop>` methods makes it easy to drop
explicitly listed variables or index labels:

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

Bug fixes
~~~~~~~~~
Expand Down
9 changes: 8 additions & 1 deletion xray/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,16 @@ def summarize_coord(name, var, col_width):
return _summarize_var_or_coord(name, var, col_width, show_values, marker)


def _maybe_truncate(obj, maxlen=500):
s = str(obj)
if len(s) > maxlen:
s = s[:(maxlen - 3)] + '...'
return s


def summarize_attr(key, value, col_width=None):
# ignore col_width for now to more clearly distinguish attributes
return ' %s: %s' % (key, value)
return ' %s: %s' % (key, _maybe_truncate(value))


EMPTY_REPR = ' *empty*'
Expand Down
4 changes: 4 additions & 0 deletions xray/test/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ def test_repr(self):
print(actual)
self.assertEqual(expected, actual)

# verify long attributes are truncated
data = Dataset(attrs={'foo': 'bar' * 1000})
self.assertTrue(len(repr(data)) < 1000)

def test_constructor(self):
x1 = ('x', 2 * np.arange(100))
x2 = ('x', np.arange(1000))
Expand Down

0 comments on commit 9eb9b91

Please sign in to comment.