nwm_eval.utils.expand_with_lists#
- nwm_eval.utils.expand_with_lists(template_str, context)[source]#
Expand a string with list placeholders using Cartesian product.
Always returns a dict if any list placeholders are involved — even if only one result. Otherwise, returns a plain substituted string.
- Return type:
dict|str- Parameters:
template_str (str) – The string template containing placeholders in the form {key}.
context (dict) – A dictionary where keys correspond to placeholders in the template string. Values can be either single values or lists. If a value is a list, the function will generate all combinations of the list items for that placeholder.
- Returns:
- If any placeholders correspond to list values, returns a dictionary where keys are
concatenated string representations of the list items and values are the template string with those items substituted. If no placeholders correspond to list values, returns the template string with simple substitution.
- Return type:
dict or str
Example
Given template_str = “file_{region}_{year}.csv” and context = {“region”: [“north”, “south”], “year”: [2020, 2021]}, the function will return: {
“north_2020”: “file_north_2020.csv”, “north_2021”: “file_north_2021.csv”, “south_2020”: “file_south_2020.csv”, “south_2021”: “file_south_2021.csv”
}