diff --git a/benedict/core/clone.py b/benedict/core/clone.py index d17ab62..04df72a 100644 --- a/benedict/core/clone.py +++ b/benedict/core/clone.py @@ -3,5 +3,8 @@ import copy -def clone(obj): - return copy.deepcopy(obj) +def clone(obj, empty=False): + d = copy.deepcopy(obj) + if empty: + d.clear() + return d diff --git a/benedict/core/filter.py b/benedict/core/filter.py index f9e26fe..2563dd9 100644 --- a/benedict/core/filter.py +++ b/benedict/core/filter.py @@ -1,11 +1,12 @@ # -*- coding: utf-8 -*- +from benedict.core import clone + def filter(d, predicate): if not callable(predicate): raise ValueError('predicate argument must be a callable.') - new_dict = d.copy() - new_dict.clear() + new_dict = clone(d, empty=True) keys = list(d.keys()) for key in keys: value = d.get(key, None) diff --git a/benedict/core/flatten.py b/benedict/core/flatten.py index 4c3648e..680d14a 100644 --- a/benedict/core/flatten.py +++ b/benedict/core/flatten.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- +from benedict.core import clone from benedict.utils import type_util @@ -10,8 +11,7 @@ def _flatten_key(base_key, key, separator): def flatten(d, separator='_', **kwargs): - new_dict = d.copy() - new_dict.clear() + new_dict = clone(d, empty=True) keys = list(d.keys()) base_key = kwargs.pop('base_key', '') for key in keys: diff --git a/benedict/core/invert.py b/benedict/core/invert.py index a84ac4c..d3ab7f6 100644 --- a/benedict/core/invert.py +++ b/benedict/core/invert.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- +from benedict.core import clone from benedict.utils import type_util @@ -16,8 +17,7 @@ def _invert_list(d, key, value, flat): def invert(d, flat=False): - new_dict = d.copy() - new_dict.clear() + new_dict = clone(d, empty=True) for key, value in d.items(): if type_util.is_list_or_tuple(value): _invert_list(new_dict, key, value, flat) diff --git a/benedict/core/subset.py b/benedict/core/subset.py index 6c5a452..2d4e224 100644 --- a/benedict/core/subset.py +++ b/benedict/core/subset.py @@ -1,11 +1,11 @@ # -*- coding: utf-8 -*- +from benedict.core import clone from benedict.utils import type_util def subset(d, keys, *args): - new_dict = d.copy() - new_dict.clear() + new_dict = clone(d, empty=True) if type_util.is_string(keys): keys = [keys] keys += args diff --git a/benedict/core/unflatten.py b/benedict/core/unflatten.py index 40c142b..adcdce6 100644 --- a/benedict/core/unflatten.py +++ b/benedict/core/unflatten.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- +from benedict.core import clone from benedict.dicts.keylist import keylist_util from benedict.utils import type_util @@ -12,8 +13,7 @@ def _unflatten_item(key, value, separator): def unflatten(d, separator='_'): - new_dict = d.copy() - new_dict.clear() + new_dict = clone(d, empty=True) keys = list(d.keys()) for key in keys: value = d.get(key, None) diff --git a/tests/core/test_clone.py b/tests/core/test_clone.py index 5b15e73..d6598d7 100644 --- a/tests/core/test_clone.py +++ b/tests/core/test_clone.py @@ -22,3 +22,15 @@ class clone_test_case(unittest.TestCase): o['a']['b']['c'] = 2 self.assertEqual(i['a']['b']['c'], 1) self.assertEqual(o['a']['b']['c'], 2) + + def test_clone_empty(self): + i = { + 'a': { + 'b': { + 'c': 1 + } + } + } + o = _clone(i, empty=True) + self.assertEqual(type(i), type(o)) + self.assertEqual(o, {})