traverse nested dicts and deal with it
Project description
dict8
features
No boilerplate: just a, b, path and some code.
Enables you to define a specific merge behavior for every part of the tree.
Merge into datclasses or attrs.
internal
The default machinery converts all input data to a Mapper. If a and b are
mappable, the new, common and old values are taken to a custom function to
decide upon the value precedence. Returning missing
will omit this key
from the intermediate result. The chosen mapper will decide how to incorporate
the latter.
dict8.merge
import dict8
@dict8.ion(DataclassMapper)
def merge(
a: t.Any,
b: t.Any,
path: Path = (),
/,
override: t.Optional[t.Callable] = None,
nothing_new: bool = False,
remove_old: bool = False,
keep_type: bool = True,
**kv: t.Any,
) -> t.Any:
"""Merge two mappable objects into one.
:param a: object a
:param b: object b
:param path: the path of keys
:param override: a function to override b
:param nothing_new: skip new keys if they ar not in a
:param remove_old: skip old keys if they are not in b
:param keep_type: b must have similar type like a
"""
...
custom merger
import dict8
@dict8.ion
def merge(a, b, path, /, **kv):
try:
# try descent into sub mappings
return merge(
a,
b,
path,
**kv,
)
except dict8.UnMappable:
# take b else a
return b if b is not dict8.missing else a
assert (
merge(
{
1: 2,
3: {4: 5},
},
{
3: {4: 2, 5: 1},
},
)
== {1: 2, 3: {4: 2, 5: 1}}
)
license
This is public domain.
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
dict8-0.1.1.tar.gz
(2.2 kB
view details)
Built Distribution
dict8-0.1.1-py3-none-any.whl
(2.0 kB
view details)
File details
Details for the file dict8-0.1.1.tar.gz
.
File metadata
- Download URL: dict8-0.1.1.tar.gz
- Upload date:
- Size: 2.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c7dcd7c034a9a9995796eeb66b596e6bb499080cd79dc5346f1f5f0c85c5177e |
|
MD5 | 64428d69d50bd6957172be5924953fe4 |
|
BLAKE2b-256 | 23b3f72c2d99ed800b77c04c65609f2db08d60cfadc9aded5c4e0c676d953a7a |
File details
Details for the file dict8-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: dict8-0.1.1-py3-none-any.whl
- Upload date:
- Size: 2.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3e310c29f0f19b6c093041fa472288d8ef8c975b8236d71f88c6d699f8adf33c |
|
MD5 | 2dbf1022e57b2c407241154c3c88243f |
|
BLAKE2b-256 | 93d67b2ac5d6d2491d593a7cd4f29d3edf53fa27a394a8387570ea3916b34f79 |