ratingslib.utils.validation module

This module gathers utilities for input validation. All functions that starts with validate raises an error if the input is not valid.

exception ValidationError

Bases: Exception

Class to raise a message when there’s a validation error

validate_from_set(input_to_check: Any, set_of_values: Set[Any], var_name: str = '')

Check if target value is an element of set

Parameters
  • input_to_check (Any) – Target input.

  • set_of_values (Set[Any]) – Set data structure.

  • var_name (str, default="") – Variable name is printed in the Error message.

Raises

ValidationError – If the target value is not an element of target set data structure.

validate_type(variable: Any, var_type, var_name: str = '')

Checks that the target variable is an instance of var_type

Parameters
  • variable (Any,) – Target value.

  • var_type (A class, type or a tuple containing classes, types or other tuples) – Target type.

  • var_name (str, default="") – Variable name is printed in the Error message.

Raises

TypeError – If variable is not an instance of var_type.

validate_not_none_and_type(variable: Any, var_type, var_name: str = '')

Checks that the target variable is not None and it is an instance of var_type.

Parameters
  • variable (Any,) – Target value

  • var_type (A class, type or a tuple containing classes, types or other tuples) – Target type

  • var_name (str, default="") – Variable name is printed in the Error message.

Raises

TypeError – If variable is None.

validate_type_of_elements(elements: Union[List, Tuple, Set], class_name)

Check if the elements parameter contains objects of same type with the target class (parameter class_name ). The parameter elements must be one of the following data structures: List, Tuple or Set

Parameters
  • elements (Union[List, Tuple, Set]) – Target list or tuple or set

  • class_name (A class, type or a tuple containing classes, types or other tuples) – Target class

Raises

TypeError – If elements is not list or tuple or set. If the objects of elements do not have the same type.

is_number(x)

Return True if the given parameter is a number

Examples

>>> from ratingslib.utils.methods import is_number
>>> x = 1000.12324125345678
>>> is_number(x)
True
>>> x = 'A'
>>> is_number(x)
False
list_has_only_numbers(list_to_check)

Return True if the given list contains numbers only

Examples

>>> from ratingslib.utils.methods import list_has_only_numbers
>>> x = [1, 2 , 1000, 1, 'A']
>>> list_has_only_numbers(x)
False
>>> x = [1, 2 , 1000, 1]
>>> list_has_only_numbers(x)
>>> True