Skip to main content

Cython recursive dictionary merging

Project description

Dict merger

Simple Cython+Python library for merge python dictionaries recursively PyPi

Usage

from dict_merger.static_merger import merge

a = {
    1: 2,
    2: 3,
    3: {1: 1, 2: 3},
    4: 1,
}
b = {
    1: 5,
    3: {1: 2},
    5: 3,
}

merge(a, b) == {
    1: 5,
    2: 3,
    3: {1: 2, 2: 3},
    4: 1,
    5: 3,
}
# P.S: If more than one given dict defines the same key,
# then the one that is later in the argument sequence takes precedence.

API

# dict_merger.static_merger - pure Cython module with static python types
# dict_merger.dynamic_merger - same module, but with dynamic types, works a bit slower

def merge(first_dict: dict, second_dict: dict) -> dict:
    """
    Merge 2 dicts recursively.
    If more than one given dict defines the same key,
    then the one that is later in the argument sequence takes precedence.
    :param first_dict: first dict to merge
    :param second_dict: second dict to merge
    :return: merged dict
    """
    ...

def merge_inplace(first_dict: dict, second_dict: dict) -> None:
    """
    Merge 2 dicts recursively.
    If more than one given dict defines the same key,
    then the one that is later in the argument sequence takes precedence.

    First dict will contain merged version, faster in general.
    :param first_dict: first dict to merge, will contain merged version
    :param second_dict: second dict to merge
    """
    ...

def merge_many(dicts: List[dict]) -> dict:
    """
    Merge list of dicts.
    If more than one given dict defines the same key,
    then the one that is later in the argument sequence takes precedence.
    :param dicts: list of dicts
    :return: merged dict
    """
    ...

def merge_many_inplace(dicts: List[dict]) -> None:
    """
    Merge list of dicts inplace
    If more than one given dict defines the same key,
    then the one that is later in the argument sequence takes precedence.

    :param dicts: list of dicts
    """

Installation

# via pypi
pip install dict_merger

# via git
pip install git+https://github.com/TeaDove/dict_merger.git #@branch/tag

# build locally
git clone https://github.com/TeaDove/dict_merger.git #@branch/tag
cd dict_merger
make install

Performance tests

dict_merger.static_merger - cython mergers with static types
dict_merger.dynamic_merger - cython mergers with dynamic types
tests.pure_python - pure python implementation mergers

test_merge_inplace_random[dict_merger.static_merger] 101.64757675000146
test_merge_inplace_random[dict_merger.dynamic_merger] 111.33557962499981
test_merge_inplace_random[tests.pure_python] 346.21198354100125

test_merge_static[dict_merger.static_merger] 4.421063124998909
test_merge_static[dict_merger.dynamic_merger] 4.379016041999421
test_merge_static[tests.pure_python] 4.7141847090024385

test_merge_inplace_static[dict_merger.static_merger] 0.21805158300048788
test_merge_inplace_static[dict_merger.dynamic_merger] 0.2745717500001774
test_merge_inplace_static[tests.pure_python] 0.6025681659994007

test_merge_many_static[dict_merger.static_merger] 4.533137833001092
test_merge_many_static[dict_merger.dynamic_merger] 4.706059832999017
test_merge_many_static[tests.pure_python] 5.675890833001176

test_merge_many_inpace_static[dict_merger.static_merger] 0.42021320900312276
test_merge_many_inpace_static[dict_merger.dynamic_merger] 0.572701542001596
test_merge_many_inpace_static[tests.pure_python] 1.5192962910005008

Credits

For any questions, suggestions etc. write peter.ibragimov@gmail.com or to telegram: @teadove

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

dict_merger-0.0.3.tar.gz (95.9 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

dict_merger-0.0.3-cp310-cp310-macosx_11_0_arm64.whl (46.0 kB view details)

Uploaded CPython 3.10macOS 11.0+ ARM64

dict_merger-0.0.3-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (201.4 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ ARM64

dict_merger-0.0.3-cp38-cp38-macosx_11_0_arm64.whl (47.3 kB view details)

Uploaded CPython 3.8macOS 11.0+ ARM64

dict_merger-0.0.3-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (209.4 kB view details)

Uploaded CPython 3.6mmanylinux: glibc 2.5+ x86-64

File details

Details for the file dict_merger-0.0.3.tar.gz.

File metadata

  • Download URL: dict_merger-0.0.3.tar.gz
  • Upload date:
  • Size: 95.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.64.1 CPython/3.8.9

File hashes

Hashes for dict_merger-0.0.3.tar.gz
Algorithm Hash digest
SHA256 d10581de797357039731dd2a6f5718c779eb0d1a3146f5483336fef8e732b8a5
MD5 cedf9c670c508227fd1965272e4aa5b5
BLAKE2b-256 2734d8cb7177327524de4d9c0374e2f1e299c5ac26850cda2ebe80e528fd91cd

See more details on using hashes here.

File details

Details for the file dict_merger-0.0.3-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

  • Download URL: dict_merger-0.0.3-cp310-cp310-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 46.0 kB
  • Tags: CPython 3.10, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.64.1 CPython/3.8.9

File hashes

Hashes for dict_merger-0.0.3-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3a9606143f63419fe42a7ca377418593691d3e3c8dda5d9154043e7e2e19938b
MD5 6963781c8d79dfcf72405968607cd35e
BLAKE2b-256 f06d9a05e53381e93964263dd0d7ed175a3d9c531d899050881b506afa5dc2c5

See more details on using hashes here.

File details

Details for the file dict_merger-0.0.3-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for dict_merger-0.0.3-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 a72218660342bd3c2d2dcbf590057bd83afcea0b68c5f55596ac016b79e766ee
MD5 83d9c76ac6fb8b4054aa589d464aaa3d
BLAKE2b-256 e57b1413f4b83d08198adf9978bbd0129507a12f323181d2af889b4bd1dac331

See more details on using hashes here.

File details

Details for the file dict_merger-0.0.3-cp38-cp38-macosx_11_0_arm64.whl.

File metadata

  • Download URL: dict_merger-0.0.3-cp38-cp38-macosx_11_0_arm64.whl
  • Upload date:
  • Size: 47.3 kB
  • Tags: CPython 3.8, macOS 11.0+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.64.1 CPython/3.8.9

File hashes

Hashes for dict_merger-0.0.3-cp38-cp38-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 26cc92641eba0f7f52ad4deede64c8a6a4725c525045f8f6f9fbf9d3b86ec638
MD5 ef9021c169c970045ecead6afc80adfa
BLAKE2b-256 d448500d86ecb04e42804d62457424c21039da5cf8fda0c9b5914d220eba7589

See more details on using hashes here.

File details

Details for the file dict_merger-0.0.3-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl.

File metadata

  • Download URL: dict_merger-0.0.3-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
  • Upload date:
  • Size: 209.4 kB
  • Tags: CPython 3.6m, manylinux: glibc 2.5+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.3 readme-renderer/34.0 requests/2.20.0 requests-toolbelt/0.9.1 urllib3/1.26.13 tqdm/4.64.1 importlib-metadata/4.8.3 keyring/23.4.1 rfc3986/1.5.0 colorama/0.4.5 CPython/3.6.8

File hashes

Hashes for dict_merger-0.0.3-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 8cedecfb0cca86363df456586aa6d6ef4594d4a587ee20433a873f3fcfa400e5
MD5 0739e9c703675de94dc8b1a9ea5d8d09
BLAKE2b-256 3699c911d0686a48d345bec8b9478b343fcb78ba6319a0e829cf6b03bbb25bdd

See more details on using hashes here.

Supported by

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