### Installation Source: https://github.com/clarketm/mergedeep/blob/master/docs/source/index.md Install the mergedeep library using pip. ```APIDOC ## Installation ```bash $ pip install mergedeep ``` ``` -------------------------------- ### Install mergedeep Source: https://github.com/clarketm/mergedeep/blob/master/README.md Install the mergedeep package using pip. ```bash pip install mergedeep ``` -------------------------------- ### Install mergedeep using conda Source: https://context7.com/clarketm/mergedeep/llms.txt Install the mergedeep library using conda. This is an alternative installation method, particularly useful for managing environments and dependencies. ```bash conda install -c conda-forge mergedeep ``` -------------------------------- ### Complex Nested Merge Example Source: https://context7.com/clarketm/mergedeep/llms.txt Illustrates a complex deep merge scenario with multiple nested levels, useful for configuration merging. ```python from mergedeep import merge # Complex nested merge with multiple levels config_defaults = { "database": { "host": "localhost", "port": 5432, "options": {"timeout": 30, "retries": 3} }, "logging": {"level": "INFO"} } config_overrides = { "database": { "host": "production.db.com", "options": {"timeout": 60} } } final_config = merge({}, config_defaults, config_overrides) print(final_config) # { # "database": { # "host": "production.db.com", # "port": 5432, # "options": {"timeout": 60, "retries": 3} # }, # "logging": {"level": "INFO"} # } ``` -------------------------------- ### Strategy.TYPESAFE_REPLACE Merge Example Source: https://context7.com/clarketm/mergedeep/llms.txt Demonstrates the TYPESAFE_REPLACE strategy, which raises a TypeError if destination and source values have different types. Otherwise, it behaves like REPLACE. ```python from mergedeep import merge, Strategy # Successful merge with matching types dst = {"key": [1, 2], "name": "test"} src = {"key": [3, 4], "name": "updated"} merge(dst, src, strategy=Strategy.TYPESAFE_REPLACE) # Same as: merge(dst, src, strategy=Strategy.TYPESAFE) print(dst) # {"key": [3, 4], "name": "updated"} ``` -------------------------------- ### Strategy.ADDITIVE Merge Example Source: https://context7.com/clarketm/mergedeep/llms.txt Illustrates the ADDITIVE strategy, which combines collection types (lists, tuples, sets, Counters) instead of replacing them. Falls back to REPLACE for incompatible types. ```python from collections import Counter from mergedeep import merge, Strategy # Combining lists dst = {"tags": ["python", "library"]} src = {"tags": ["merge", "deep"]} merge(dst, src, strategy=Strategy.ADDITIVE) print(dst) # {"tags": ["python", "library", "merge", "deep"]} ``` ```python from collections import Counter from mergedeep import merge, Strategy # Combining sets dst = {"permissions": {"read", "write"}} src = {"permissions": {"execute", "delete"}} merge(dst, src, strategy=Strategy.ADDITIVE) print(dst) # {"permissions": {"read", "write", "execute", "delete"}} ``` ```python from collections import Counter from mergedeep import merge, Strategy # Combining tuples dst = {"coordinates": (1, 2)} src = {"coordinates": (3, 4)} merge(dst, src, strategy=Strategy.ADDITIVE) print(dst) # {"coordinates": (1, 2, 3, 4)} ``` ```python from collections import Counter from mergedeep import merge, Strategy # Combining Counters dst = {"word_count": Counter({"a": 1, "b": 1})} src = {"word_count": Counter({"a": 1, "c": 1})} merge(dst, src, strategy=Strategy.ADDITIVE) print(dst) # {"word_count": Counter({"a": 2, "b": 1, "c": 1})} ``` ```python from collections import Counter from mergedeep import merge, Strategy # Complex additive merge dst = { "key": [1, 2], "count": Counter({"a": 1, "b": 1}) } src = { "key": [3, 4], "count": Counter({"a": 1, "c": 1}) } merge(dst, src, strategy=Strategy.ADDITIVE) print(dst) # {"key": [1, 2, 3, 4], "count": Counter({"a": 2, "b": 1, "c": 1})} ``` -------------------------------- ### Safe Configuration Merging with TYPESAFE Source: https://context7.com/clarketm/mergedeep/llms.txt Shows a successful merge of default and user configurations using Strategy.TYPESAFE, where types match. ```python default_config = { "port": 8080, "debug": False, "hosts": ["localhost"] } user_config = { "port": 9000, "debug": True, "hosts": ["example.com"] } # This will succeed because types match merge(default_config, user_config, strategy=Strategy.TYPESAFE) print(default_config) # {"port": 9000, "debug": True, "hosts": ["example.com"]} ``` -------------------------------- ### Basic Deep Merge with merge() Source: https://context7.com/clarketm/mergedeep/llms.txt Demonstrates a basic deep merge operation using the `merge` function. It creates a new dictionary without mutating the source dictionaries. ```python from mergedeep import merge # Basic deep merge - creates new dict without mutating sources a = {"keyA": 1} b = {"keyB": {"sub1": 10}} c = {"keyB": {"sub2": 20}} merged = merge({}, a, b, c) print(merged) # {"keyA": 1, "keyB": {"sub1": 10, "sub2": 20}} ``` -------------------------------- ### Additive Merge with Matching Types Source: https://context7.com/clarketm/mergedeep/llms.txt Illustrates a successful additive merge using Strategy.TYPESAFE_ADDITIVE when destination and source values are of matching collection types. ```python from collections import Counter from mergedeep import merge, Strategy # Successful additive merge with matching types dst = { "tags": ["python"], "scores": Counter({"a": 1}) } src = { "tags": ["deep"], "scores": Counter({"b": 2}) } merge(dst, src, strategy=Strategy.TYPESAFE_ADDITIVE) print(dst) # {"tags": ["python", "deep"], "scores": Counter({"a": 1, "b": 2})} ``` -------------------------------- ### mergedeep Strategy Enum Reference Source: https://context7.com/clarketm/mergedeep/llms.txt Lists all available merge strategies in the Strategy enum and demonstrates how to enumerate them. ```python from mergedeep import Strategy # Available strategies Strategy.REPLACE # Default: replace destination with source Strategy.ADDITIVE # Combine collections (list, tuple, set, Counter) Strategy.TYPESAFE # Alias for TYPESAFE_REPLACE Strategy.TYPESAFE_REPLACE # Type check + replace Strategy.TYPESAFE_ADDITIVE # Type check + additive # Enumerate all strategies for strategy in Strategy: print(f"{strategy.name}: {strategy.value}") # REPLACE: 0 # ADDITIVE: 1 # TYPESAFE: 2 # TYPESAFE_REPLACE: 3 # TYPESAFE_ADDITIVE: 4 ``` -------------------------------- ### Replace Merge Strategy Source: https://github.com/clarketm/mergedeep/blob/master/README.md When keys are the same, the destination value is replaced by the source value. This is the default strategy. With multiple sources, the rightmost source's value is used. ```python # When `destination` and `source` keys are the same, replace the `destination` value with one from `source` (default). # Note: with multiple sources, the `last` (i.e. rightmost) source value will be what appears in the merged result. from mergedeep import merge, Strategy dst = {"key": [1, 2]} src = {"key": [3, 4]} merge(dst, src, strategy=Strategy.REPLACE) # same as: merge(dst, src) print(dst) # {"key": [3, 4]} ``` -------------------------------- ### Usage: Deep merge into an existing dict Source: https://github.com/clarketm/mergedeep/blob/master/docs/source/index.md Demonstrates how to deep merge into an existing dictionary, modifying it in place. ```APIDOC ## Usage: Deep merge into an existing dict ```python from mergedeep import merge a = {"keyA": 1} b = {"keyB": {"sub1": 10}} c = {"keyB": {"sub2": 20}} merge(a, b, c) print(a) # {“keyA”: 1, “keyB”: {“sub1”: 10, “sub2”: 20}} ``` ``` -------------------------------- ### Additive merge strategy Source: https://github.com/clarketm/mergedeep/blob/master/docs/source/index.md Extends destination collections with values from source collections. ```python3 # When `destination and source values are both the same additive collection type, extend destination by adding values from source. # Additive collection types include: list, tuple, set, and Counter # Note: if the values are not additive collections of the same type, then fallback to a REPLACE merge. from mergedeep import merge, Strategy dst = {“key”: [1, 2], “count”: Counter({“a”: 1, “b”: 1})} src = {“key”: [3, 4], “count”: Counter({“a”: 1, “c”: 1})} merge(dst, src, strategy=Strategy.ADDITIVE) print(dst) # {“key”: [1, 2, 3, 4], “count”: Counter({“a”: 2, “b”: 1, “c”: 1})} ``` -------------------------------- ### Merge Strategies: Additive Source: https://github.com/clarketm/mergedeep/blob/master/docs/source/index.md When destination and source values are both additive collection types (list, tuple, set, Counter), their values are combined. Otherwise, it falls back to REPLACE. ```APIDOC * Additive > Strategy.ADDITIVE ```python # When `destination and source values are both the same additive collection type, extend destination by adding values from source. # Additive collection types include: list, tuple, set, and Counter # Note: if the values are not additive collections of the same type, then fallback to a REPLACE merge. from mergedeep import merge, Strategy from collections import Counter dst = {"key": [1, 2], "count": Counter({"a": 1, "b": 1})} src = {"key": [3, 4], "count": Counter({"a": 1, "c": 1})} merge(dst, src, strategy=Strategy.ADDITIVE) print(dst) # {“key”: [1, 2, 3, 4], “count”: Counter({“a”: 2, “b”: 1, “c”: 1})} ``` ``` -------------------------------- ### Merge Strategies: Replace Source: https://github.com/clarketm/mergedeep/blob/master/docs/source/index.md The default merge strategy. When keys are the same, the destination value is replaced by the source value. With multiple sources, the rightmost source's value prevails. ```APIDOC ### Merge strategies: * Replace (*default*) > Strategy.REPLACE ```python # When `destination and source keys are the same, replace the destination value with one from source (default). # Note: with multiple sources, the last (i.e. rightmost) source value will be what appears in the merged result. from mergedeep import merge, Strategy dst = {"key": [1, 2]} src = {"key": [3, 4]} merge(dst, src, strategy=Strategy.REPLACE) # same as: merge(dst, src) print(dst) # {“key”: [3, 4]} ``` ``` -------------------------------- ### Replace merge strategy Source: https://github.com/clarketm/mergedeep/blob/master/docs/source/index.md Default strategy where source values replace destination values on key collision. ```python3 # When `destination and source keys are the same, replace the destination value with one from source (default). # Note: with multiple sources, the last (i.e. rightmost) source value will be what appears in the merged result. from mergedeep import merge, Strategy dst = {“key”: [1, 2]} src = {“key”: [3, 4]} merge(dst, src, strategy=Strategy.REPLACE) # same as: merge(dst, src) print(dst) # {“key”: [3, 4]} ``` -------------------------------- ### Usage: Deep merge without mutating source dicts Source: https://github.com/clarketm/mergedeep/blob/master/docs/source/index.md Demonstrates how to perform a deep merge without modifying the original source dictionaries. ```APIDOC ## Usage: Deep merge without mutating source dicts ```text merge(destination: MutableMapping, *sources: Mapping, strategy: Strategy = Strategy.REPLACE) -> MutableMapping ``` Deep merge without mutating the source dicts. ```python from mergedeep import merge a = {"keyA": 1} b = {"keyB": {"sub1": 10}} c = {"keyB": {"sub2": 20}} merged = merge({}, a, b, c) print(merged) # {“keyA”: 1, “keyB”: {“sub1”: 10, “sub2”: 20}} ``` ``` -------------------------------- ### Typesafe Additive Strategy Source: https://github.com/clarketm/mergedeep/blob/master/README.md When destination and source values are of different types, raise TypeError. Otherwise, perform an ADDITIVE merge. ```python # When `destination` and `source` values are of different types, raise `TypeError`. Otherwise, perform a `ADDITIVE` merge. from mergedeep import merge, Strategy dst = {"key": [1, 2]} src = {"key": {3, 4}} merge(dst, src, strategy=Strategy.TYPESAFE_ADDITIVE) # TypeError: destination type: differs from source type: for key: "key" ``` -------------------------------- ### Additive Merge Strategy Source: https://github.com/clarketm/mergedeep/blob/master/README.md When destination and source values are additive collection types (list, tuple, set, Counter), extend the destination by adding values from the source. Otherwise, it falls back to REPLACE. ```python # When `destination` and `source` values are both the same additive collection type, extend `destination` by adding values from `source`. # Additive collection types include: `list`, `tuple`, `set`, and `Counter` # Note: if the values are not additive collections of the same type, then fallback to a `REPLACE` merge. from mergedeep import merge, Strategy dst = {"key": [1, 2], "count": Counter({"a": 1, "b": 1})} src = {"key": [3, 4], "count": Counter({"a": 1, "c": 1})} merge(dst, src, strategy=Strategy.ADDITIVE) print(dst) # {"key": [1, 2, 3, 4], "count": Counter({"a": 2, "b": 1, "c": 1})} ``` -------------------------------- ### Complex Typesafe Additive Merge Source: https://context7.com/clarketm/mergedeep/llms.txt Shows a complex additive merge with multiple collection types using Strategy.TYPESAFE_ADDITIVE. ```python # Complex typesafe additive merge dst = { "users": ["alice", "bob"], "permissions": {"read", "write"}, "stats": Counter({"visits": 100}), "config": {"nested": {"items": (1, 2)}} } src = { "users": ["charlie"], "permissions": {"execute"}, "stats": Counter({"visits": 50, "clicks": 25}), "config": {"nested": {"items": (3, 4)}} } merge(dst, src, strategy=Strategy.TYPESAFE_ADDITIVE) print(dst) # { # "users": ["alice", "bob", "charlie"], # "permissions": {"read", "write", "execute"}, # "stats": Counter({"visits": 150, "clicks": 25}), # "config": {"nested": {"items": (1, 2, 3, 4)}} # } ``` -------------------------------- ### Merge function signature Source: https://github.com/clarketm/mergedeep/blob/master/docs/source/index.md The signature for the merge function. ```text merge(destination: MutableMapping, *sources: Mapping, strategy: Strategy = Strategy.REPLACE) -> MutableMapping ``` -------------------------------- ### Typesafe Replace Strategy Source: https://github.com/clarketm/mergedeep/blob/master/README.md When destination and source values are of different types, raise TypeError. Otherwise, perform a REPLACE merge. This strategy is also aliased as Strategy.TYPESAFE. ```python # When `destination` and `source` values are of different types, raise `TypeError`. Otherwise, perform a `REPLACE` merge. from mergedeep import merge, Strategy dst = {"key": [1, 2]} src = {"key": {3, 4}} merge(dst, src, strategy=Strategy.TYPESAFE_REPLACE) # same as: `Strategy.TYPESAFE` # TypeError: destination type: differs from source type: for key: "key" ``` -------------------------------- ### Basic Deep Merge Source: https://github.com/clarketm/mergedeep/blob/master/README.md Deep merge dictionaries without mutating source dictionaries. Creates a new dictionary for the result. ```python from mergedeep import merge a = {"keyA": 1} b = {"keyB": {"sub1": 10}} c = {"keyB": {"sub2": 20}} merged = merge({}, a, b, c) print(merged) # {"keyA": 1, "keyB": {"sub1": 10, "sub2": 20}} ``` -------------------------------- ### Typesafe replace merge strategy Source: https://github.com/clarketm/mergedeep/blob/master/docs/source/index.md Raises TypeError if types differ, otherwise performs a replace merge. ```python3 # When `destination and source values are of different types, raise TypeError. Otherwise, perform a REPLACE merge. from mergedeep import merge, Strategy dst = {“key”: [1, 2]} src = {“key”: {3, 4}} merge(dst, src, strategy=Strategy.TYPESAFE_REPLACE) # same as: Strategy.TYPESAFE # TypeError: destination type: differs from source type: for key: “key” ``` -------------------------------- ### TypeError with TYPESAFE_ADDITIVE Source: https://context7.com/clarketm/mergedeep/llms.txt Demonstrates a TypeError when attempting to merge dictionaries with differing types for the same key using Strategy.TYPESAFE_ADDITIVE. ```python # TypeError when types differ dst = {"key": [1, 2]} src = {"key": {3, 4}} try: merge(dst, src, strategy=Strategy.TYPESAFE_ADDITIVE) except TypeError as e: print(e) # destination type: differs from source type: for key: "key" ``` -------------------------------- ### Merge Strategies: Typesafe Replace Source: https://github.com/clarketm/mergedeep/blob/master/docs/source/index.md Raises a TypeError if destination and source values are of different types. Otherwise, performs a REPLACE merge. ```APIDOC * Typesafe replace > Strategy.TYPESAFE_REPLACE or Strategy.TYPESAFE ```python # When `destination and source values are of different types, raise TypeError. Otherwise, perform a REPLACE merge. from mergedeep import merge, Strategy dst = {"key": [1, 2]} src = {"key": {3, 4}} merge(dst, src, strategy=Strategy.TYPESAFE_REPLACE) # same as: Strategy.TYPESAFE # TypeError: destination type: differs from source type: for key: “key” ``` ``` -------------------------------- ### Merge Strategies: Typesafe Additive Source: https://github.com/clarketm/mergedeep/blob/master/docs/source/index.md Raises a TypeError if destination and source values are of different types. Otherwise, performs an ADDITIVE merge. ```APIDOC * Typesafe additive > Strategy.TYPESAFE_ADDITIVE ```python # When `destination and source values are of different types, raise TypeError. Otherwise, perform a ADDITIVE merge. from mergedeep import merge, Strategy dst = {"key": [1, 2]} src = {"key": {3, 4}} merge(dst, src, strategy=Strategy.TYPESAFE_ADDITIVE) # TypeError: destination type: differs from source type: for key: “key” ``` ``` -------------------------------- ### Typesafe additive merge strategy Source: https://github.com/clarketm/mergedeep/blob/master/docs/source/index.md Raises TypeError if types differ, otherwise performs an additive merge. ```python3 # When `destination and source values are of different types, raise TypeError. Otherwise, perform a ADDITIVE merge. from mergedeep import merge, Strategy dst = {“key”: [1, 2]} src = {“key”: {3, 4}} merge(dst, src, strategy=Strategy.TYPESAFE_ADDITIVE) # TypeError: destination type: differs from source type: for key: “key” ``` -------------------------------- ### TypeError with TYPESAFE_REPLACE Source: https://context7.com/clarketm/mergedeep/llms.txt Demonstrates a TypeError when attempting to merge dictionaries with differing types for the same key using Strategy.TYPESAFE_REPLACE. ```python dst = {"key": [1, 2]} src = {"key": {3, 4}} # set instead of list try: merge(dst, src, strategy=Strategy.TYPESAFE_REPLACE) except TypeError as e: print(e) # destination type: differs from source type: for key: "key" ``` -------------------------------- ### Deep merge into existing dictionary Source: https://github.com/clarketm/mergedeep/blob/master/docs/source/index.md Perform a deep merge directly into an existing dictionary. ```python3 from mergedeep import merge a = {“keyA”: 1} b = {“keyB”: {“sub1”: 10}} c = {“keyB”: {“sub2”: 20}} merge(a, b, c) print(a) # {“keyA”: 1, “keyB”: {“sub1”: 10, “sub2”: 20}} ``` -------------------------------- ### Deep Merge into Existing Dictionary Source: https://github.com/clarketm/mergedeep/blob/master/README.md Deep merge dictionaries into an existing destination dictionary, mutating the destination. ```python from mergedeep import merge a = {"keyA": 1} b = {"keyB": {"sub1": 10}} c = {"keyB": {"sub2": 20}} merge(a, b, c) print(a) # {"keyA": 1, "keyB": {"sub1": 10, "sub2": 20}} ``` -------------------------------- ### Deep merge without mutation Source: https://github.com/clarketm/mergedeep/blob/master/docs/source/index.md Perform a deep merge into a new dictionary without modifying the source dictionaries. ```python3 from mergedeep import merge a = {“keyA”: 1} b = {“keyB”: {“sub1”: 10}} c = {“keyB”: {“sub2”: 20}} merged = merge({}, a, b, c) print(merged) # {“keyA”: 1, “keyB”: {“sub1”: 10, “sub2”: 20}} ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.