culebra.checker module

Checker functions for different data types.

Functions

check_bool(value: bool, name: str) bool

Check if the given value is a valid boolean.

Parameters:
  • value (bool) – The value

  • name (str) – The value name

Returns:

A valid boolean

Return type:

bool

Raises:

TypeError – If value is not a boolean value

check_str(value: str, name: str, valid_chars: str | None = None, invalid_chars: str | None = None) str

Check if the given value is a valid string.

Parameters:
  • value (str) – The value

  • name (str) – The value name

  • valid_chars (str, optional) – If provided, contains the valid chars for the string

  • invalid_chars (str, optional) – If provided, contains the forbiden chars for the string

Returns:

A valid string

Return type:

str

Raises:

TypeError – If value is not a string

check_limits(value: Real, name: str, gt: Real | None = None, ge: Real | None = None, lt: Real | None = None, le: Real | None = None) Real

Check if the given value meets the limits.

Parameters:
  • value (Real) – The value

  • name (str) – The value name

  • gt (Real, optional) – Inferior limit. If provided, value must be greater than gt

  • ge (Real, optional) – Inferior limit. If provided, value must be greater than or equal to ge

  • lt (Real, optional) – Superior limit. If provided, value must be lower than lt

  • le (Real, optional) – Superior limit. If provided, value must be lower than or equal to le

Returns:

A valid integer

Return type:

Real

Raises:

ValueError – If value does not meet any imposed limit

check_int(value: int, name: str, gt: int | None = None, ge: int | None = None, lt: int | None = None, le: int | None = None, ne: int | None = None) int

Check if the given value is a valid integer.

Parameters:
  • value (int) – The value

  • name (str) – The value name

  • gt (int, optional) – Inferior limit. If provided, value must be greater than gt

  • ge (int, optional) – Inferior limit. If provided, value must be greater than or equal to ge

  • lt (int, optional) – Superior limit. If provided, value must be lower than lt

  • le (int, optional) – Superior limit. If provided, value must be lower than or equal to le

  • ne (int, optional) – Not equal. If provided, value can not be equal to ne

Returns:

A valid integer

Return type:

int

Raises:
  • TypeError – If value is not an integer number

  • ValueError – If value does not meet any imposed constraint

check_float(value: float, name: str, gt: float | None = None, ge: float | None = None, lt: float | None = None, le: float | None = None) float

Check if the given value is a valid float.

Parameters:
  • value (float) – The value

  • name (str) – The value name

  • gt (float, optional) – Inferior limit. If provided, value must be greater than gt

  • ge (float, optional) – Inferior limit. If provided, value must be greater than or equal to ge

  • lt (float, optional) – Superior limit. If provided, value must be lower than lt

  • le (float, optional) – Superior limit. If provided, value must be lower than or equal to le

Returns:

A valid float

Return type:

float

Raises:

TypeError – If value is not a floating point number

check_instance(value: object, name: str, cls: type) object

Check if the given value is an instance of cls.

Parameters:
  • value (object) – An object

  • name (str) – The value name

  • cls (type) – A class

Raises:

TypeError – If value is not instance of cls

check_subclass(value: type, name: str, cls: type) type

Check if the given value is subclass of cls.

Parameters:
  • value (type) – A class

  • name (str) – The value name

  • cls (type) – Another class

Raises:

TypeError – If value is not subclass of cls

check_func(value: Callable, name: str) Callable

Check if the given value is a valid function.

Parameters:
  • value (Callable) – A function

  • name (str) – The value name

Raises:

TypeError – If value is not callable

check_func_params(value: Dict[str, Any], name: str) Dict[str, Any]

Check if the given value is a valid set of function parameters.

Parameters:
  • value (A dict) – A dictionary

  • name (str) – The value name

Raises:
  • TypeError – If value is not a dictionary

  • ValueError – If the keys in value are not strings

check_sequence(seq: Sequence[Any], name: str, size: int | None = None, item_checker: Callable[[Any, str], Any] | None = None) List[Any]

Check a sequence of items.

Parameters:
  • seq (Sequence) – The sequence.

  • name (str) – The value name

  • size (int, optional) – Length of the sequence. If omitted, sequences of any length are allowed

  • item_checker (Callable, optional) – Function to check the sequence items. If omitted, no restrictions are applied to the sequence items

Returns:

The checked sequence

Return type:

A list

Raises:
check_filename(value: str, name: str, ext: str | None = None) str

Check if the given value is a valid filename.

Parameters:
  • value (str, bytes or os.PathLike object) – The value

  • name (str) – The value name

  • ext (str, bytes or os.PathLike object, optional) – Required file extension

Returns:

A valid filename

Return type:

str

Raises:
  • TypeError – If value is not a valid file name

  • ValueError – If value does not meet the constraints

  • ValueError – If ext does not begin with a dot

check_matrix(values: Sequence[Sequence[object], ...], name: str, dtype: str | Type | None = None, square: bool | None = False, gt: float | None = None, ge: float | None = None, lt: float | None = None, le: float | None = None) str

Check if the given values define a correct two-dimensional matrix.

Parameters:
  • values – The values

  • name (str) – The value name

  • dtype (str or type) – Data type. If provided, all values must be of this type, optional

  • square (bool) – If True the matrix is required to be square. Defaults to False

  • gt (float, optional) – Inferior limit. If provided, all values must be greater than gt

  • ge (float, optional) – Inferior limit. If provided, all values must be greater than or equal to ge

  • lt (float, optional) – Superior limit. If provided, all values must be lower than lt

  • le (float, optional) – Superior limit. If provided, all values must be lower than or equal to le

Returns:

A valid float

Return type:

float

Returns:

A valid matrix

Return type:

numpy.ndarray

Raises:
  • TypeError – If values is not an array-like object

  • ValueError – If values do not conform the provided dtype

  • ValueError – If values has not an homogeneous shape

  • ValueError – If values has not two dimensions

  • ValueError – If values does not meet any imposed limit