From 3ea65654e36c88e83266ee8e79f1995929143135 Mon Sep 17 00:00:00 2001 From: David Hassell Date: Tue, 1 Mar 2022 15:31:20 +0000 Subject: [PATCH 1/2] error messages, tests --- cf/data/data.py | 14 ++++++-------- cf/test/test_Data.py | 21 +++++++++++++++------ 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/cf/data/data.py b/cf/data/data.py index 068b309d90..a6b3393e3e 100644 --- a/cf/data/data.py +++ b/cf/data/data.py @@ -10558,15 +10558,13 @@ def datum(self, *index): index = tuple(index) else: raise ValueError( - "Incorrect number of indices for {} array".format( - self.__class__.__name__ - ) + f"Incorrect number of indices ({n_index}) for " + f"{self.ndim}-d {self.__class__.__name__} data" ) elif n_index != self.ndim: raise ValueError( - "Incorrect number of indices for {} array".format( - self.__class__.__name__ - ) + f"Incorrect number of indices ({n_index}) for " + f"{self.ndim}-d {self.__class__.__name__} data" ) array = self[index].array @@ -10576,8 +10574,8 @@ def datum(self, *index): else: raise ValueError( - "Can only convert a {} array of size 1 to a " - "Python scalar".format(self.__class__.__name__) + f"For size {self.size} data, must provide an index of " + "the element to be converted to a Python scalar" ) if not np.ma.isMA(array): diff --git a/cf/test/test_Data.py b/cf/test/test_Data.py index 53eed8b5e6..2224b1f411 100644 --- a/cf/test/test_Data.py +++ b/cf/test/test_Data.py @@ -1771,12 +1771,7 @@ def test_Data_datum(self): self.assertEqual(d.datum(-1), 3) for index in d.ndindex(): self.assertEqual(d.datum(index), d.array[index].item()) - self.assertEqual( - d.datum(*index), - d.array[index].item(), - "{}, {}".format(d.datum(*index), d.array[index].item()), - ) - # --- End: for + self.assertEqual(d.datum(*index), d.array[index].item()) d = cf.Data(5, "metre") d[()] = cf.masked @@ -1795,6 +1790,20 @@ def test_Data_datum(self): self.assertIs(d.datum([0, -1]), cf.masked) self.assertIs(d.datum(-1, -1), cf.masked) + d = cf.Data([1, 2]) + with self.assertRaises(ValueError): + d.datum() + + with self.assertRaises(ValueError): + d.datum(3) + + with self.assertRaises(ValueError): + d.datum(0, 0) + + d = cf.Data([[1, 2]]) + with self.assertRaises(ValueError): + d.datum((0,)) + def test_Data_flip(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: return From adcec58fddd34a3b623cd982ec873101ad93aaa5 Mon Sep 17 00:00:00 2001 From: David Hassell Date: Tue, 1 Mar 2022 15:33:23 +0000 Subject: [PATCH 2/2] daskified --- cf/data/data.py | 1 + 1 file changed, 1 insertion(+) diff --git a/cf/data/data.py b/cf/data/data.py index a6b3393e3e..3db1024372 100644 --- a/cf/data/data.py +++ b/cf/data/data.py @@ -10443,6 +10443,7 @@ def in_memory(self): return True + @daskified(_DASKIFIED_VERBOSE) def datum(self, *index): """Return an element of the data array as a standard Python scalar.