Created
August 28, 2012 16:23
-
-
Save rtt/3499898 to your computer and use it in GitHub Desktop.
Itempath
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 itempath(pth, obj): | |
"""Evaluate a dotted 'item path' on object""" | |
parts = pth.split('.', 1) | |
try: | |
parts[0] = int(parts[0]) | |
except ValueError: | |
pass | |
if len(parts) == 1: | |
return obj[parts[0]] | |
item, remaining = parts | |
obj = obj[item] | |
return itempath(remaining, obj) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment