Skip to content

Instantly share code, notes, and snippets.

@niallo
Created January 21, 2011 07:11
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 niallo/789348 to your computer and use it in GitHub Desktop.
Save niallo/789348 to your computer and use it in GitHub Desktop.
def sub_get(d, key, default=0):
''' sub_get({ 'foo' : { 'bar' : 1 }}, 'foo.bar') returns 1 '''
if '.' not in key:
return d.get(key, default)
parts = key.split('.')
r = d
for p in parts:
r = r.get(p, default)
return r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment