jtushman / dict_digger

Digs into Dicts (lists and tuples)

Home Page:http://jtushman.github.io/blog/2013/11/06/dict-digger/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

About

Digs into Dicts (lists and tuples)

http://jtushman.github.io/blog/2013/11/06/dict-digger/


Languages

Language:Python 100.0%