Metadata-Version: 2.4
Name: k3dict
Version: 0.1.12
Summary: Dict operation utilities including iteration, getter/setter makers, and attribute access
Author-email: Zhang Yanpo <drdr.xp@gmail.com>
License-Expression: MIT
Project-URL: Homepage, https://github.com/pykit3/k3dict
Project-URL: Documentation, https://k3dict.readthedocs.io
Keywords: dict,dictionary,iteration,attrdict,getter,setter
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Libraries
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == "dev"
Requires-Dist: ruff; extra == "dev"
Requires-Dist: coverage; extra == "dev"
Requires-Dist: k3ut; extra == "dev"
Provides-Extra: publish
Requires-Dist: build; extra == "publish"
Requires-Dist: twine; extra == "publish"
Requires-Dist: pk3; extra == "publish"
Provides-Extra: docs
Requires-Dist: mkdocs>=1.5; extra == "docs"
Requires-Dist: mkdocs-material>=9.0; extra == "docs"
Requires-Dist: mkdocstrings[python]>=0.24; extra == "docs"
Dynamic: license-file

# k3dict

[![Action-CI](https://github.com/pykit3/k3dict/actions/workflows/python-package.yml/badge.svg)](https://github.com/pykit3/k3dict/actions/workflows/python-package.yml)
[![Documentation Status](https://readthedocs.org/projects/k3dict/badge/?version=stable)](https://k3dict.readthedocs.io/en/stable/?badge=stable)
[![Package](https://img.shields.io/pypi/pyversions/k3dict)](https://pypi.org/project/k3dict)

It provides with several dict operation functions.

k3dict is a component of [pykit3] project: a python3 toolkit set.


k3dict

It provides with several dict operation functions.

#   Status

This library is considered production ready.




# Install

```
pip install k3dict
```

# Synopsis

```python
import k3dict

mydict = {"a": {"a.a": "v-a.a", "a.b": {"a.b.a": "v-a.b.a"}, "a.c": {"a.c.a": {"a.c.a.a": "v-a.c.a.a"}}}}

# depth-first iterative the dict
for rst in k3dict.depth_iter(mydict):
    print(rst)

# output:
#     (['a', 'a.c', 'a.c.a', 'a.c.a.a'], 'v-a.c.a.a')
#     (['a', 'a.b', 'a.b.a'], 'v-a.b.a')
#     (['a', 'a.a'], 'v-a.a')

for rst in k3dict.breadth_iter(mydict):
    print(rst)

# output:
#     (['a'],                            {'a.c': {'a.c.a': {'a.c.a.a': 'v-a.c.a.a'}}, 'a.b': {'a.b.a': 'v-a.b.a'}
#                                           , 'a.a': 'v-a.a'})
#     (['a', 'a.a'],                     'v-a.a')
#     (['a', 'a.b'],                     {'a.b.a': 'v-a.b.a'})
#     (['a', 'a.b', 'a.b.a'],            'v-a.b.a')
#     (['a', 'a.c'],                     {'a.c.a': {'a.c.a.a': 'v-a.c.a.a'}})
#     (['a', 'a.c', 'a.c.a'],            {'a.c.a.a': 'v-a.c.a.a'})
#     (['a', 'a.c', 'a.c.a', 'a.c.a.a'], 'v-a.c.a.a')
#

records = [
    {
        "event": "log in",
        "time": {
            "hour": 10,
            "minute": 30,
        },
    },
    {
        "event": "post a blog",
        "time": {
            "hour": 10,
            "minute": 40,
        },
    },
    {
        "time": {
            "hour": 11,
            "minute": 20,
        },
    },
    {
        "event": "log out",
        "time": {
            "hour": 11,
            "minute": 20,
        },
    },
]

get_event = k3dict.make_getter("event", default="NOTHING DONE")
get_time = k3dict.make_getter("time.$field")

for record in records:
    ev = get_event(record)

    hour = get_time(record, {"field": "hour"})
    minute = get_time(record, {"field": "minute"})
    tm = f"{hour:d}:{minute:d}"

    print(f"{ev:<12}   at {tm}")

# output:
# log in         at 10:30
# post a blog    at 10:40
# NOTHING DONE   at 11:20
# log out        at 11:20
```

#   Author

Zhang Yanpo (张炎泼) <drdr.xp@gmail.com>

#   Copyright and License

The MIT License (MIT)

Copyright (c) 2015 Zhang Yanpo (张炎泼) <drdr.xp@gmail.com>


[pykit3]: https://github.com/pykit3
