From 8c5e1d0cd57853b3ff290864a1e897822e62a853 Mon Sep 17 00:00:00 2001 From: Jeff Bryner Date: Wed, 6 Jun 2018 12:12:18 -0700 Subject: [PATCH] update dotdict to allow for get of a dotted string --- lib/utilities/dot_dict.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib/utilities/dot_dict.py b/lib/utilities/dot_dict.py index 6413ef65..16e0273e 100644 --- a/lib/utilities/dot_dict.py +++ b/lib/utilities/dot_dict.py @@ -17,3 +17,27 @@ class DotDict(dict): if hasattr(value, 'keys'): value = DotDict(value) self[key] = value + + def get(self, key): + """get to allow for dot string notation + :param str key: Key in dot-notation (e.g. 'foo.lol'). + :return: value. None if no value was found. + """ + try: + return self.__lookup(self, key) + except KeyError: + return None + + def __lookup(self, dct, key): + """Checks dct recursive to find the value for key. + Is used by get() interanlly. + :param dict dct: input dictionary + :param str key: The key we are looking for. + :return: The value. + :raise KeyError: If the given key is not found + """ + if '.' in key: + key, node = key.split('.', 1) + return self.__lookup(dct[key], node) + else: + return dct[key] \ No newline at end of file