From 02e995265a4548aac7a990ae3e176f993b2f5a17 Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Sun, 23 Aug 2020 13:05:03 +0200 Subject: vyos.util: T2755: add vyos_dict_search() to traverse a dictionary This is faster implementation then using jmespath.search('foo.bar', dict). --- python/vyos/util.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'python') diff --git a/python/vyos/util.py b/python/vyos/util.py index d27a8a3e7..4cc25764b 100644 --- a/python/vyos/util.py +++ b/python/vyos/util.py @@ -672,3 +672,17 @@ def find_device_file(device): return os.path.join(root, basename) return None + +def vyos_dict_search(dict, path): + """ Traverse Python dictionary (dict) delimited by dot (.). + Return value of key if found, None otherwise. + + This is faster implementation then jmespath.search('foo.bar', dict)""" + parts = path.split('.') + inside = parts[:-1] + if not inside: + return dict[path] + c = dict + for p in parts[:-1]: + c = c.get(p, {}) + return c.get(parts[-1], None) -- cgit v1.2.3