Nestedaccess is a simple, elegant way to retrieve nested data from deep dictionaries or lists.
Project description
README.md
Nestedaccess
Nestedaccess is a simple, elegant way to retrieve deeply nested data from a dictionary.
Pass in the keys you want to obtain data through lists and tuples in sequence, avoiding the use of overly long if-elif-else judgment statements and making the code more elegant.
In addition, if the field does not exist, it can be customized to return a default value. If there is no default value, an error message will be output. The error message can specify a specific language.
import nestedaccess
data = {
"foo": {
"bar": [1, 2, 3],
"baz": {
"qux": "hello",
"quux": {
"corge": True,
"grault": [4, 5, 6]
}
}
},
"empty_list": [
{
"baz": {
"qux": "hello",
"quux": {
"grault": [1, 2, 3]
}
}
},
{
"baz": {
"qux": "hello1",
"quux": {
"grault": [4, 5, 6]
}
}
}
],
"empty_dict": {}
}
# normal data test.
print(nestedaccess.get(data, ["foo", "baz", "quux", "grault", 1])) # Output: 5
test_key = ("empty_list", 0, "baz", "qux")
print(get(data, test_key)) # Output: hello
# Field does not exist, return default value data test.
print(nestedaccess.get(data, ["nonexistent"], default="Not found")) # Output: Not found
# If the field does not exist, an error message is returned.
test_key = ["foo", "baz", "quux", "corge", "invalid"]
res = nestedaccess.get(data, test_key, lang="en") # Output: ValueError: cannot fetch nested data because object of type bool has no key or index
test1_key = ("foo", "bae", "quux", "corge")
res1 = nestedaccess.get(data, test1_key, lang="en") #Output: KeyError: "field 'bae' does not exist"
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
-
Used to obtain nested data. When the nested structure is deep, data can be obtained through lists or tuples to avoid using overly long if-elif-else judgment statements and make the code more elegant.
-
Takes a nested data object (can be a dictionary or a list), a list of keys or indexes, and an optional default value.
-
It will try to get the nested data in the given key or index order, output the non-existent field name if the field does not exist, and return the default value if a custom default value is set.
-
If the field does not exist, the detailed error message returned can optionally be specified in a specific language ("en" is used by default).
Supported languages:
language code English en Chinese zh
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.2.tar.gz.
File metadata
- Download URL: nestedaccess-1.1.2.tar.gz
- Upload date:
- Size: 5.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 |
64116a70005f7096a2088dea06a55eab007778cd8d7eb5cb963a02b0f7a1dc9d
|
|
| MD5 |
0fed01ac3255743a0af191837b5ff1f7
|
|
| BLAKE2b-256 |
98f379681ff9890f9b4ca15862b1ee78770349bab6841bc8c84cac430dd170b5
|
File details
Details for the file nestedaccess-1.1.2-py3-none-any.whl.
File metadata
- Download URL: nestedaccess-1.1.2-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a955511d5c1315bb7add5014a6679fae36969b39becf350b10493c00226bade2
|
|
| MD5 |
164612dc5e5076bfa4610dd2c54a5d26
|
|
| BLAKE2b-256 |
44856dcfd279bcd62ba5f7fd7f61e011dc75fc9be6d799a57ffbad980395173e
|