From 179f9ac5a655c1e100dda8458fccb4f2420989ff Mon Sep 17 00:00:00 2001 From: Azalea Gui <22280294+hykilpikonna@users.noreply.github.com> Date: Mon, 25 Nov 2024 18:47:03 -0500 Subject: [PATCH] [+] Recursive get --- hypy_utils/__init__.py | 2 +- hypy_utils/dict_utils.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/hypy_utils/__init__.py b/hypy_utils/__init__.py index 3d6075e..3dcfb01 100644 --- a/hypy_utils/__init__.py +++ b/hypy_utils/__init__.py @@ -1,6 +1,6 @@ from __future__ import annotations -__version__ = "1.0.23" +__version__ = "1.0.24" import time import logging diff --git a/hypy_utils/dict_utils.py b/hypy_utils/dict_utils.py index 4ffbd99..1e4f768 100644 --- a/hypy_utils/dict_utils.py +++ b/hypy_utils/dict_utils.py @@ -84,3 +84,19 @@ def deep_dict(o: object, exclude: set | None): if isinstance(o, list): return [deep_dict(v, exclude) for v in o] return o + + +def get_rec(cd: dict, key: str): + """ + :param cd: Dictionary + :param key: Recursive key in the format of keya.keyb.keyc... + """ + if '.' not in key: + return cd.get(key) + + ks = key.split('.') + while len(ks) > 0: + cd = cd.get(ks.pop(0)) + if cd is None: + break + return cd