Skip to content

Instantly share code, notes, and snippets.

@JacobCallahan
Last active April 8, 2021 01:18
Show Gist options
  • Save JacobCallahan/f246b13cc3f6e5d4296e0e484f7bfa86 to your computer and use it in GitHub Desktop.
Save JacobCallahan/f246b13cc3f6e5d4296e0e484f7bfa86 to your computer and use it in GitHub Desktop.
from collections import UserDict
class DictToObj(UserDict):
"""Converts dictionaries into dot accessible objects, great with nesting"""
def __init__(self, in_dict):
"""Initialize the class and all nested dictionaries"""
for key, value in in_dict.items():
if isinstance(value, dict):
setattr(self, key, DictToObj(value))
elif type(value) in (list, tuple):
setattr(
self,
key,
[DictToObj(x) if isinstance(x, dict) else x for x in value],
)
else:
setattr(self, key, value)
super().__init__(in_dict)
def pop(self, item=None):
"""awxkit uses pop() on objects, this allows for that and normal use"""
if not item:
return self
else:
return super().pop(item)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment