-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy path__init__.py
More file actions
72 lines (56 loc) · 2.18 KB
/
__init__.py
File metadata and controls
72 lines (56 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import importlib
import re
from contextlib import contextmanager
import dask
import pytest
from packaging import version
@contextmanager
def raises_regex(error, pattern):
__tracebackhide__ = True
with pytest.raises(error) as excinfo:
yield
message = str(excinfo.value)
if not re.search(pattern, message):
raise AssertionError(
f"exception {excinfo.value!r} did not match pattern {pattern!r}"
)
class CountingScheduler:
"""Simple dask scheduler counting the number of computes.
Reference: https://stackoverflow.com/questions/53289286/"""
def __init__(self, max_computes=0):
self.total_computes = 0
self.max_computes = max_computes
def __call__(self, dsk, keys, **kwargs):
self.total_computes += 1
if self.total_computes > self.max_computes:
raise RuntimeError(
f"Too many computes. Total:{self.total_computes} > max: {self.max_computes}."
)
return dask.get(dsk, keys, **kwargs)
def raise_if_dask_computes(max_computes=0):
scheduler = CountingScheduler(max_computes)
return dask.config.set(scheduler=scheduler)
def _importorskip(modname, minversion=None):
try:
mod = importlib.import_module(modname)
has = True
if minversion is not None:
if LooseVersion(mod.__version__) < LooseVersion(minversion):
raise ImportError("Minimum version not satisfied")
except ImportError:
has = False
func = pytest.mark.skipif(not has, reason=f"requires {modname}")
return has, func
def LooseVersion(vstring):
# Our development version is something like '0.10.9+aac7bfc'
# This function just ignored the git commit id.
vstring = vstring.split("+")[0]
return version.parse(vstring)
has_cftime, requires_cftime = _importorskip("cftime")
has_scipy, requires_scipy = _importorskip("scipy")
has_shapely, requires_shapely = _importorskip("shapely")
has_pint, requires_pint = _importorskip("pint")
has_pooch, requires_pooch = _importorskip("pooch")
_, requires_rich = _importorskip("rich")
has_regex, requires_regex = _importorskip("regex")
has_cartopy, requires_cartopy = _importorskip("cartopy")