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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
71a72f7916db87173295df8b20105d58f8a1ea75a581c33d4e199ab1e3c4b6d8
|
|
| MD5 |
2d114d26e1296bf05c8ce9f0538ced40
|
|
| BLAKE2b-256 |
e585257a242025ed220f8ab30afb342f6cfb4cca87f19c7babab2acbbf0743ca
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1ddb80d3075737871e66de8131faa99d72f4a5df59fb49eed0e2299f289018ab
|
|
| MD5 |
8418d7e31f37d99aaacda1d67678f2cb
|
|
| BLAKE2b-256 |
8b338934809feaf50a6dfbb02d33b84765a779db5388abec4ce4936c323d2420
|