Skip to content

Instantly share code, notes, and snippets.

@rtt
Created August 28, 2012 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtt/3499898 to your computer and use it in GitHub Desktop.
Save rtt/3499898 to your computer and use it in GitHub Desktop.
Itempath
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