Skip to content

Dict manip

load_yaml(input_yaml)

Load the yaml file

Source code in automation/utils/dict_manip.py
 8
 9
10
11
12
def load_yaml(input_yaml: str):
    """Load the yaml file"""
    with open(input_yaml, encoding="utf8") as f:
        data = yaml.safe_load(f)
    return data

sort_dict(my_dict, my_list)

Sort dict by list of keys. Return OrderedDict

Source code in automation/utils/dict_manip.py
15
16
17
18
19
20
21
def sort_dict(my_dict, my_list):
    """Sort dict by list of keys. Return OrderedDict"""
    index_map = {v: i for i, v in enumerate(my_list)}
    my_dict_reduced = {k: v for k, v in my_dict.items() if k in my_list}
    return OrderedDict(
        sorted(my_dict_reduced.items(), key=lambda pair: index_map[pair[0]])
    )

flatten_embedded(input_dict)

Check vals in input. If dict, make embedded values new keys in output dict

Novel keys in output dict are {'key_embedded-key': 'embedded_value'}

Parameters:

Name Type Description Default
input_dict dict

any dict

required

Returns:

Type Description
dict

output_dict (dict)

Source code in automation/utils/dict_manip.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def flatten_embedded(input_dict) -> dict:
    """Check vals in input. If dict, make embedded values new keys in output dict

    Novel keys in output dict are {'key_embedded-key': 'embedded_value'}

    Args:
        input_dict (dict): any dict

    Returns:
        output_dict (dict)
    """
    output = {}
    for k, v in input_dict.items():
        if isinstance(v, dict):
            output.update(
                {
                    f"{k}_{embed_k}": list_to_or(embed_v)
                    for embed_k, embed_v in v.items()
                    if embed_v
                }
            )
        else:
            output.update({k: v})
    return output

filter_dict_by_key(dict_content, key_filter='Type', key_options=None)

Filter an embedded dict by if values of key_filter in listed key_options

Example

my_dict = {"a": {"b": 1}, "c": {"b": 2}, "d": {"b": 3}} filter_dict_by_key(dict_content=my_dict,key_filter="b",key_options=[1,2])

{'a': {'b': 1}, 'c': {'b': 2}}

Parameters:

Name Type Description Default
dict_content dict

Input dict

required
key_filter str

Optional specification of key. Default "Type"

'Type'
key_options set

Set of items that, when retyped to list must match value[list_filter].

None

Returns:

Name Type Description
dict dict

filtered dict

Source code in automation/utils/dict_manip.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
def filter_dict_by_key(
    dict_content: dict,
    key_filter: str = "Type",
    key_options: set = None,
) -> dict:
    """Filter an embedded dict by if values of key_filter in listed key_options

    Example:
        my_dict = {"a": {"b": 1}, "c": {"b": 2}, "d": {"b": 3}}
        filter_dict_by_key(dict_content=my_dict,key_filter="b",key_options=[1,2])
        >> {'a': {'b': 1}, 'c': {'b': 2}}

    Args:
        dict_content (dict): Input dict
        key_filter (str): Optional specification of key. Default "Type"
        key_options (set): Set of items that, when retyped to list must match
            value[list_filter].

    Returns:
        dict: filtered dict
    """
    if not key_options:
        return dict_content
    return {
        key: value
        for (key, value) in dict_content.items()
        if value[key_filter] in list(key_options)
    }