Skip to main content

Utilities for Python nested dictionaries.

Project description

Build Coverage Status PyPI Version PyPI - Python Version License

🪆 Nested Dict Tools

Nested Dict Tools is a Python package that provides utilities for working with nested dictionaries. It includes:

  • Recursive types for describing nested mappings and dictionaries.
  • Fully typed functions to:
    • Flatten and unflatten nested dictionaries.
    • Get and set deeply nested values.
    • Filter and map functions on leaves.
from nested_dict_tools import (
    filter_leaves,
    flatten_dict,
    get_deep,
    map_leaves,
    set_deep,
    unflatten_dict,
)

nested = {"a": {"b": {"c": 42}}}

# Get a deeply nested value
value = get_deep(nested, ["a", "b"])
print(value)  # Output: {'c': 42}

# Set a deeply nested value
set_deep(nested, ["a", "z"], "new_value")
print(nested)  # Output: {'a': {'b': {'c': 42}, 'z': 'new_value'}}

# Flatten the nested dictionary
flat = flatten_dict(nested, sep=".")
print(flat)  # Output: {'a.b.c': 42, 'a.z': 'new_value'}

# Unflatten the flattened dictionary
unflattened = unflatten_dict(flat, sep=".")
print(unflattened == nested)  # Output: True

# Filter leaves
nested = filter_leaves(lambda k, v: isinstance(v, int), nested)
print(nested)  # Output: {'a': {'b': {'c': 42}}}

# Map on leaves
mapped = map_leaves(lambda x: x + 1, nested)
print(mapped)  # Output: {'a': {'b': {'c': 43}}}

# Map on leaves with several dictionaries
mapped = map_leaves(lambda x, y: x + y + 1, nested, nested)
print(mapped)  # Output: {'a': {'b': {'c': 85}}}


# Recursive types:
type NestedDict[K, V] = dict[K, NestedDictNode[K, V]]
type NestedDictNode[K, V] = V | NestedDict[K, V]
# Similar types for Mapping and MutableMapping

⬇️ Installation

You can install Nested Dict Tools via pip:

pip install nested-dict-tools

🧾 License

MIT

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

nested_dict_tools-0.3.3.tar.gz (75.4 kB view details)

Uploaded Source

Built Distribution

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

nested_dict_tools-0.3.3-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file nested_dict_tools-0.3.3.tar.gz.

File metadata

  • Download URL: nested_dict_tools-0.3.3.tar.gz
  • Upload date:
  • Size: 75.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.27.2

File hashes

Hashes for nested_dict_tools-0.3.3.tar.gz
Algorithm Hash digest
SHA256 59f699c0e90d386cbf6f2010f89713119a41c246a0ee8a5c1665e24fda9846d9
MD5 f252352ba039316c17a1cfd682c7c95a
BLAKE2b-256 4b15e9dbd073f9840a7d31bc0cd87e105c7be1ef129ff1ca9bf2caa9740ff15e

See more details on using hashes here.

File details

Details for the file nested_dict_tools-0.3.3-py3-none-any.whl.

File metadata

File hashes

Hashes for nested_dict_tools-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 152256ec5e354c2d2fabb338f5b0339a04fa72c3aa4dbd13c2068eae49fd7b2c
MD5 b2f75de1dbdddee65f9a1f176603a676
BLAKE2b-256 a7511d823675696fe1f03c80838f7c1efbcf81cda2b60252c7753a05ae34b5e9

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