Debian Codegraph @

geotiepoints/tests/utils.py

raw
1 """Utilities for creating and checking tests."""
2
3
4 class CustomScheduler(object):
5 """Scheduler raising an exception if data are computed too many times.
6
7 This only makes sense to include when working with dask-based arrays. To
8 use it::
9
10 with dask.config.set(scheduler=CustomScheduler(2)):
11 my_dask_arr.compute() # allowed
12 my_dask_arr.compute() # 2nd call, not allowed, fails
13
14 """
15
16 def __init__(self, max_computes=1):
17 """Set starting and maximum compute counts."""
18 self.max_computes = max_computes
19 self.total_computes = 0
20
21 def __call__(self, dsk, keys, **kwargs):
22 """Compute dask task and keep track of number of times we do so."""
23 import dask
24 self.total_computes += 1
25 if self.total_computes > self.max_computes:
26 raise RuntimeError("Too many dask computations were scheduled: "
27 "{}".format(self.total_computes))
28 return dask.get(dsk, keys, **kwargs)