diff options
author | Christian Poessinger <christian@poessinger.com> | 2021-07-25 11:42:04 +0200 |
---|---|---|
committer | Christian Poessinger <christian@poessinger.com> | 2021-07-25 11:58:03 +0200 |
commit | 2ababc4f33295778825763f5cbdf3974e471fee2 (patch) | |
tree | de953a67821c6e8fdddf21c00bb685ac21b1a0df /python/vyos/util.py | |
parent | b5250fa28f22b1cdd1ddbe6ebfce6803f53c8806 (diff) | |
download | vyos-1x-2ababc4f33295778825763f5cbdf3974e471fee2.tar.gz vyos-1x-2ababc4f33295778825763f5cbdf3974e471fee2.zip |
vyos.util: backport 1.4 changes on dict_search()
Diffstat (limited to 'python/vyos/util.py')
-rw-r--r-- | python/vyos/util.py | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/python/vyos/util.py b/python/vyos/util.py index 53e8c08bf..be7c53438 100644 --- a/python/vyos/util.py +++ b/python/vyos/util.py @@ -675,18 +675,20 @@ def find_device_file(device): return None -def dict_search(path, dict): - """ Traverse Python dictionary (dict) delimited by dot (.). +def dict_search(path, my_dict): + """ Traverse Python dictionary (my_dict) delimited by dot (.). Return value of key if found, None otherwise. + This is faster implementation then jmespath.search('foo.bar', my_dict)""" + if not isinstance(my_dict, dict) or not path: + return None - This is faster implementation then jmespath.search('foo.bar', dict)""" parts = path.split('.') inside = parts[:-1] if not inside: - if path not in dict: + if path not in my_dict: return None - return dict[path] - c = dict + return my_dict[path] + c = my_dict for p in parts[:-1]: c = c.get(p, {}) return c.get(parts[-1], None) |