Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dask: Data.__float__ and Data.__int__ #337

Merged
merged 1 commit into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 15 additions & 12 deletions cf/data/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions cf/test/test_Data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down