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.
Release files for hashmerge 0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| hashmerge-0.2.tar.gz | 3.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| hashmerge-0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 7.5 kB
Release files / hashmerge-0.2.tar.gz
| Download URL | hashmerge-0.2.tar.gz |
|---|---|
| Size | 3.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b46f6be49c2381c8644ff8f3a3cea0b4b12cb35d6a0ce638f468ffe352e9697e
|
|
BLAKE2b-256 checksum How to use checksums |
b9340b12354828fe0637858af502e2f8828773aafd4faced7293201524296cc5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / hashmerge-0.2-py3-none-any.whl
| Download URL | hashmerge-0.2-py3-none-any.whl |
|---|---|
| Size | 4.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
65c78b1b5e7221ce93fd529d803a4486c36138a37d45c0a5ea054fb7bf080f59
|
|
BLAKE2b-256 checksum How to use checksums |
78c5efbd540a7655dd95beea50a9a1a733bd2f1c66948ea294026b91240f298e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|