Skip to main content

Provides path-like access to nested dictionary elements

Project description

Simple DictPath

Provides a simple path-like access to nested dictionary elements

Install

pip install simple-dictpath

Examples

from dictpath import dictp

# First declare a dictp, just like a regular dict.
testval = dictp({
    "first": {
        "second": {
            "animals": [
                {
                    "type": "cat",
                    "name": "meow1",
                    "children": 2
                },
                {
                    "type": "cat",
                    "name": "meow1",
                    "children": 4
                },
                {
                    "type": "cat",
                    "name": "meow1",
                    "children": 6
                },
                {
                    "type": "dog",
                    "name": "dog1",
                    "eye_colour": "blue"
                },
                {
                    "type": "dog",
                    "name": "dog2",
                    "eye_colour": "green"
                },
            ]
        }
    }
})

# dictps can behave just like regular dicts when needed:
# prints: 'meow1'
print(testval["first"]["second"]["animals"][0]["name"])

# But the power comes in cleanly accessing nested elements.
# To use, just call the dictp value like a function with '()' syntax:
animals = testval("first/second/animals")
print(animals)

# If a any key is missing, whole thing just returns None:
cars = testval("first/third/cars")
assert(cars is None)

# To force a keyError, use raise_if_missing:
# This throws: KeyError: 'third'
testval("first/third/cars", raise_if_missing=True)

# You can use list indexes to only get certain elements in a nested list
# prints: 'cat'
first = testval("first/second/animals/0/type")
print(first)

# Uses list comprehension, e.g. "1:2", ":-1", ":", etc.
last_two = testval("first/second/animals/-2:/type")
middle_cats = testval("first/second/animals/1:-2/name")
all_animals = testval("first/second/animals")
all_types = testval("first/second/animals/:/type")

# If key actually is a string with ":", use raw_index option
testval("real/key/:/a", raw_index=True)

# To only get list items that have a specific subkey, use drop_missing:
# This returns [None, None, None, 'blue', 'green']
testval("first/second/animals/:/eye_colour")
# This returns ['blue', 'green']
testval("first/second/animals/:/eye_colour", drop_missing=True)

# You can also set a overide the value if missing:
# This returns ['unknown', 'unknown', 'unknown', 'blue', 'green']
testval("first/second/animals/:/eye_colour", value_if_missing="unknown")

# These both return "no cars"
testval("first/second/cars", value_if_missing="no cars")
testval("first/second/cars/0/type", value_if_missing="no cars")

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

simple-dictpath-1.0.3.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

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

simple_dictpath-1.0.3-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file simple-dictpath-1.0.3.tar.gz.

File metadata

  • Download URL: simple-dictpath-1.0.3.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.6

File hashes

Hashes for simple-dictpath-1.0.3.tar.gz
Algorithm Hash digest
SHA256 9780871b19ca19500e33bdc4dfa99f546d1f21c069a875ce8eb0a0717ef10345
MD5 a95f6b4cf123016db3bc7deeae53c43a
BLAKE2b-256 18c99a02d979d4975b368ce3634b01258a69be0181fa5464119faa11ec03f8e8

See more details on using hashes here.

File details

Details for the file simple_dictpath-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for simple_dictpath-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 92766c571215eea6c7ddfd3fa29b060a3d0c7d9d3ca3acc771618492c21641ba
MD5 cf39aa7ecd529b2dc398735b0bd0aeb4
BLAKE2b-256 ee5e50d4acc48f6d50c7a07d39d04d5f9b95016145b994ee2102bbad0c47853c

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