A small package, which provides functions for working with nested dictionaries.
Project description
Pydictnest
Nothing fancy. Just a lightweight Python package for some basic work with nested dictionaries. Provides functions to set, get, check, flatten, and unflatten values in arbitrarily deep mappings, plus iteration over nested structures.
- Get and Set Get and set values based on a list of keys in nested mappings
- Check whether a nested path exists.
- Iterate over all (path, value) pairs in a nested mapping.
- Flatten a nested dict to a single-level dict with concatenated keys.
- Unflatten a flat dict back into a nested structure.
Installation
Install via pip with
pip install pydictnest
or from the newest source with
pip install git+https://github.com/MSallermann/pydictnest.git
You can also clone the repo and install locally
git clone https://github.com/MSallermann/pydictnest.git
cd pydictnest
pip install .
Usage
Should be quite obvious. Here is a small writeup (mostly take from the unit test):
from pydictnest import (
set_nested, get_nested, has_nested,
items_nested, flatten_dict, unflatten_dict
)
# Start with an empty dict
data = {}
set_nested(data, ['a', 'b', 'c'], 123)
# data == {'a': {'b': {'c': 123}}}
# Retrieve values
val = get_nested(data, ['a', 'b', 'c']) # 123
missing = get_nested(data, ['x', 'y'], default=0) # 0
# Check existence
assert has_nested(data, ['a', 'b', 'c'])
assert not has_nested(data, ['a', 'z'])
# Iterate over all leaf nodes
for path, value in items_nested(data):
print(path, value)
# ['a', 'b', 'c'] 123
# Flatten and unflatten
nested = {'x': {'y': 1, 'z': {'w': 2}}, 'u': 3}
flat = flatten_dict(nested, sep='.')
# flat == {'x.y': 1, 'x.z.w': 2, 'u': 3}
roundtrip = unflatten_dict(flat, sep='.')
# roundtrip == nested
API Reference
set_nested(dictionary, keys, value, subdict_factory=dict)
Set a value at the specified nested key-path, creating sub-dicts with subdict_factory.
get_nested(dictionary, keys, default=None)
Retrieve a deep value, returning default if any key is missing.
has_nested(dictionary, keys)
Return True if a deep key-path exists, otherwise False.
items_nested(d, subkeys=[])
Yields (key_path, value) tuples for each leaf node in a nested mapping.
keys_nested(d)
Yields key_path for each leaf node in a nested mapping.
values_nested(d)
Yields value for each leaf node in a nested mapping.
flatten_dict(dictionary, sep='.', dict_factory=dict)
Convert a nested dict into a flat dict by joining keys with sep.
unflatten_dict(dictionary, sep='.', dict_factory=dict)
Reconstruct a nested mapping from a flat dict with joined keys.
Running Tests
This project uses pytest. To run the test suite:
pytest
Rationale
Not much of a rationale really. Needed this for a small project and wanted something lightweight.
Contributing
Make a PR if you want to contribute anything.
License
This project is licensed under the MIT License. See LICENSE for details.
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 Distributions
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 pydictnest-0.2.0-py3-none-any.whl.
File metadata
- Download URL: pydictnest-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d8751577abadfe1d4cf6e6eb842f32db895fc01f073344195513729ed2d86068
|
|
| MD5 |
ce2925d325a7cbfb9222867bb68d57a1
|
|
| BLAKE2b-256 |
1eb379bf1802a84b459c96d85fba638a4b89bcd8c6fb752d902e9ff8ddd6eca7
|
Provenance
The following attestation bundles were made for pydictnest-0.2.0-py3-none-any.whl:
Publisher:
pypi.yml on MSallermann/pydictnest
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pydictnest-0.2.0-py3-none-any.whl -
Subject digest:
d8751577abadfe1d4cf6e6eb842f32db895fc01f073344195513729ed2d86068 - Sigstore transparency entry: 351622617
- Sigstore integration time:
-
Permalink:
MSallermann/pydictnest@b38329b8559bcfd19a74443b6d8211f6b78f03df -
Branch / Tag:
refs/heads/main - Owner: https://github.com/MSallermann
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi.yml@b38329b8559bcfd19a74443b6d8211f6b78f03df -
Trigger Event:
push
-
Statement type: