cfelpyutils.parameter_utils module

Parameter parsing utilities.

This module contains the implementation of several utilities used to parse and manipulate dictionaries that store options and parameters.

cfelpyutils.parameter_utils.convert_parameters(config_dict)

Convert strings in parameter dictionaries to the correct data type.

Convert a dictionary return by the configparse module to a dictionar contaning the same parameters converted from string to their correct type (int, float, string, etc.)

Try to convert each entry in the dictionary according to the following rules. The first rule that applies to the entry determines the type.

  • If the entry starts and ends with a single quote or double quote, leave it as a string.

  • If the entry starts and ends with a square bracket, convert it to a list.

  • If the entry starts and ends with a curly braces, convert it to a dictionary or a set.

  • If the entry is the word None, without quotes, convert it to NoneType.

  • If the entry is the word False, without quotes, convert it to a boolean False.

  • If the entry is the word True, without quotes, convert it to a boolean True.

  • If none of the previous options match the content of the entry, try to interpret the entry in order as:

    • An integer number.
    • A float number.
    • A string.
  • If all else fails, raise an exception.

Parameters:

config (Dict) – a dictionary containing strings (the dictionary returned by Config Parser).

Returns:

dictionary with the same structure as the input dictionary, but with correct types assigned to each entry.

Return type:

Dict

Raises:
  • RuntimeError – if an entry cannot be converted to any supported
  • type.