deepmerge
Class info¶
Classes¶
Name | Children | Inherits |
---|---|---|
DeepMerger jinjarope.deepmerge A class that handles deep merging of data structures. |
🛈 DocStrings¶
Module for deep merging of data structures with customizable merge strategies.
DeepMerger
¶
A class that handles deep merging of data structures.
The merger can be customized by providing different merge strategies for different types.
Attributes:
Name | Type | Description |
---|---|---|
mergers |
dict[type[Any], Callable[..., Any]]
|
A dictionary mapping types to their corresponding merge functions |
Example
merger = DeepMerger()
source = {"a": {"b": 1}}
target = {"a": {"c": 2}}
merger.merge(source, target)
# {'a': {'b': 1, 'c': 2}}
Source code in src/jinjarope/deepmerge.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
|
__init__
¶
Initialize the DeepMerger with custom merge strategies.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mergers
|
dict[type[Any], Callable[..., Any]] | None
|
Optional dictionary of type-specific merge functions |
None
|
Source code in src/jinjarope/deepmerge.py
95 96 97 98 99 100 101 102 103 104 |
|
merge
¶
merge(source: T, target: T) -> T
Merge two objects of the same type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
source
|
T
|
The source object whose values take precedence |
required |
target
|
T
|
The target object to merge into |
required |
Returns:
Type | Description |
---|---|
T
|
The merged object |
Raises:
Type | Description |
---|---|
TypeError
|
If the types cannot be merged |
Source code in src/jinjarope/deepmerge.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
|
merge_dict
¶
merge_dict(merger: DeepMerger, source: Mapping, target: Mapping) -> Mapping
Merge two mappings recursively.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
merger
|
DeepMerger
|
The DeepMerger instance handling the merge operation |
required |
source
|
Mapping
|
The source mapping whose values take precedence |
required |
target
|
Mapping
|
The target mapping to merge into |
required |
Returns:
Type | Description |
---|---|
Mapping
|
A new dictionary containing the merged key-value pairs |
Example
merger = DeepMerger()
source = {"a": {"b": 1}}
target = {"a": {"c": 2}}
merge_dict(merger, source, target)
# {'a': {'b': 1, 'c': 2}}
Source code in src/jinjarope/deepmerge.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
merge_list
¶
merge_list(merger: DeepMerger, source: list, target: list) -> list
Concatenate two lists.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
merger
|
DeepMerger
|
The DeepMerger instance handling the merge operation |
required |
source
|
list
|
The source list to append |
required |
target
|
list
|
The target list to merge into |
required |
Returns:
Type | Description |
---|---|
list
|
A new list containing all elements from both lists |
Example
merger = DeepMerger()
merge_list(merger, [3, 4], [1, 2])
# [1, 2, 3, 4]
Source code in src/jinjarope/deepmerge.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
|