In the age or SaaS, and working with 3rd part APIs developers often have to navigate a complex object (arrays of hashes of arrays or hashes) (I am looking at you Adwords API)
I wanted a nice way to avoid doing None checks and does this key exist over and over again.
So I made a (very) simple utility to help with it dict_digger
importdict_diggerh={'a':{'b':'tuna','c':'fish'},'b':{}}result=dict_digger.dig(h,'a','b')printresult# prints 'tuna'result=dict_digger.dig(h,'c','a')printresult# prints None# Important!! Does not through an error, just returns None#but if you likeresult=dict_digger.dig(h,'c','a',fail=True)# raises a KeyError# also support complex objects so ...complex={'a':{['tuna','fish']},'b':{}}result=dict_digger.dig(complex,'a',0)printresult#prints tuna