Metadata-Version: 2.3
Name: nested_dict_tools
Version: 0.3.0
Summary: Utilities for Python nested dictionaries.
Project-URL: Repository, https://github.com/Kajiih/nested_dict_tools
Project-URL: Issues, https://github.com/Kajiih/nested_dict_tools/issues
Author-email: Kajih <itskajih@gmail.com>
License: MIT License
        
        Copyright (c) 2024, Kajih <itskajih@gmail.com>
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
Keywords: nested dictionary,python,tools,utils
Classifier: Development Status :: 4 - Beta
Classifier: License :: OSI Approved :: MIT License
Classifier: Natural Language :: English
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: Implementation :: CPython
Requires-Python: >=3.12
Description-Content-Type: text/markdown

[![Build][github-ci-image]][github-ci-link]
[![Coverage Status][codecov-image]][codecov-link]
[![PyPI Version][pypi-image]][pypi-link]
[![PyPI - Python Version][python-image]][pypi-link]
![License][license-image-mit]

# 🪆 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.

```python
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:

```bash
pip install nested-dict-tools
```

## 🧾 License

[MIT](LICENSE)

<!-- Links -->
[github-ci-image]: https://github.com/kajiih/nested_dict_tools/actions/workflows/build.yml/badge.svg?branch=main
[github-ci-link]: https://github.com/kajiih/nested_dict_tools/actions?query=workflow%3Abuild+branch%3Amain

[codecov-image]: https://img.shields.io/codecov/c/github/kajiih/nested_dict_tools/main.svg?logo=codecov&logoColor=aaaaaa&labelColor=333333
[codecov-link]: https://codecov.io/github/kajiih/nested_dict_tools

[pypi-image]: https://img.shields.io/pypi/v/nested-dict-tools.svg?logo=pypi&logoColor=aaaaaa&labelColor=333333
[pypi-link]: https://pypi.python.org/pypi/nested-dict-tools

[python-image]: https://img.shields.io/pypi/pyversions/nested-dict-tools?logo=python&logoColor=aaaaaa&labelColor=333333
[license-image-mit]: https://img.shields.io/badge/license-MIT-blue.svg?labelColor=333333
