skip to navigation
skip to content

Not Logged In

dict_digger 0.2.1

Useful syntax for digging into nested dictionaries, lists and tuples, and removes the need to check if a key exists

Digs into Dicts

Useful syntax for digging into nested dictionaries, lists and tuples, and removes the need to check if a key or index exists, or handling of KeyError or IndexError

Installation

$ pip install dict_digger

Usage

import dict_digger

h = {
    'a': {
         'b': 'tuna',
         'c': 'fish'
     },
     'b': {}
}

result = dict_digger.dig(h, 'a','b')
print result  # prints 'tuna'

result = dict_digger.dig(h, 'c','a')
print result # prints None
# Important!!  Does not through an error, just returns None

#but if you like
result = 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)
print result #prints tuna

Alternatives

try:
    result = h['c']['a']
except KeyError:
    result = None

Testing

We are using nose

$ nosetests
 
File Type Py Version Uploaded on Size
dict_digger-0.2.1.tar.gz (md5) Source 2013-11-07 1KB
  • Downloads (All Versions):
  • 36 downloads in the last day
  • 253 downloads in the last week
  • 520 downloads in the last month