mypythontools.misc module

Module with miscellaneous functions. For example myproperty, which is decarator for creating simplified properties or json_to_py that can convert json string to correct python types or str_to_infer_type that will convert string to correct type.

mypythontools.misc.json_to_py(json, replace_comma_decimal=True, convert_decimal=False)[source]

Take json and eval it from strings. If string to string, if float to float, if object then to dict.

When to use? - If sending object as parameter in function.

Parameters
  • json (dict) – JSON with various formats as string.

  • replace_comma_decimal (bool) – Some countries use comma as decimal separator (e.g. 12,3). If True, comma replaced with dot (if not converted to number string remain untouched)

  • convert_decimal (bool) – Some countries has ‘,’ decimal, then conversion would fail. If True, convert ‘,’ to ‘.’ in strings. Only if there are no brackets (list, dict…). For example ‘2,6’ convert to 2.6.

Returns

Python dictionary with correct types.

Return type

dict

mypythontools.misc.str_to_infer_type(string_var)[source]
mypythontools.misc.validate(value, types, options, name=None)[source]

Validate type of variable and check if this variable is in defined options.

Parameters
  • value (Any) – Value that will be validated.

  • types (type) – For example int, str or list.

  • options (list) – List of possible options. If value is not in options, error will be raised.

  • name (str, optional) – If error raised, name will be printed. Defaults to None.

Raises
  • TypeError – Type does not fit.

  • KeyError – Value not in defined options.