cfelpyutils.parameter_utils module

Utilities for parsing command line options and configuration files.

cfelpyutils.parameter_utils.convert_parameters(config_dict)

Convert strings in parameter dictionaries to the corrent data type.

Read a parameter dictionary returned by the ConfigParser python module, and convert each entry in an object of the corresponding type, without changing the structure of the dictionary.

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.