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