Skip to main content

Merges two arbitrarily deep hashes into a single hash.

Project description

hashmerge: a Python library to merge deep hashes

hashmerge is a port of the excellent Perl module Hash::Merge written by Jens Rehsack.

It merges two arbitrarily deep hashes into a single hash. That is, at any level, it will add non-conflicting key-value pairs from one hash to the other, and follows a set of specific rules when there are key value conflicts (as outlined below). The hash is followed recursively, so that deeply nested hashes that are at the same level will be merged when the parent hashes are merged. Please note that self-referencing hashes, or recursive references, are not handled well by this method.

Values in hashes are considered to be either lists or tuples (ARRAY), dictionaries (HASH), or otherwise are treated as SCALARs. By default, the data passed to the merge function will be cloned using the copy.deepcopy method; however, if necessary, this behavior can be changed to use as many of the original values as possible, using set_clone_behavior.

Because there are a number of possible ways that one may want to merge values when keys are conflicting, hashmerge provides several preset methods for your convenience, as well as a way to define you own.
These are (currently):

Usage

    import hashmerge
    merger = hashmerge.HashMerge()
    print(merger.merge({'d': 123, 'l': [1]}, {'e': 456, 'l': [2]}))

    merger2 = hashmerge.HashMerge(hashmerge.RIGHT_PRECEDENT)
    print(merger2.merge({'d': 123, 'l': [1]}, {'e': 456, 'l': [2]}))

Left Precedence

This is the default behavior.

The values buried in the left hash will never be lost; any values that can be added from the right hash will be attempted.

Right Precedence

Same as Left Precedence, but with the right hash values never being lost

Storage Precedence

If conflicting keys have two different storage mediums, the 'bigger' medium will win; arrays are preferred over scalars, hashes over either. The other medium will try to be fitted in the other, but if this isn't possible, the data is dropped.

Retainment Precedence

No data will be lost; scalars will be joined with arrays, and scalars and arrays will be 'hashified' to fit them into a hash.

Notes

Now you can safely abandon Perl.

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

hashmerge-0.2.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

hashmerge-0.2-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

Details for the file hashmerge-0.2.tar.gz.

File metadata

  • Download URL: hashmerge-0.2.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.6

File hashes

Hashes for hashmerge-0.2.tar.gz
Algorithm Hash digest
SHA256 b46f6be49c2381c8644ff8f3a3cea0b4b12cb35d6a0ce638f468ffe352e9697e
MD5 ff6aab36f82c03d75cb957e0f199ec3d
BLAKE2b-256 b9340b12354828fe0637858af502e2f8828773aafd4faced7293201524296cc5

See more details on using hashes here.

File details

Details for the file hashmerge-0.2-py3-none-any.whl.

File metadata

  • Download URL: hashmerge-0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.7.6

File hashes

Hashes for hashmerge-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 65c78b1b5e7221ce93fd529d803a4486c36138a37d45c0a5ea054fb7bf080f59
MD5 c166ecc31ddcefb5e3b580c65427a9a4
BLAKE2b-256 78c5efbd540a7655dd95beea50a9a1a733bd2f1c66948ea294026b91240f298e

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