Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nicholishen/0f046ec4214a68688b8980e574539b84 to your computer and use it in GitHub Desktop.
Save nicholishen/0f046ec4214a68688b8980e574539b84 to your computer and use it in GitHub Desktop.
Get a deep AttrDict Object
def deep_attrdict(d: dict) -> AttrDict:
"""Uses recursion to dive deep into the dict
and convert all dict types to type AttrDict. This will
eliminate the bug where pulling a list of dicts
out of an AttrDict results in just a list of dicts.
"""
d = AttrDict(d)
for k, v in d.items():
if isinstance(v, dict):
d[k] = deep_attrdict(v)
elif isinstance(v, list):
for i, item in enumerate(v):
if isinstance(item, dict):
v[i] = deep_attrdict(item)
return d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment