Created
October 8, 2021 03:28
-
-
Save ryantuck/6c275f42e0efa35e146f35ff1c1697b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RecursiveNamespace(SimpleNamespace): | |
""" | |
Recursive instantiation of SimpleNamespace for supporting nested dicts. | |
Literally copied from the internet: https://dev.to/taqkarim/extending-simplenamespace-for-nested-dictionaries-58e8 | |
""" | |
@staticmethod | |
def map_entry(entry): | |
if isinstance(entry, dict): | |
return RecursiveNamespace(**entry) | |
return entry | |
def __init__(self, **kwargs): | |
super().__init__(**kwargs) | |
for key, val in kwargs.items(): | |
if type(val) == dict: | |
setattr(self, key, RecursiveNamespace(**val)) | |
elif type(val) == list: | |
setattr(self, key, list(map(self.map_entry, val))) | |
def recursive_namespace(input_dict): | |
return RecursiveNamespace(**input_dict) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment