https://github.com/pykit3/k3dict
It provides with several dict operation functions.
https://github.com/pykit3/k3dict
dictionary python util
Last synced: 6 months ago
JSON representation
It provides with several dict operation functions.
- Host: GitHub
- URL: https://github.com/pykit3/k3dict
- Owner: pykit3
- License: mit
- Created: 2021-07-28T06:36:39.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2025-08-28T12:36:23.000Z (9 months ago)
- Last Synced: 2025-09-28T08:25:35.092Z (8 months ago)
- Topics: dictionary, python, util
- Language: Python
- Homepage: https://blog.openacid.com
- Size: 45.9 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# k3dict
[](https://github.com/pykit3/k3dict/actions/workflows/python-package.yml)
[](https://travis-ci.com/pykit3/k3dict)
[](https://k3dict.readthedocs.io/en/stable/?badge=stable)
[](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)
tm = "%d:%d" % (get_time(record, {"field": "hour"}),
get_time(record, {"field": "minute"}))
print("{ev:<12} at {tm}".format(ev=ev, tm=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 (张炎泼)
# Copyright and License
The MIT License (MIT)
Copyright (c) 2015 Zhang Yanpo (张炎泼)
[pykit3]: https://github.com/pykit3