Last active
April 8, 2021 01:18
-
-
Save JacobCallahan/f246b13cc3f6e5d4296e0e484f7bfa86 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
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