cmip6_data_citation_generator.utils API¶
-
cmip6_data_citation_generator.utils.
deep_substitute
(input_val, substitutions)¶ Substitute strings in the input recursively
In the input, strings contained between carets e.g. ‘<name>’, will be replaced with the corresponding value in
substitutions
. Before looking for a replacement, the carets are removed. For example, ‘<name>’ will be replaced withsubstitutions["name"]
.Note that keys in any input value which is a dictionary will not be replaced, see the examples.
Parameters: - input_val (str, float, int, list, dict, nested structures of the above) – The object in which the substitutions should be made
- substitutions (dict) – The substitutions to make
Returns: The input_val with all substitutions made
Return type: type(input_val)
Raises: KeyError
– If no substitution can be foundExamples
>>> deep_substitute("<source_id>", {"source_id": "UoM"}) 'UoM'
>>> deep_substitute(["<source_id>", "other string"], {"source_id": "UoM"}) ['UoM', 'other string']
>>> deep_substitute([["<source_id>", "other string"], "<activity_id>"], {"source_id": "UoM", "activity_id": "21st Century runs"}) [['UoM', 'other string'], '21st Century runs']
>>> deep_substitute([{"other string": "<source_id>"}, "<activity_id>"], {"source_id": "UoM", "activity_id": "21st Century runs"}) [{'other string': 'UoM'}, '21st Century runs']
>>> # keys in input dictionaries are not substituted >>> deep_substitute({"<source_id>": "<source_id>"}, {"source_id": "UoM"}) {'<source_id>': 'UoM'}
>>> # missing substitutions will raise ``KeyError`` >>> deep_substitute("<source_id>", {"activity_id": "21st Century runs"}) KeyError: "No substitution provided for ['<source_id>']"