Skip to content

Instantly share code, notes, and snippets.

@jtushman
Last active December 14, 2015 09:49
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 jtushman/5067266 to your computer and use it in GitHub Desktop.
Save jtushman/5067266 to your computer and use it in GitHub Desktop.
from copy import deepcopy
def dig(hash,*keys):
"""
digs into an dict, if anything along the way is None, then simply return None
So instead of
entry['bids']['maxCpm']['amount']['microAmount']
you do
dig(entry,'bids','maxCpm','amount','microAmount')
"""
end_of_chain = deepcopy(hash)
for key in keys:
if isinstance(end_of_chain,dict) and key in end_of_chain:
end_of_chain = end_of_chain[key]
else:
return None
return end_of_chain
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment