Skip to main content

Nestedaccess is a simple, elegant way to retrieve nested data from deep dictionaries or lists.

Project description

Nestedaccess

Nestedaccess is a simple, elegant way to retrieve nested data from deep dictionaries. If the field does not exist, you can customize the return to the default value, and you can also choose whether to output an error message.

import nestedaccess

data = {
    'foo': {
        'bar': [1, 2, 3],
        'baz': {
            'qux': 'hello',
            'quux': {
                'corge': True,
                'grault': [4, 5, 6]
            }
        }
    },
    'empty_list': [],
    'empty_dict': {}
}

# normal data test.
print(nestedaccess.get(data, ['foo', 'baz', 'quux', 'grault', 1]))  # Output: 5

# Field does not exist, return default value data test.
print(nestedaccess.get(data, ['nonexistent'], default='Not found'))  # Output: 'Not found'

# The field does not exist, no error message data test is returned.
print(nestedaccess.get(data, ['foo', 'baz', 'quux', 'corge', 'invalid']))  # Output: None

# The field does not exist and returns an error message test.
test_key = ['foo', 'baz', 'quux', 'corge', 'invalid']
print(nestedaccess.get(data, test_key, error_info=True))  # Output: None (dictionary key does not exist)
print(nestedaccess.get(data, test_key, error_info=True, default='Not found'))  # Output: 'Not found'

Installing Nestedaccess and Supported Versions

Nestedaccess is available on PyPI:

$ python -m pip install nestedaccess

Nestedaccess officially supports Python 3.7+.

Supported Features & Best–Practices

  • It is used to obtain nested data. In the case of a deep nested structure, the data is obtained through a list, which makes the code more elegant.
  • Takes a nested data object (which can be a dictionary or a list), a list of keys or indices, and an optional default value.
  • It will attempt to fetch nested data in the given key or index order, and fall back to the default value if fetching fails.
  • If the field does not exist, output the non-existent field name and return the default value.
  • Customize the output error message (no error message is output by default).

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

nestedaccess-1.1.0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nestedaccess-1.1.0-py2.py3-none-any.whl (3.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file nestedaccess-1.1.0.tar.gz.

File metadata

  • Download URL: nestedaccess-1.1.0.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.0

File hashes

Hashes for nestedaccess-1.1.0.tar.gz
Algorithm Hash digest
SHA256 71a72f7916db87173295df8b20105d58f8a1ea75a581c33d4e199ab1e3c4b6d8
MD5 2d114d26e1296bf05c8ce9f0538ced40
BLAKE2b-256 e585257a242025ed220f8ab30afb342f6cfb4cca87f19c7babab2acbbf0743ca

See more details on using hashes here.

File details

Details for the file nestedaccess-1.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: nestedaccess-1.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.0

File hashes

Hashes for nestedaccess-1.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1ddb80d3075737871e66de8131faa99d72f4a5df59fb49eed0e2299f289018ab
MD5 8418d7e31f37d99aaacda1d67678f2cb
BLAKE2b-256 8b338934809feaf50a6dfbb02d33b84765a779db5388abec4ce4936c323d2420

See more details on using hashes here.

Supported by

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