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 = False,
**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}}
)
merge into attrs and dataclasses
import typing as t
import dataclasses as dc
import dict8
@dc.dataclass
class Foo:
my_value: int
some: str = "default"
@dc.dataclass
class Bar:
foo: Foo
baz: t.Optional[int] = None
bar = dict8.merge(Bar, {"foo": {"my_value": 123}})
assert bar == Bar(foo=Foo(my_value=123, some="default"), baz=None)
license
This is public domain.
Metadata
Release files for dict8 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| dict8-0.4.0.tar.gz | 6.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| dict8-0.4.0-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 12.3 kB
Release files / dict8-0.4.0.tar.gz
| Download URL | dict8-0.4.0.tar.gz |
|---|---|
| Size | 6.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
80a3ccbc1f11cf8395e45b3a658e2c6cab0dbb50ddc97482043d35d0a42bb6ab
|
|
BLAKE2b-256 checksum How to use checksums |
304e47f60ce7ac65f93b8672ee3bb6762aabed01f1f846715bf26da4e27c355a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.11
|
Release files / dict8-0.4.0-py2.py3-none-any.whl
| Download URL | dict8-0.4.0-py2.py3-none-any.whl |
|---|---|
| Size | 6.2 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
cbf993979c44959aa8df8153a490a725d98f7aa13951126721fa911e92fff620
|
|
BLAKE2b-256 checksum How to use checksums |
380de68fc86f7701abf2c62f5279a969f7376b96224dcddad12c6c783c82c1ae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.11
|