Created
June 18, 2018 23:01
-
-
Save nicholishen/0f046ec4214a68688b8980e574539b84 to your computer and use it in GitHub Desktop.
Get a deep AttrDict Object
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
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