diff --git a/cf/data/data.py b/cf/data/data.py index 40092e776a..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 @@ -9544,6 +9544,7 @@ def flip(self, axes=None, inplace=False, i=False): return d + @daskified(_DASKIFIED_VERBOSE) def inspect(self): """Inspect the object for debugging. @@ -9553,10 +9554,23 @@ def inspect(self): `None` + **Examples** + + >>> d = cf.Data([9], 'm') + >>> d.inspect() + + ------------------- + {'_components': {'custom': {'_Units': , + '_axes': ('dim0',), + '_cyclic': set(), + '_hardmask': True, + 'dask': dask.array}, + 'netcdf': {}}} + """ from ..functions import inspect - print(inspect(self)) # pragma: no cover + 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 a495a0c45a..c31e4760fa 100644 --- a/cf/functions.py +++ b/cf/functions.py @@ -2688,14 +2688,17 @@ def inspect(self): `None` """ - name = repr(self) - out = [name, "".ljust(len(name), "-")] + from pprint import pprint - if hasattr(self, "__dict__"): - for key, value in sorted(self.__dict__.items()): - out.append(f"{key}: {value!r}") + try: + name = repr(self) + except Exception: + name = self.__class__.__name__ - print("\n".join(out)) + print("\n".join([name, "".ljust(len(name), "-")])) + + if hasattr(self, "__dict__"): + pprint(self.__dict__) def broadcast_array(array, shape): diff --git a/cf/test/test_Data.py b/cf/test/test_Data.py index 82141080ec..5853a63381 100644 --- a/cf/test/test_Data.py +++ b/cf/test/test_Data.py @@ -1,6 +1,8 @@ +import contextlib import datetime import faulthandler import inspect +import io import itertools import os import unittest @@ -3909,8 +3911,15 @@ def test_Data_rtol(self): self.assertEqual(d._rtol, cf.rtol()) cf.rtol(0.001) self.assertEqual(d._rtol, 0.001) - - + + def test_Data_inspect(self): + d = cf.Data([9], "m") + + f = io.StringIO() + with contextlib.redirect_stdout(f): + self.assertIsNone(d.inspect()) + + if __name__ == "__main__": print("Run date:", datetime.datetime.now()) cf.environment()