Skip to main content

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

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dict_digger-0.2.1.tar.gz (1.6 kB view details)

Uploaded Source

File details

Details for the file dict_digger-0.2.1.tar.gz.

File metadata

File hashes

Hashes for dict_digger-0.2.1.tar.gz
Algorithm Hash digest
SHA256 85db80169b93bfa3df55f1f111bc2ca695a48e7f4cbcb09aaee37df52094efdd
MD5 0cad877db7638700ce738ece96ccdd9b
BLAKE2b-256 778621a269637c44bbcd0a6b1cf8e2e72e56ebaf70e354cce4df852089a907ed

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page