Skip to main content

A simple package for safely retrieving nested values from data structures using a sequence of keys or indexes.

Project description

NGET (Nested Get)

Simple Python package for safely retrieving nested values from data structures using sequences of keys or indexes.

Installation

pip install nget

Usage

Import the nget function from the package and use it to safely retrieve nested values from dictionaries or lists.

from nget import nget

data = {"a": {"b": {"c": "value", "d": [1, 2, 3]}}}

nget(data, ["a", "b", "c"])  # Output: "value"
nget(data, ["a", "b", "d", 1])  # Output: 2
nget(data, ["a", "b", "e"], "default")  # Output: "default"
nget(data, ["a", "b", "e"])  # Raises KeyError: 'e'

Examples

Retrieve a nested dictionary value

data = {"a": {"b": {"c": "value"}}}
nget(data, ["a", "b", "c"])  # Output: "value"

Retrieve a nested list value

data = [[["value"]]]
nget(data, [0, 0, 0])  # Output: "value"

Provide a default value if the key is not found

data = {"a": {"b": {}}}
nget(data, ["a", "b", "c"], "default")  # Output: "default"

Motivation

When working with services that interact with third-party, undocumented APIs, we often encounter complex data structures containing nested dictionaries and lists. Extracting the required information from these structures can be challenging, especially when keys or indexes might be missing or their values could be None.

Common Problems

Direct Access

The standard way to retrieve nested values is using multiple square brackets:

data = {"a": {"b": {"c": "value"}}}
data["a"]["b"]["c"]  # Output: "value"

However, if any key is missing, this results in a KeyError.

Using get()

data = {"a": {"b2": {"c2": "value2"}}}
data.get("a", {}).get("b", {}).get("c", None)  # Output: None (default value)

But this approach fails if the intermediate value is None:

data = {"a": {"b": None}}
data.get("a", {}).get("b", {}).get("c", None)

# AttributeError: 'NoneType' object has no attribute 'get'

Using Try-Except

Another common solution is wrapping the access in a try-except block:

try:
    x = data["a"]["b"]["c"]
except KeyError:
    x = None  # Default value

Checking Keys Step-by-Step

Alternatively, you can manually check the existence of each key or index:

x = None
if "a" in data and "b" in data["a"] and "c" in data["a"]["b"]:
    x = data["a"]["b"]["c"]

This is not very convenient, especially when we need to retrieve many nested values.

To address these issues, I created this simple package to safely handle nested data retrieval.

License

This project is licensed under the MIT License.

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

nget-0.0.3.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

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

nget-0.0.3-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file nget-0.0.3.tar.gz.

File metadata

  • Download URL: nget-0.0.3.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.22.1 CPython/3.9.20 Linux/6.8.0-1017-azure

File hashes

Hashes for nget-0.0.3.tar.gz
Algorithm Hash digest
SHA256 f781d2e5e1456298ced74968eafe545461b63525e0dd2bdf6db03375a94b351d
MD5 d6359f97d00e6f08e5c402e4f2e0a2a2
BLAKE2b-256 b8a45e57f2feb2ca91ba7feff5d9c328606b60a63433a624be322a6296f27523

See more details on using hashes here.

File details

Details for the file nget-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: nget-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.22.1 CPython/3.9.20 Linux/6.8.0-1017-azure

File hashes

Hashes for nget-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 071473c436a5405229665d56708ee13e3229868d35cec87015f25f6419c7ebcc
MD5 62e0e38f0c8496b615485383ae06b31c
BLAKE2b-256 f95ea30a7102efc1e50e069f48c241134acd0dd9a7b1b6c8555e484027678bad

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