Mapping
symconstraints.mapping
Basic implementations of operations to the standard python mapping (dict, defaultdict, etc.).
StringMap = Mapping[str, Any]
module-attribute
Any generic map that has a string key, such as dict, defaultdict, Counter, etc.
ValidationError
Bases: Exception
Validation error.
Attributes:
Name | Type | Description |
---|---|---|
values |
StringMap
|
Mapping values relevant to the validation operations. |
unsatisfied_booleans |
list[Boolean]
|
List of equalities/inequalities where these values do not satisfy. |
Source code in src/symconstraints/mapping.py
ConstraintsValidationError
Bases: Exception
Constraints validation error.
Attributes:
Name | Type | Description |
---|---|---|
validation_errors |
list[ValidationError]
|
List of errors for each validations in the constraints. |
Source code in src/symconstraints/mapping.py
validate(constraints, mapping)
Validate mapping via a validation or constraints.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
constraints |
Constraints | Validation
|
Constraints or validation to use for validation. |
required |
mapping |
AnyValueMap
|
Input to validate |
required |
Raises:
Type | Description |
---|---|
ValidationError
|
Raised when the mapping data given is invalid under constraints of type |
ConstraintsValidationError
|
Raised when the mapping data given is invalid under constraints of type |
Examples:
>>> from symconstraints import Constraints, symbols
>>> from symconstraints.mapping import validate
>>> a, b, c = symbols('a b c')
>>> constraints = Constraints([a < b, b < c])
>>> validate(constraints, {'a': 1, 'b': 2})
>>> # Nothing happens, data is valid
>>> try:
... validate(constraints, {'a': 4, 'c': 1})
... except ConstraintsValidationError as e:
... print(e)
Mapping is invalid due to:
- Mapping {'a': 4, 'c': 1} is invalid due to not satisfying [a < c]
Source code in src/symconstraints/mapping.py
impute(constraints, mapping)
Impute mapping via a validation or constraints object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
constraints |
Imputation | Constraints
|
Constraints or Imputation object to use for imputation. |
required |
mapping |
StringMap
|
Input to impute. |
required |
Returns:
Type | Description |
---|---|
dict[str, Any]
|
The imputed mapping as a dictionary. |
Examples:
>>> from sympy import Eq
>>> from symconstraints import symbols, Constraints
>>> from symconstraints.mapping import impute
>>> a, b, c, d = symbols("a b c d")
>>> constraints = Constraints([Eq(a, 2 * b + c), c < b, Eq(d, a * c)])
>>> impute(constraints, {"b": 10, "c": 3})
{'a': 23, 'b': 10, 'c': 3, 'd': 69}