Skip to content

Instantly share code, notes, and snippets.

@kkroesch
Created May 20, 2019 10:45
Show Gist options
  • Select an option

  • Save kkroesch/cfd017e6ec5b4af42fd9a49fd3370917 to your computer and use it in GitHub Desktop.

Select an option

Save kkroesch/cfd017e6ec5b4af42fd9a49fd3370917 to your computer and use it in GitHub Desktop.
Wrap dictionary in namespace to access values wit dot notation
class NestedNamespace(SimpleNamespace):
""" Wrapping dictionaries in namespace to access it with dot notation. """
def __init__(self, dictionary, **kwargs):
super().__init__(**kwargs)
for key, value in dictionary.items():
if isinstance(value, dict):
self.__setattr__(key, NestedNamespace(value))
else:
self.__setattr__(key, value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment