It provides with several dict operation functions.
Project description
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
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 (张炎泼) drdr.xp@gmail.com
Copyright and License
The MIT License (MIT)
Copyright (c) 2015 Zhang Yanpo (张炎泼) drdr.xp@gmail.com
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
k3dict-0.1.2.tar.gz
(8.6 kB
view details)
Built Distribution
k3dict-0.1.2-py3-none-any.whl
(10.0 kB
view details)
File details
Details for the file k3dict-0.1.2.tar.gz
.
File metadata
- Download URL: k3dict-0.1.2.tar.gz
- Upload date:
- Size: 8.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a51bb47032de34fbca6eb69eee2d71264513829651bd3c6b42d6dd07d077fef4 |
|
MD5 | 12b43945fb8c63822c594b77c324c61e |
|
BLAKE2b-256 | 1c8a3c3b271262e06af7b0c9a28d712928079cebf2d013e32d343984e451527a |
File details
Details for the file k3dict-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: k3dict-0.1.2-py3-none-any.whl
- Upload date:
- Size: 10.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 157bd68cfdd89bb333f509e5cade3bbdb7aaf25f2c78f5b6d0e5a45d51dc20f9 |
|
MD5 | 8097ffb311a897e9c12993034470cc25 |
|
BLAKE2b-256 | f309c5d5c1a93ceef72cde979db66e939dfa7942b1216615ec47e3b9b4ebd14b |