Skip to main content

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(nestedaccess.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


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.3.tar.gz (5.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.3-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: nestedaccess-1.1.3.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

Hashes for nestedaccess-1.1.3.tar.gz
Algorithm Hash digest
SHA256 1789ad799cd2181fcbf7ba12a58df71ebb8ed81b42ed9882339459c2150c7d58
MD5 45cd6b33fb486ed7c4ed53b99103594d
BLAKE2b-256 7b9df221caa4c99f85b154f62c40d19b4345b7957c87dcdb70444082ebf41ae5

See more details on using hashes here.

File details

Details for the file nestedaccess-1.1.3-py3-none-any.whl.

File metadata

  • Download URL: nestedaccess-1.1.3-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

Hashes for nestedaccess-1.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 27f6188c8e1beb42e8d22b072e4925b9e1837b7bbe1fd3ca92892f070c4c6d22
MD5 0d84d4ffd24d376dbf741f08024fb527
BLAKE2b-256 92b68c7761a0fcb1878ed2ca7c8d82ff1b3d6c606a9e9742a90959c78604a642

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