Utilities for Python nested dictionaries.
Project description
🪆 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
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
nested_dict_tools-0.3.2.tar.gz
(73.4 kB
view details)
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 nested_dict_tools-0.3.2.tar.gz.
File metadata
- Download URL: nested_dict_tools-0.3.2.tar.gz
- Upload date:
- Size: 73.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.27.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
022c8fcaf231fea13a762ae0f8a7ac121307ee9e1d03f8582779e0c3a3c2f18b
|
|
| MD5 |
b32e4c2609d6265fe88eeb0c059c8d04
|
|
| BLAKE2b-256 |
83b3df4027c89005a89eb1648a2bc626a67504194b413e6f6076a4b4d325f462
|
File details
Details for the file nested_dict_tools-0.3.2-py3-none-any.whl.
File metadata
- Download URL: nested_dict_tools-0.3.2-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-httpx/0.27.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
220d87a54fb61015c0bc78c751041500c39201b2a2daed3fc60f73995530ca05
|
|
| MD5 |
ce2338eae479442b6d2d862053535645
|
|
| BLAKE2b-256 |
cf72839814305645944e5aa096247d005dd91098171f152540c5a66865e01116
|