Skip to main content

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 = 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.

Project details


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.3.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

dict8-0.3.0-py2.py3-none-any.whl (5.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file dict8-0.3.0.tar.gz.

File metadata

  • Download URL: dict8-0.3.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for dict8-0.3.0.tar.gz
Algorithm Hash digest
SHA256 9724171eb52cd9c906ad3a5e49683d38fe932a6f8383ecf791c0fa048abf87c2
MD5 647a68c63c875a53c63d45c869996794
BLAKE2b-256 4bd66412d83fe7e51c5fca3ac33eed54ed340b7b28ee717805f773fd99ff4362

See more details on using hashes here.

File details

Details for the file dict8-0.3.0-py2.py3-none-any.whl.

File metadata

  • Download URL: dict8-0.3.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using 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

File hashes

Hashes for dict8-0.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6e1cc2f40dbe9ca08874a6c0fbc5bd77fecdab2fd5b882122c76dfa2f350787a
MD5 a562fa10a351034fec7e652d295b4daf
BLAKE2b-256 0e7942724808e6fd5fe2e3539efb4faffdba2956607f3e2c5961331dcfeabfb6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page