Module functions.metatables.validators

Functions

def make_categorical_validator(value_type, values)

Generate a categorical validator function for attribute validation.

This function returns a validator function that checks if the provided attribute value is of the specified value_type and belongs to the specified set of allowed values.

Parameters

value_type (type): The expected data type of the attribute value. values (list): The list of allowed values for the attribute.

Returns

Callable
A validator function for attribute validation.

Example

categorical_validator = make_categorical_validator(str, ['value1', 'value2']) @attr.s class MyClass: my_attribute = attr.ib(validator=categorical_validator)

def make_range_validator(value_type, min_value, max_value)

Generate a range validator function for attribute validation.

This function returns a validator function that checks if the provided attribute value is of the specified value_type and falls within the specified range.

Parameters

value_type (type): The expected data type of the attribute value. min_value (Any): The minimum allowed value for the attribute. max_value (Any): The maximum allowed value for the attribute.

Returns

Callable
A validator function for attribute validation.

Example

range_validator = make_range_validator(int, 1, 10) @attr.s class MyClass: my_attribute = attr.ib(validator=range_validator)

Classes

class LabelsDict (*args, **kwargs)

A custom dictionary for storing value labels in a MetaTable.

This class is designed to enforce that keys are of type 'int' and values are of type 'str'.

Attributes

Inherits from dict.

Methods

setitem(key, value): Overrides the setitem method to enforce the data types of keys and values.

Example

labels_dict = LabelsDict() labels_dict[1] = 'Label 1'

Ancestors

  • builtins.dict