valarray 0.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- valarray/__init__.py +6 -0
- valarray/core/__init__.py +27 -0
- valarray/core/array.py +121 -0
- valarray/core/array_type_adapter.py +109 -0
- valarray/core/axes_and_fields.py +127 -0
- valarray/core/comparisons.py +71 -0
- valarray/core/errors_exceptions/__init__.py +41 -0
- valarray/core/errors_exceptions/error_list.py +87 -0
- valarray/core/errors_exceptions/exceptions.py +75 -0
- valarray/core/errors_exceptions/generic.py +57 -0
- valarray/core/errors_exceptions/validation_errors/__init__.py +0 -0
- valarray/core/errors_exceptions/validation_errors/array_creation.py +14 -0
- valarray/core/errors_exceptions/validation_errors/axes.py +68 -0
- valarray/core/errors_exceptions/validation_errors/base.py +26 -0
- valarray/core/errors_exceptions/validation_errors/dtype.py +60 -0
- valarray/core/errors_exceptions/validation_errors/values.py +99 -0
- valarray/core/types/__init__.py +24 -0
- valarray/core/types/generics.py +40 -0
- valarray/core/types/other.py +23 -0
- valarray/core/utils.py +89 -0
- valarray/core/validation_functions/__init__.py +20 -0
- valarray/core/validation_functions/array.py +62 -0
- valarray/core/validation_functions/array_values.py +47 -0
- valarray/core/validation_functions/dtype.py +44 -0
- valarray/core/validation_functions/field_values/__init__.py +0 -0
- valarray/core/validation_functions/field_values/core.py +106 -0
- valarray/core/validation_functions/field_values/types_and_data_structures.py +90 -0
- valarray/core/validation_functions/field_values/utils.py +143 -0
- valarray/core/validation_functions/shape.py +67 -0
- valarray/core/validation_functions/utils.py +24 -0
- valarray/core/validators/__init__.py +13 -0
- valarray/core/validators/base.py +72 -0
- valarray/core/validators/value_comparisons.py +107 -0
- valarray/numpy/__init__.py +24 -0
- valarray/numpy/array.py +63 -0
- valarray/numpy/array_type_adapter.py +133 -0
- valarray/numpy/axes_and_fields.py +83 -0
- valarray/numpy/comparisons.py +135 -0
- valarray/numpy/errors_exceptions.py +172 -0
- valarray/numpy/types.py +32 -0
- valarray/numpy/validation_functions.py +170 -0
- valarray/numpy/validators.py +35 -0
- valarray-0.4.dist-info/METADATA +698 -0
- valarray-0.4.dist-info/RECORD +45 -0
- valarray-0.4.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
"""
|
|
2
|
+
*import* `validate_array` **function**
|
|
3
|
+
|
|
4
|
+
*import* `validate_dtype` **function**
|
|
5
|
+
|
|
6
|
+
*import* `validate_shape` **function**
|
|
7
|
+
|
|
8
|
+
*import* `validate_array_values` **function**
|
|
9
|
+
|
|
10
|
+
*import* `validate_field_values` **function**
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from types import EllipsisType
|
|
14
|
+
from typing import Any, Literal, Sequence, overload
|
|
15
|
+
|
|
16
|
+
import numpy as np
|
|
17
|
+
import numpy.typing as npt
|
|
18
|
+
|
|
19
|
+
from valarray.core.errors_exceptions import (
|
|
20
|
+
IncorrectAxNumberError,
|
|
21
|
+
IncorrectAxSizesError,
|
|
22
|
+
IncorrectDTypeError,
|
|
23
|
+
InvalidArrayValuesError,
|
|
24
|
+
InvalidFieldValuesError,
|
|
25
|
+
ValidationError,
|
|
26
|
+
ValidationErrorList,
|
|
27
|
+
)
|
|
28
|
+
from valarray.core.validation_functions import validate_array as core_validate_array
|
|
29
|
+
from valarray.core.validation_functions import (
|
|
30
|
+
validate_array_values as core_validate_array_values,
|
|
31
|
+
)
|
|
32
|
+
from valarray.core.validation_functions import validate_dtype as core_validate_dtype
|
|
33
|
+
from valarray.core.validation_functions import (
|
|
34
|
+
validate_field_values as core_validate_field_values,
|
|
35
|
+
)
|
|
36
|
+
from valarray.core.validation_functions import validate_shape as core_validate_shape
|
|
37
|
+
from valarray.core.validators import Validator
|
|
38
|
+
|
|
39
|
+
from .array_type_adapter import NumpyArrayTypeAdapter
|
|
40
|
+
from .axes_and_fields import AxesTuple
|
|
41
|
+
from .types import AdvancedIndex, NumpyDTypeT
|
|
42
|
+
from .validators import NumpyValidator
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def validate_dtype(
|
|
46
|
+
arr: npt.NDArray[Any], dtypelike: npt.DTypeLike | EllipsisType
|
|
47
|
+
) -> ValidationErrorList[IncorrectDTypeError[npt.DTypeLike, np.dtype]]:
|
|
48
|
+
"""Validate that numpy array has the correct dtype.
|
|
49
|
+
|
|
50
|
+
Args:
|
|
51
|
+
arr (npt.NDArray[Any]): Array to be validated.
|
|
52
|
+
dtypelike (npt.DTypeLike | EllipsisType): Expected datatype
|
|
53
|
+
(if `...` , do not validate).
|
|
54
|
+
|
|
55
|
+
Returns:
|
|
56
|
+
errs (ValidationErrorList[IncorrectDTypeError[npt.DTypeLike, np.dtype]]):
|
|
57
|
+
list containing a single dtype error or an empty list.
|
|
58
|
+
|
|
59
|
+
(introduced ***v0.1***, last_modified ***v0.4***)
|
|
60
|
+
"""
|
|
61
|
+
return core_validate_dtype(arr, dtypelike, NumpyArrayTypeAdapter())
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
validate_shape = core_validate_shape
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def validate_array_values(
|
|
68
|
+
arr: npt.NDArray[NumpyDTypeT],
|
|
69
|
+
validators: Sequence[NumpyValidator[NumpyDTypeT]],
|
|
70
|
+
) -> ValidationErrorList[
|
|
71
|
+
InvalidArrayValuesError[npt.NDArray[NumpyDTypeT], AdvancedIndex]
|
|
72
|
+
]:
|
|
73
|
+
"""Validate numpy array with a sequence of validators applied to the whole array
|
|
74
|
+
and return potential errors.
|
|
75
|
+
|
|
76
|
+
Args:
|
|
77
|
+
arr (npt.NDArray[NumpyDTypeT]: Array to be validated.
|
|
78
|
+
validators (Sequence[NumpyValidator[NumpyDTypeT]]: Validators to apply.
|
|
79
|
+
|
|
80
|
+
Returns:
|
|
81
|
+
errs (ValidationErrorList[InvalidArrayValuesError[npt.NDArray[NumpyDTypeT], AdvancedIndex]]):
|
|
82
|
+
List containing at most 1 error per validator.
|
|
83
|
+
|
|
84
|
+
(introduced ***v0.3***, last_modified ***v0.4***)
|
|
85
|
+
"""
|
|
86
|
+
return core_validate_array_values(arr, validators, NumpyArrayTypeAdapter())
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
@overload
|
|
90
|
+
def validate_field_values(
|
|
91
|
+
arr: npt.NDArray[NumpyDTypeT],
|
|
92
|
+
schema: AxesTuple[NumpyDTypeT] | EllipsisType,
|
|
93
|
+
check_shape: Literal[False] = False,
|
|
94
|
+
) -> ValidationErrorList[
|
|
95
|
+
InvalidFieldValuesError[npt.NDArray[NumpyDTypeT], AdvancedIndex]
|
|
96
|
+
]: ...
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
@overload
|
|
100
|
+
def validate_field_values(
|
|
101
|
+
arr: npt.NDArray[NumpyDTypeT],
|
|
102
|
+
schema: AxesTuple[NumpyDTypeT] | EllipsisType,
|
|
103
|
+
check_shape: Literal[True] = True,
|
|
104
|
+
) -> (
|
|
105
|
+
ValidationErrorList[
|
|
106
|
+
InvalidFieldValuesError[npt.NDArray[NumpyDTypeT], AdvancedIndex]
|
|
107
|
+
]
|
|
108
|
+
| ValidationErrorList[IncorrectAxNumberError | IncorrectAxSizesError]
|
|
109
|
+
): ...
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def validate_field_values(
|
|
113
|
+
arr: npt.NDArray[NumpyDTypeT],
|
|
114
|
+
schema: AxesTuple[NumpyDTypeT] | EllipsisType,
|
|
115
|
+
check_shape: bool = False,
|
|
116
|
+
) -> (
|
|
117
|
+
ValidationErrorList[
|
|
118
|
+
InvalidFieldValuesError[npt.NDArray[NumpyDTypeT], AdvancedIndex]
|
|
119
|
+
]
|
|
120
|
+
| ValidationErrorList[IncorrectAxNumberError | IncorrectAxSizesError]
|
|
121
|
+
):
|
|
122
|
+
"""Validate numpy array fields with validators defined in schema and return potential errors.
|
|
123
|
+
|
|
124
|
+
Args:
|
|
125
|
+
arr (npt.NDArray[NumpyDTypeT]): Array to be validated.
|
|
126
|
+
schema (AxesTuple | EllipsisType):
|
|
127
|
+
Array schema (if `...` , do not validate).
|
|
128
|
+
check_shape (bool, optional): If `True`, check shape with `validate_shape()` first.
|
|
129
|
+
Defaults to False.
|
|
130
|
+
|
|
131
|
+
Returns:
|
|
132
|
+
errs (ValidationErrorList[InvalidFieldValuesError[npt.NDArray[NumpyDTypeT], AdvancedIndex]]):
|
|
133
|
+
List containing at most 1 error per validator
|
|
134
|
+
(including built-in field value comparisons like `gt`, `lt` etc.).
|
|
135
|
+
|
|
136
|
+
(introduced ***v0.4***)
|
|
137
|
+
"""
|
|
138
|
+
return core_validate_field_values(arr, schema, NumpyArrayTypeAdapter(), check_shape)
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def validate_array(
|
|
142
|
+
arr: npt.NDArray[NumpyDTypeT],
|
|
143
|
+
dtypelike: npt.DTypeLike | EllipsisType,
|
|
144
|
+
schema: AxesTuple[NumpyDTypeT] | EllipsisType,
|
|
145
|
+
validators: Sequence[Validator[npt.NDArray[NumpyDTypeT], AdvancedIndex]],
|
|
146
|
+
) -> ValidationErrorList[ValidationError]:
|
|
147
|
+
"""Validate array:
|
|
148
|
+
- data type
|
|
149
|
+
- shape (based on array schema)
|
|
150
|
+
- array values (using a sequence of validators)
|
|
151
|
+
- field values (based on validators defined in array schema)
|
|
152
|
+
|
|
153
|
+
First validate data type and shape. If those are correct, validate field and array values.
|
|
154
|
+
|
|
155
|
+
Args:
|
|
156
|
+
arr (npt.NDArray[NumpyDTypeT]): Array to be validated.
|
|
157
|
+
dtypelike (npt.DTypeLike | EllipsisType,): Expected datatype (if `...` , do not validate).
|
|
158
|
+
schema (AxesTuple[NumpyDTypeT] | EllipsisType):
|
|
159
|
+
Expected array schema (if `...` , do not validate).
|
|
160
|
+
validators (Sequence[Validator[npt.NDArray[NumpyDTypeT], AdvancedIndex]]):
|
|
161
|
+
Validators to apply.
|
|
162
|
+
|
|
163
|
+
Returns:
|
|
164
|
+
ValidationErrorList[ValidationError]: Validation errors.
|
|
165
|
+
|
|
166
|
+
(introduced ***v0.4***)
|
|
167
|
+
"""
|
|
168
|
+
return core_validate_array(
|
|
169
|
+
arr, dtypelike, schema, validators, NumpyArrayTypeAdapter()
|
|
170
|
+
)
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
from abc import abstractmethod
|
|
2
|
+
from typing import Generic, Optional
|
|
3
|
+
|
|
4
|
+
import numpy.typing as npt
|
|
5
|
+
|
|
6
|
+
from valarray.core.validators import ValidationResult, Validator
|
|
7
|
+
|
|
8
|
+
from .types import AdvancedIndex, NumpyDTypeT
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class NumpyValidator(
|
|
12
|
+
Validator[npt.NDArray[NumpyDTypeT], AdvancedIndex], Generic[NumpyDTypeT]
|
|
13
|
+
):
|
|
14
|
+
"""Base class for numpy array Validators.
|
|
15
|
+
|
|
16
|
+
Type variables:
|
|
17
|
+
- `NumpyDTypeT` - data type of array to be validated.
|
|
18
|
+
|
|
19
|
+
Subclasses need to implement:
|
|
20
|
+
**abstract method**
|
|
21
|
+
- `validate` -
|
|
22
|
+
Method to validate an array.
|
|
23
|
+
|
|
24
|
+
Subclasses can optionally implement:
|
|
25
|
+
**abstract method**
|
|
26
|
+
- `__str__` -
|
|
27
|
+
String representation of validator used in error messages.
|
|
28
|
+
If not implemented defalts to class name.
|
|
29
|
+
|
|
30
|
+
(introduced ***v0.3***)"""
|
|
31
|
+
|
|
32
|
+
@abstractmethod
|
|
33
|
+
def validate(
|
|
34
|
+
self, arr: npt.NDArray[NumpyDTypeT]
|
|
35
|
+
) -> Optional[bool | ValidationResult[AdvancedIndex]]: ...
|