Last active
August 29, 2015 14:09
-
-
Save WLPhoenix/bd00b7d8afd690a20c59 to your computer and use it in GitHub Desktop.
Python dict deepen
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 deepen(shallow_obj): | |
"""Recursively deepens a dictionary based on keys""" | |
shallow_copy = copy.copy(shallow_obj) | |
deep_obj = {} | |
for key in shallow_copy: | |
if isinstance(shallow_copy[key], dict): | |
shallow_copy[key] = deepen(shallow_copy[key]) | |
t = deep_obj | |
parts = key.split('.') | |
n_key = parts.pop() | |
while parts: | |
part, parts = parts[0], parts[1:] | |
t[part] = t.get(part, {}) | |
t = t[part] | |
t[n_key] = shallow_copy[key] | |
return deep_obj |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Takes properties in
and converts it to