shared.string_dict_utils

shared.string_dict_utils.load_json_file(filename)[source]

Loads a JSON file.

Parameters:

filename (str) – The path to the JSON file.

Returns:

The content of the JSON file.

Return type:

dict

shared.string_dict_utils.parse_and_identify_type(s)[source]
shared.string_dict_utils.recursively_normalize_dict_keys(d: dict) dict[source]

Normalize all keys in a dictionary. If a value is a dictionary, normalize its keys recursively.

Parameters:

d (dict) – The dictionary whose keys are to be normalized.

Returns:

A new dictionary with normalized keys.

Return type:

dict

Usage:

data = {
    'Hello World': 1,
    'Good_Day': {
        'Inner_Key': 2
    }
}
normalized_data = normalize_dict(data)
print(normalized_data)  # Output: {'helloworld': 1, 'goodday': {'innerkey': 2}}
shared.string_dict_utils.remove_json_comments(json_string)[source]

Removes the comments from a JSON string.

Parameters:

json_string (str) – The JSON string.

Returns:

The cleaned JSON string.

Return type:

str

shared.string_dict_utils.remove_spaces_and_set_case(key: str, case='lower') str[source]

Normalize a string key by converting all characters to lowercase and removing all spaces and underscores.

Parameters:

key (str) – The string key to be normalized.

Returns:

The normalized key.

Return type:

str

Usage:

normalized_key = normalize_key('Hello_World')
print(normalized_key)  # Output: 'helloworld'
shared.string_dict_utils.save_json_file(filename, data)[source]

Saves a JSON file.

Parameters:
  • filename (str) – The path to the JSON file.

  • data (dict) – The data to be saved.

shared.string_dict_utils.validate_and_correct_dictionary(dict_to_verify: dict, default_config: dict, log=None) tuple[source]

Verifies that the given JSON config contains all the required keys as per the default config. If not, adds the missing keys with the default values. Returns a log of warnings and errors in case of missing or unknown keys.

Parameters:
  • dict_to_verify (dict) – The JSON configuration to verify.

  • default_config (dict) – The default configuration used for verification.

  • log (list, optional) – A list of log messages. Default is an empty list.

Returns:

A tuple of the verified JSON configuration and the log of warnings/errors.

Return type:

tuple

Usage:

json_config = {
    "key1": "value1",
    "key2": {
        "subkey1": "subvalue1"
    }
}

default_config = {
    "key1": "value1",
    "key2": {
        "subkey1": "subvalue1",
        "subkey2": "subvalue2"
    },
    "key3": "value3"
}

verified_config, log = verify_configuration_keys(json_config, default_config)
# verified_config will contain all keys from default_config, and log will contain warning/error messages