Created
March 25, 2015 21:20
-
-
Save oss6/df73a464fa9420343ca6 to your computer and use it in GitHub Desktop.
Gets the value of a nested dictionary specified by the path
This file contains hidden or 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 get_prop(d, path): | |
""" | |
Gets the value of a nested dictionary specified by the path | |
d: the dictionary to search recursively | |
path: a string path | |
""" | |
if d is None or not isinstance(d, dict) or not path: | |
return d | |
path = path.split('.') | |
ht = path[0], path[1:] | |
return get_prop(d.get(ht[0]), '.'.join(ht[1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment