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.

example

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


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.0.2.tar.gz (1.9 kB view hashes)

Uploaded Source

Built Distribution

dict8-0.0.2-py3-none-any.whl (1.8 kB view hashes)

Uploaded Python 3

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