Skip to content

Commit

Permalink
Replaces Merging dict func (#731)
Browse files Browse the repository at this point in the history
* refactor: update dict

* fix: 3.10 issue

* fix: spelling
  • Loading branch information
JGSweets committed Dec 2, 2022
1 parent 5ce6fe2 commit 6d5cc38
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ dataprofiler/labelers/embeddings/glove-reduced-64D.txt
*.pkl
*.whl
*.egg
.DS_Store
**/.DS_Store

test/__pycache__/

Expand Down
8 changes: 6 additions & 2 deletions dataprofiler/profilers/profile_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,9 @@ def diff(self, other_profile: StructuredColProfiler, options: Dict = None) -> Di
comp_diff = self.profiles[key].diff(
other_profile.profiles[key], options=options
)
utils.dict_merge(unordered_profile, comp_diff)
unordered_profile = utils.recursive_dict_update(
unordered_profile, comp_diff
)

name = self.name
if isinstance(self.name, np.integer):
Expand Down Expand Up @@ -323,7 +325,9 @@ def report(self, remove_disabled_flag: bool = False) -> OrderedDict:
"""Return profile."""
unordered_profile: Dict = dict()
for profile in self.profiles.values():
utils.dict_merge(unordered_profile, profile.report(remove_disabled_flag))
unordered_profile = utils.recursive_dict_update(
unordered_profile, profile.report(remove_disabled_flag)
)

name = self.name
if isinstance(self.name, np.integer):
Expand Down
50 changes: 15 additions & 35 deletions dataprofiler/profilers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
Tuple,
TypeVar,
Union,
cast,
overload,
)

Expand All @@ -37,43 +38,22 @@
from dataprofiler import profilers, settings


def dict_merge(dct: Dict, merge_dct: Dict) -> None:
# Recursive dictionary merge
# Copyright (C) 2016 Paul Durivage <pauldurivage+github@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
Merge dict recursively.
Inspired by :meth:``dict.update()``, instead of updating only
top-level keys, dict_merge recurses down into dicts nested
to an arbitrary depth, updating keys. The ``merge_dct`` is
merged into ``dct``.
:param dct: dict onto which the merge is executed
:param merge_dct: dct merged into dct
:return: None
"""
for k in merge_dct.keys():
if (
k in dct
and isinstance(dct[k], dict)
and isinstance(merge_dct[k], collections.abc.Mapping)
def recursive_dict_update(d: Dict, update_d: Dict) -> Dict:
"""
Recursive updates nested dictionaries. Updating d with update_d.
:param d: dict which gets updated with update_d
:param update_d: dict to update d with
:return: updated dict
"""
for k, v in update_d.items():
if isinstance(v, collections.abc.Mapping) and isinstance(
d.get(k, None), collections.abc.Mapping
):
dict_merge(dct[k], merge_dct[k])
d[k] = recursive_dict_update(d.get(k, {}), cast(Dict, v))
else:
dct[k] = merge_dct[k]
d[k] = v
return d


class KeyDict(collections.defaultdict):
Expand Down

0 comments on commit 6d5cc38

Please sign in to comment.