From cf60a9c6f3a50c11d70ea2dc45b592795d0c884c Mon Sep 17 00:00:00 2001 From: David Hassell Date: Tue, 29 Mar 2022 16:04:08 +0100 Subject: [PATCH 1/3] inspect --- cf/data/data.py | 11 ++++++++++- cf/functions.py | 15 ++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/cf/data/data.py b/cf/data/data.py index c93d48f7d4..0a9f072fb6 100644 --- a/cf/data/data.py +++ b/cf/data/data.py @@ -11206,6 +11206,7 @@ def HDF_chunks(self, *chunks): removed_at="5.0.0", ) + @daskified(_DASKIFIED_VERBOSE) def inspect(self): """Inspect the object for debugging. @@ -11215,8 +11216,16 @@ def inspect(self): `None` + **Examples** + + >>> d = cf.Data([9], 'm') + >>> d.inspect() + + ------------------- + _components: {'custom': {'_Units': , '_hardmask': True, '_cyclic': set(), '_axes': ('dim0',), 'dask': dask.array}, 'netcdf': {}} + """ - print(cf_inspect(self)) # pragma: no cover + cf_inspect(self) def isclose(self, y, rtol=None, atol=None): """Return where data are element-wise equal to other, diff --git a/cf/functions.py b/cf/functions.py index 070f30f84e..b9dcfba296 100644 --- a/cf/functions.py +++ b/cf/functions.py @@ -2697,14 +2697,19 @@ def inspect(self): `None` """ - name = repr(self) - out = [name, "".ljust(len(name), "-")] + try: + name = repr(self) + except Exception: + name = " " if hasattr(self, "__dict__"): - for key, value in sorted(self.__dict__.items()): - out.append(f"{key}: {value!r}") + out = [ + f"{key}: {value!r}" for key, value in sorted(self.__dict__.items()) + ] + else: + out = [] - print("\n".join(out)) + print("\n".join([name, "".ljust(len(name), "-")] + out)) def broadcast_array(array, shape): From c2d0f15651a692b2b9f3fe6a53b568fcac061dc6 Mon Sep 17 00:00:00 2001 From: David Hassell Date: Thu, 28 Apr 2022 08:24:51 +0100 Subject: [PATCH 2/3] Data.inspect io --- cf/test/test_Data.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cf/test/test_Data.py b/cf/test/test_Data.py index 668c8aabf5..74015482a7 100644 --- a/cf/test/test_Data.py +++ b/cf/test/test_Data.py @@ -2,6 +2,7 @@ import datetime import faulthandler import inspect +import io import itertools import os import unittest @@ -3848,11 +3849,11 @@ def test_Data_override_calendar(self): def test_Data_inspect(self): d = cf.Data([9], "m") - with open(os.devnull, "w") as devnull: - with contextlib.redirect_stdout(devnull): - self.assertIsNone(d.inspect()) - + f = io.StringIO() + with contextlib.redirect_stdout(f): + self.assertIsNone(d.inspect()) + if __name__ == "__main__": print("Run date:", datetime.datetime.now()) cf.environment() From c2181a0bdc26101659cbce1956d59e04e9f5fd59 Mon Sep 17 00:00:00 2001 From: David Hassell Date: Fri, 29 Apr 2022 08:57:22 +0100 Subject: [PATCH 3/3] _get_dask -> to_dask_array --- cf/data/data.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cf/data/data.py b/cf/data/data.py index 51bc68d7d3..197a83cbfa 100644 --- a/cf/data/data.py +++ b/cf/data/data.py @@ -845,7 +845,7 @@ def __bool__(self): "elements is ambiguous. Use d.any() or d.all()" ) - return bool(self._get_dask()) + return bool(self.to_dask_array()) def __repr__(self): """Called by the `repr` built-in function. @@ -5665,7 +5665,7 @@ def all(self, axis=None, keepdims=True, split_every=None): """ d = self.copy(array=False) - dx = self._get_dask() + dx = self.to_dask_array() dx = da.all(dx, axis=axis, keepdims=keepdims, split_every=split_every) d._set_dask(dx, reset_mask_hardness=False) d.hardmask = _DEFAULT_HARDMASK @@ -5782,7 +5782,7 @@ def any(self, axis=None, keepdims=True, split_every=None): """ d = self.copy(array=False) - dx = self._get_dask() + dx = self.to_dask_array() dx = da.any(dx, axis=axis, keepdims=keepdims, split_every=split_every) d._set_dask(dx, reset_mask_hardness=False) d.hardmask = _DEFAULT_HARDMASK