Digs into Dicts (lists and tuples)
Python
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
dict_digger
.gitignore
README.rst
setup.py
test_dict_digger.py

README.rst

dict_digger

Digs into Dicts (or Lists, or Tuples or Complex objects)

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

from dict_digger import dig

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

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

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

# but if you like
result = dig(h, 'c','a', fail=True)
# raises a KeyError

# also supports complex objects so ...

complex = {
    'a': {
         ['tuna','fish']
     },
     'b': {}
}
result = dig(complex, 'a',0)
print result #prints tuna

Alternatives

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

Testing

We are using nose

$ nosetests