Skip to content

Commit

Permalink
Merge pull request #394 from davidhassell/dask-inspect
Browse files Browse the repository at this point in the history
dask: `Data.inspect`
  • Loading branch information
davidhassell authored May 9, 2022
2 parents 01e533d + c2181a0 commit 70043ac
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
16 changes: 15 additions & 1 deletion cf/data/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9515,6 +9515,7 @@ def flip(self, axes=None, inplace=False, i=False):

return d

@daskified(_DASKIFIED_VERBOSE)
def inspect(self):
"""Inspect the object for debugging.
Expand All @@ -9524,10 +9525,23 @@ def inspect(self):
`None`
**Examples**
>>> d = cf.Data([9], 'm')
>>> d.inspect()
<CF Data(1): [9] m>
-------------------
{'_components': {'custom': {'_Units': <Units: m>,
'_axes': ('dim0',),
'_cyclic': set(),
'_hardmask': True,
'dask': dask.array<cf_harden_mask, shape=(1,), dtype=int64, chunksize=(1,), chunktype=numpy.ndarray>},
'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,
Expand Down
15 changes: 9 additions & 6 deletions cf/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2686,14 +2686,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):
Expand Down
13 changes: 11 additions & 2 deletions cf/test/test_Data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import contextlib
import datetime
import faulthandler
import inspect
import io
import itertools
import os
import unittest
Expand Down Expand Up @@ -3928,8 +3930,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()
Expand Down

0 comments on commit 70043ac

Please sign in to comment.