From 6ad589cbf6dca2ede17b324aafe6d76bc5e7ace1 Mon Sep 17 00:00:00 2001 From: David Hassell Date: Fri, 4 Mar 2022 10:54:30 +0000 Subject: [PATCH] int and float --- cf/data/data.py | 27 +++++++++++++++------------ cf/test/test_Data.py | 4 ++-- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/cf/data/data.py b/cf/data/data.py index 31cb3566dd..535bcd7011 100644 --- a/cf/data/data.py +++ b/cf/data/data.py @@ -837,18 +837,20 @@ def __hash__(self): """ return hash_array(self.array) + @daskified(_DASKIFIED_VERBOSE) def __float__(self): """Called to implement the built-in function `float` x.__float__() <==> float(x) - """ - if self.size != 1: - raise TypeError( - "only length-1 arrays can be converted to Python scalars" - ) + **Performance** - return float(self.datum()) + `__float__` causes all delayed operations to be executed, + unless the dask array size is already known to be greater than + 1. + + """ + return float(self._get_dask()) def __round__(self, *ndigits): """Called to implement the built-in function `round` @@ -863,18 +865,19 @@ def __round__(self, *ndigits): return round(self.datum(), *ndigits) + @daskified(_DASKIFIED_VERBOSE) def __int__(self): """Called to implement the built-in function `int` x.__int__() <==> int(x) - """ - if self.size != 1: - raise TypeError( - "only length-1 arrays can be converted to Python scalars" - ) + **Performance** - return int(self.datum()) + `__int__` causes all delayed operations to be executed, unless + the dask array size is already known to be greater than 1. + + """ + return int(self._get_dask()) def __iter__(self): """Called when an iterator is required. diff --git a/cf/test/test_Data.py b/cf/test/test_Data.py index 1707e1cb87..c693d7ca55 100644 --- a/cf/test/test_Data.py +++ b/cf/test/test_Data.py @@ -2417,8 +2417,8 @@ def test_Data__float__(self): self.assertEqual(float(cf.Data(x)), float(x)) self.assertEqual(float(cf.Data(x)), float(x)) - with self.assertRaises(Exception): - _ = float(cf.Data([1, 2])) + with self.assertRaises(TypeError): + float(cf.Data([1, 2])) def test_Data__int__(self): if self.test_only and inspect.stack()[0][3] not in self.test_only: