From f7c6596befc605d4f4921b678476bd98a1e59b2d Mon Sep 17 00:00:00 2001 From: Joel Collins Date: Mon, 21 Jan 2019 10:49:22 +0000 Subject: [PATCH] Added dictionary filter function --- openflexure_microscope/utilities.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/openflexure_microscope/utilities.py b/openflexure_microscope/utilities.py index 4e01a85f..3c903dee 100644 --- a/openflexure_microscope/utilities.py +++ b/openflexure_microscope/utilities.py @@ -1,4 +1,6 @@ import copy +import operator +from functools import reduce def axes_to_array(coordinate_dictionary, axis_keys=['x', 'y', 'z'], base_array=None): """Takes key-value pairs of a JSON value, and maps onto an array""" @@ -15,4 +17,13 @@ def axes_to_array(coordinate_dictionary, axis_keys=['x', 'y', 'z'], base_array=N if key in coordinate_dictionary: base_array[axis] = coordinate_dictionary[key] - return base_array \ No newline at end of file + return base_array + +def filter_dict(dictionary: dict, keys: list): + # Get value by recursively applying getitem + val = reduce(operator.getitem, keys, dictionary) + + # Create new dictionary by running reduce on key, val pairs + out = reduce(lambda x, y: {y: x}, reversed(keys), val) + + return out \ No newline at end of file