Skip to content

Commit

Permalink
feat: merge_dicts in init
Browse files Browse the repository at this point in the history
  • Loading branch information
thorwhalen committed Jan 17, 2025
1 parent 36cf176 commit a019ca9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
7 changes: 6 additions & 1 deletion lkj/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
get_by_value, # Get a dictionary from a list of dictionaries by a field value
)
from lkj.funcs import mk_factory
from lkj.dicts import truncate_dict_values, inclusive_subdict, exclusive_subdict
from lkj.dicts import (
truncate_dict_values, # Truncate list and string values in a dictionary
inclusive_subdict, # new dictionary with only the keys in `include`
exclusive_subdict, # new dictionary with only the keys not in `exclude`.
merge_dicts, # Merge multiple dictionaries recursively
)
from lkj.filesys import get_app_data_dir, get_watermarked_dir, enable_sourcing_from_file
from lkj.strings import (
indent_lines, # Indent all lines of a string
Expand Down
6 changes: 4 additions & 2 deletions lkj/dicts.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def truncate_string(value, max_len, marker):
KT = TypeVar("KT") # Key type
VT = TypeVar("VT") # Value type

# Note: Could have all function parameters (recursive_condition, etc.) also take the
# Note: Could have all function parameters (recursive_condition, etc.) also take the
# enumerated index of the mapping as an argument. That would give us even more
# flexibility, but it might be overkill and make the interface more complex.
from typing import Mapping, Callable, TypeVar, Iterable, Tuple
Expand All @@ -120,6 +120,7 @@ def truncate_string(value, max_len, marker):
KT = TypeVar("KT") # Key type
VT = TypeVar("VT") # Value type


def merge_dicts(
*mappings: Mapping[KT, VT],
recursive_condition: Callable[[VT], bool] = lambda v: isinstance(v, Mapping),
Expand Down Expand Up @@ -206,7 +207,8 @@ def merge_dicts(
):
# Recursively merge nested mappings
merged[key] = merge_dicts(
merged[key], value,
merged[key],
value,
recursive_condition=recursive_condition,
conflict_resolver=conflict_resolver,
mapping_constructor=mapping_constructor,
Expand Down

0 comments on commit a019ca9

Please sign in to comment.