From 61726ae1730c80af54ec2f1a52ca472e1cc6ae00 Mon Sep 17 00:00:00 2001 From: Stephan Hoyer Date: Wed, 25 Feb 2015 23:36:37 -0800 Subject: [PATCH 1/3] Truncate long attributes when printing datasets Only the first 500 characters are now shown, e.g., In [2]: xray.Dataset(attrs={'foo': 'bar' * 1000}) Out[2]: Dimensions: () Coordinates: *empty* Data variables: *empty* Attributes: foo: barbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarb arbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarba rbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbar barbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarb arbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarba rbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbar barbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarb arbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarbarba... --- doc/whats-new.rst | 6 +++++- xray/core/formatting.py | 9 ++++++++- xray/test/test_dataset.py | 4 ++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 37b031489c5..1c19067bc4c 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -136,7 +136,11 @@ Enhancements - The :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:`337`). This should make things more convenient for working with + datasets interactively. +- Added a new `documentation example `_ on calculating + properly weighted seasonal means. Thanks Joe Hamman! Bug fixes ~~~~~~~~~ diff --git a/xray/core/formatting.py b/xray/core/formatting.py index 2f520750978..304213f3a62 100644 --- a/xray/core/formatting.py +++ b/xray/core/formatting.py @@ -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*' diff --git a/xray/test/test_dataset.py b/xray/test/test_dataset.py index 67c491cabb3..fc76be54214 100644 --- a/xray/test/test_dataset.py +++ b/xray/test/test_dataset.py @@ -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)) From 507b9d465fa3b93ab98fef51fa7e777a1ded6035 Mon Sep 17 00:00:00 2001 From: Stephan Hoyer Date: Wed, 25 Feb 2015 23:57:04 -0800 Subject: [PATCH 2/3] more documentation fixes --- doc/api.rst | 2 ++ doc/examples/monthly-means.rst | 2 ++ doc/whats-new.rst | 13 +++++++------ 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/doc/api.rst b/doc/api.rst index 954b27f2dd5..c2140ed0a42 100644 --- a/doc/api.rst +++ b/doc/api.rst @@ -77,6 +77,7 @@ Comparisons Dataset.equals Dataset.identical + Dataset.broadcast_equals Indexing -------- @@ -236,6 +237,7 @@ Comparisons DataArray.equals DataArray.identical + DataArray.broadcast_equals IO / Conversion =============== diff --git a/doc/examples/monthly-means.rst b/doc/examples/monthly-means.rst index c39175409ef..906a1834bbb 100644 --- a/doc/examples/monthly-means.rst +++ b/doc/examples/monthly-means.rst @@ -1,3 +1,5 @@ +.. _monthly means example: + Calculating Seasonal Averages from Timeseries of Monthly Means ============================================================== diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 1c19067bc4c..f683c76ea13 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -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 ` and - :py:meth:`xray.DataArray.drop ` methods makes it easy to drop +- The new :py:meth:`Dataset.drop ` and + :py:meth:`DataArray.drop ` methods makes it easy to drop explicitly listed variables or index labels: .. ipython:: python @@ -134,13 +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. - Long attributes are now truncated at 500 characters when printing a dataset (:issue:`337`). This should make things more convenient for working with datasets interactively. -- Added a new `documentation example `_ on calculating - properly weighted seasonal means. Thanks Joe Hamman! +- Added a new documentation example, :ref:`monthly means example`. Thanks Joe + Hamman! Bug fixes ~~~~~~~~~ From 5b8dbef12748fb1aecb00aa02982afcbd3bd3456 Mon Sep 17 00:00:00 2001 From: Stephan Hoyer Date: Wed, 25 Feb 2015 23:58:03 -0800 Subject: [PATCH 3/3] Fix issue number --- doc/whats-new.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index f683c76ea13..b98c7abebb8 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -138,7 +138,7 @@ Enhancements - :py:meth:`~xray.Dataset.broadcast_equals` has been added to correspond to the new ``compat`` option. - Long attributes are now truncated at 500 characters when printing a dataset - (:issue:`337`). This should make things more convenient for working with + (: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!