Utils#

Interesting methods that globally helps

sanitize_secondary_parameters(d, **kw)#

This function is used to sanitize any secondary parameter (meaning not mandatory parameters)

Parameters
  • d (dict) – A dictionnary with a list of parameters to sanitize

  • **kw (dict, required) – Arbitrary keyword arguments for secondary parameters.

Returns

a sanitized dictionnary with secondary parameters

Return type

dict

Examples

>>> d = {"field1": str}
>>> kw = {"field1": "value1"}
>>> sanitize_secondary_parameters(d=d, **kw)
{"field1": "value1"}
>>> d = {"field1": str}
>>> kw = {"field1": "value1", "field2": "value2"}
>>> sanitize_secondary_parameters(d=d, **kw)
{"field1": "value1"}
sanitize_value(field, t, is_mandatory=False, default=None, **kw)#

This function is used to sanitize the value from a keyword dictionnary (such as default value, type etc.)

Parameters
  • field (str) – Name of the field to sanitize

  • t (type) – Expected type from the given field

  • is_mandatory (bool, optional) – Indicates if field is mandatory. Defaults to False

  • default (Any, optional) – Default value for field if any. Defaults to None

  • **kw (dict, required) – The field value will be extracted from the provided keyword aguments

Raises
  • MandatoryFieldMissing – The value is not given as a keyword parameter and it’s mandatory

  • WrongType – If t is not equals to the value type for the given field

Returns

the value for the given field if all checks passed

Return type

Any

Examples

>>> sanitize_value(field="field1", t=str, is_mandatory=False, default=None, {"field1": "value1"})
"value1"
>>> sanitize_value(field="field1", t=int, is_mandatory=False, default=None, {"field1": "value1"})
# Exception raised as "value1" is not an integer