magni.utils.validation._generic module

Module providing the validate_generic function.

Routine listings

validate_generic(name, type, value_in=None, len_=None, keys_in=None,
has_keys=None, superclass=None, ignore_none=False, var=None) Validate non-numeric objects.
magni.utils.validation._generic.validate_generic(name, type_, value_in=None, len_=None, keys_in=None, has_keys=None, superclass=None, ignore_none=False, var=None)[source]

Validate non-numeric objects.

The present function is meant to validate the type or class of an object. Furthermore, if the object may only take on a limited number of values, the object can be validated against this list. In the case of collections (for example lists and tuples) and mappings (for example dictionaries), a specific length can be required. Furthermore, in the case of mappings, the keys can be validated by requiring and/or only allowing certain keys.

If the present function is called with name set to None, an iterable with the value ‘generic’ followed by the remaining arguments passed in the call is returned. This is useful in combination with the validation function magni.utils.validation.validate_levels.

Parameters:
  • name (None) – The name of the variable to be validated.
  • type_ (None) – One or more references to groups of types, specific types, and/or specific classes.
  • value_in (set-like) – The list of values accepted. (the default is None, which implies that all values are accepted)
  • len_ (int) – The length required. (the default is None, which implies that all lengths are accepted)
  • keys_in (set-like) – The list of accepted keys. (the default is None, which implies that all keys are accepted)
  • has_keys (set-like) – The list of required keys. (the default is None, which implies that no keys are required)
  • superclass (class) – The required superclass. (the default is None, which implies that no superclass is required)
  • ignore_none (bool) – A flag indicating if the variable is allowed to be none. (the default is False)
  • var (None) – The value of the variable to be validated.

See also

magni.utils.validation.validate_levels()
Validate contained objects.
magni.utils.validation.validate_numeric()
Validate numeric objects.

Notes

name must refer to a variable in the parent scope of the function or method decorated by magni.utils.validation.decorate_validation which is closest to the top of the call stack. If name is a string then there must be a variable of that name in that scope. If name is a set-like object then there must be a variable having the first value in that set-like object as name. The remaining values are used as keys/indices on the variable to obtain the variable to be validated. For example, the name (‘name’, 0, ‘key’) refers to the variable “name[0][‘key’]”.

type_ is either a single value treated as a list with one value or a set-like object containing at least one value. Each value is either a specific type or class, or it refers to one or more types by having one of the string values ‘string’, ‘explicit collection’, ‘implicict collection’, ‘collection’, ‘mapping’, ‘function’, ‘class’.

  • ‘string’ tests if the variable is a str.
  • ‘explicit collection’ tests if the variable is a list or tuple.
  • ‘implicit collection’ tests if the variable is iterable.
  • ‘collection’ is a combination of the two above.
  • ‘mapping’ tests if the variable is a dict.
  • ‘function’ tests if the variable is a function.
  • ‘class’ tests if the variable is a type.

var can be used to pass the value of the variable to be validated. This is useful either when the variable cannot be looked up by name (for example, if the variable is a property of the argument of a function) or to remove the overhead of looking up the value.

Examples

Every public function and method of the present package (with the exception of the functions of this subpackage itself) validates every argument and keyword argument using the functionality of this subpackage. Thus, for examples of how to use the present function, browse through the code.

magni.utils.validation._generic._check_inheritance(name, var, superclass)[source]

Check the superclasses of a variable.

Parameters:
  • name (None) – The name of the variable to be validated.
  • var (None) – The value of the variable to be validated.
  • superclass (class) – The required superclass.
magni.utils.validation._generic._check_keys(name, var, keys_in, has_keys)[source]

Check the keys of a variable.

Parameters:
  • name (None) – The name of the variable to be validated.
  • var (None) – The value of the variable to be validated.
  • keys_in (set-like) – The allowed keys.
  • has_keys (set-like) – The required keys.
magni.utils.validation._generic._check_len(name, var, len_)[source]

Check the length of a variable.

Parameters:
  • name (None) – The name of the variable to be validated.
  • var (None) – The value of the variable to be validated.
  • len_ (int) – The required length.
magni.utils.validation._generic._check_type(name, var, types_)[source]

Check the type of a variable.

Parameters:
  • name (None) – The name of the variable to be validated.
  • var (None) – The value of the variable to be validated.
  • types_ (set-like) – The allowed types.
magni.utils.validation._generic._check_value(name, var, value_in)[source]

Check the value of a variable.

Parameters:
  • name (None) – The name of the variable to be validated.
  • var (None) – The value of the variable to be validated.
  • value_in (set-like) – The allowed values.