Skip to content

Commit e26acda

Browse files
author
Joseph Hamman
committed
docstrings
1 parent b6dec98 commit e26acda

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

tests/test_core.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ def test_dataarray_validate_dims():
7070
schema.validate(da)
7171

7272

73+
def test_dataarray_validate_array_type():
74+
75+
da = xr.DataArray(np.ones(4), dims=['x'])
76+
schema = DataArraySchema(array_type=np.ndarray)
77+
schema.validate(da)
78+
79+
schema = DataArraySchema(array_type=float)
80+
with pytest.raises(SchemaError, match=r'.*(float).*'):
81+
schema.validate(da)
82+
83+
7384
def test_dataset_empty_constructor():
7485
ds_schema = DatasetSchema()
7586
assert hasattr(ds_schema, 'validate')

xarray_schema/core.py

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,29 @@
88

99

1010
class SchemaError(Exception):
11+
'''Custom Schema Error'''
12+
1113
pass
1214

1315

1416
class DataArraySchema:
17+
'''A light-weight xarray.DataArray validator
18+
19+
Parameters
20+
----------
21+
dtype : Any, optional
22+
Datatype of the the variable. If a string is specified it must be a valid NumPy data type value, by default None
23+
shape : Tuple[Union[int, None]], optional
24+
Shape of the DataArray. `None` may be used as a wildcard value. By default None
25+
dims : Tuple[Union[Hashable, None]], optional
26+
Dimensions of the DataArray. `None` may be used as a wildcard value. By default None
27+
name : str, optional
28+
Name of the DataArray, by default None
29+
array_type : Any, optional
30+
Type of the underlying data in a DataArray (e.g. `numpy.ndarray`), by default None
31+
checks : Iterable[Callable], optional
32+
List of callables that take and return a DataArray, by default None'''
33+
1534
def __init__(
1635
self,
1736
dtype: Any = None,
@@ -20,9 +39,9 @@ def __init__(
2039
coords: Dict[Hashable, Any] = None,
2140
chunks: Dict[Hashable, Union[int, None]] = None,
2241
name: str = None,
23-
checks: Iterable[Callable] = None,
2442
array_type: Any = None,
2543
attrs: Dict[Hashable, Any] = None,
44+
checks: Iterable[Callable] = None,
2645
) -> None:
2746

2847
self.dtype = dtype
@@ -37,6 +56,22 @@ def __init__(
3756
self.checks = checks if checks is not None else []
3857

3958
def validate(self, da: xr.DataArray) -> xr.DataArray:
59+
'''Check if the DataArray complies with the Schema.
60+
61+
Parameters
62+
----------
63+
da : xr.DataArray
64+
DataArray to be validated
65+
66+
Returns
67+
-------
68+
xr.DataArray
69+
Validated DataArray
70+
71+
Raises
72+
------
73+
SchemaError
74+
'''
4075

4176
if self.dtype is not None and not np.issubdtype(da.dtype, self.dtype):
4277
raise SchemaError(f'dtype {da.dtype} != {self.dtype}')
@@ -84,6 +119,16 @@ def validate(self, da: xr.DataArray) -> xr.DataArray:
84119

85120

86121
class DatasetSchema:
122+
'''A light-weight xarray.Dataset validator
123+
124+
Parameters
125+
----------
126+
data_vars : mapping of variable names and DataArraySchemas, optional
127+
Per-variable DataArraySchema's, by default None
128+
checks : Iterable[Callable], optional
129+
Dataset wide checks, by default None
130+
'''
131+
87132
def __init__(
88133
self,
89134
data_vars: Dict[Hashable, Union[DataArraySchema, None]] = None,
@@ -98,6 +143,22 @@ def __init__(
98143
self.checks = checks
99144

100145
def validate(self, ds: xr.Dataset) -> xr.Dataset:
146+
'''Check if the Dataset complies with the Schema.
147+
148+
Parameters
149+
----------
150+
ds : xr.Dataset
151+
Dataset to be validated
152+
153+
Returns
154+
-------
155+
xr.Dataset
156+
Validated Dataset
157+
158+
Raises
159+
------
160+
SchemaError
161+
'''
101162

102163
if self.data_vars is not None:
103164
for key, da_schema in self.data_vars.items():

0 commit comments

Comments
 (0)