Skip to content
Snippets Groups Projects
Commit 80513b80 authored by leufen1's avatar leufen1
Browse files

new helper to extract subset of dictionary indicated by key

parent 4e1252c2
No related branches found
No related tags found
3 merge requests!226Develop,!225Resolve "release v1.2.0",!197Resolve "REFAC: mixed sampling data handler loads to much data"
...@@ -3,4 +3,4 @@ ...@@ -3,4 +3,4 @@
from .testing import PyTestRegex, PyTestAllEqual from .testing import PyTestRegex, PyTestAllEqual
from .time_tracking import TimeTracking, TimeTrackingWrapper from .time_tracking import TimeTracking, TimeTrackingWrapper
from .logger import Logger from .logger import Logger
from .helpers import remove_items, float_round, dict_to_xarray, to_list, extract_value from .helpers import remove_items, float_round, dict_to_xarray, to_list, extract_value, select_from_dict
...@@ -99,6 +99,19 @@ def remove_items(obj: Union[List, Dict], items: Any): ...@@ -99,6 +99,19 @@ def remove_items(obj: Union[List, Dict], items: Any):
raise TypeError(f"{inspect.stack()[0][3]} does not support type {type(obj)}.") raise TypeError(f"{inspect.stack()[0][3]} does not support type {type(obj)}.")
def select_from_dict(dict_obj: dict, sel_list: str):
"""
Extract all key values pairs whose key is contained in the sel_list.
Does not perform a check if all elements of sel_list are keys of dict_obj. Therefore the number of pairs in the
returned dict is always smaller or equal to the number of elements in the sel_list.
"""
sel_list = to_list(sel_list)
assert isinstance(dict_obj, dict)
sel_dict = {k: v for k, v in dict_obj.items() if k in sel_list}
return sel_dict
def extract_value(encapsulated_value): def extract_value(encapsulated_value):
try: try:
return extract_value(encapsulated_value[0]) return extract_value(encapsulated_value[0])
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment