Skip to content

Instantly share code, notes, and snippets.

@ryantuck
Created October 8, 2021 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryantuck/6c275f42e0efa35e146f35ff1c1697b5 to your computer and use it in GitHub Desktop.
Save ryantuck/6c275f42e0efa35e146f35ff1c1697b5 to your computer and use it in GitHub Desktop.
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