dispel.data.validators module#

A module containing value validators to ensure conformity of values.

The following validators are implemented:

As some validators are very common, hence there are constants for convenience:

dispel.data.validators.BETWEEN_MINUS_ONE_AND_ONE = <RangeValidator: [-1, 1]>#

A validator that ensures values are between minus one and one

Parameters:

value (Any) –

dispel.data.validators.BETWEEN_ZERO_AND_ONE = <RangeValidator: [0, 1]>#

A validator that ensures values are between zero and one

Parameters:

value (Any) –

dispel.data.validators.GREATER_THAN_ZERO = <RangeValidator: [0, ∞]>#

A validator that ensures values are greater or equal than zero

Parameters:

value (Any) –

class dispel.data.validators.RangeValidator[source]#

Bases: object

A range validator.

The range validator can be used to ensure a measure value is within a given range. It is specified via the validator argument when creating ValueDefinitions.

Examples

To create a range validator that allows all values between -4 and 7 one can use it the following way:

validator

>>> validator = RangeValidator(-4, 7)
>>> validator
<RangeValidator: [-4, 7]>
>>> validator(5)

When called the range validator will raise an except only if the value is outside its range:

validator

>>> validator(10)
Traceback (most recent call last):
 ...
dispel.data.validators.ValidationException: Value violated upper bound
[-4, 7]: 10

The range validator can also be used with just one side by specifying only the lower or upper bound:

validator

>>> RangeValidator(lower_bound=-4)
<RangeValidator: [-4, ∞]>
>>> RangeValidator(upper_bound=7)
<RangeValidator: [-∞, 7]>

To exclude the boundaries in the range, one can set include_lower or include_upper to False:

validator

>>> validator = RangeValidator(lower_bound=0, include_lower=False)
>>> validator
<RangeValidator: (0, ∞]>
>>> validator(0)
Traceback (most recent call last):
 ...
dispel.data.validators.ValidationException: Value violated lower bound
(0, ∞]: 0
lower_bound#

The lower bound of the range validator.

upper_bound#

The upper bound of the range validator.

include_lower#

Include the lower boundary in the range check.

include_upper#

Include the upper boundary in the range check.

__init__(lower_bound=None, upper_bound=None, include_lower=True, include_upper=True)[source]#
Parameters:
  • lower_bound (float | None) –

  • upper_bound (float | None) –

  • include_lower (bool) –

  • include_upper (bool) –

repr_boundaries()[source]#

Get a representation of the boundaries.

class dispel.data.validators.SetValidator[source]#

Bases: object

A set validator.

The set validator can be used to ensure that a value is within a particular set of values. It is specified via the validator argument when creating ValueDefinitions.

Examples

The most common application of the SetValidator is to validate survey responses and to provide additional labels for numerical responses. Assuming you have a survey that has possible responses ranging from 1 to 4 and you want to provide the respective labels to those responses you can achieve this in the following way:

validators

>>> validator = SetValidator({
...    1: 'Not at all',
...    2: 'A little',
...    3: 'Moderately',
...    4: 'Extremely'
... })
>>> validator
<SetValidator: {1: 'Not at all', 2: 'A little', 3: 'Moderately', ...}>
>>> validator(1)

When calling the validator with a value not being part of the allowed_values the validator will raise an exception:

validator

>>> validator(0)
Traceback (most recent call last):
  ...
dispel.data.validators.ValidationException: Value must be one of: {1, ...}

If there are no labels for values one can simply pass a list of unique values to the validator:

validator

>>> validator = SetValidator([1, 2, 3, 4])
>>> validator
<SetValidator: {1, 2, 3, 4}>
allowed_values#

The allowed values by the validator. Any value within this set will pass the validator.

labels#

The labels for the allowed values. To get a label for a specific value consider using get_label(). Labels for values are specified by providing a dictionary with allowed values as keys and labels as values, e.g.

>>> from dispel.data.validators import SetValidator
>>> validator = SetValidator({1: 'label for 1', 2: 'label for two'})
__init__(values)[source]#
Parameters:

values (List[Any] | Dict[Any, str]) –

get_label(value)[source]#

Get the label for the specified value.

Parameters:

value (Any) – The value for which to get the label for

Returns:

The label for the specified value.

Return type:

str

Raises:

KeyError – If the value is not part of the allowed values.

exception dispel.data.validators.ValidationException[source]#

Bases: Exception

An exception risen if a value didn’t match the validator’s expectations.

Parameters:
  • message – Explanation of the error

  • value – The observed value

  • validator – The validator that rose the exception

__init__(message, value, validator)[source]#