Added empty option to clone utility method.

This commit is contained in:
Fabio Caccamo 2020-09-17 14:09:59 +02:00
parent b49b7e99ee
commit 8065eddf41
7 changed files with 28 additions and 12 deletions

View File

@ -3,5 +3,8 @@
import copy import copy
def clone(obj): def clone(obj, empty=False):
return copy.deepcopy(obj) d = copy.deepcopy(obj)
if empty:
d.clear()
return d

View File

@ -1,11 +1,12 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from benedict.core import clone
def filter(d, predicate): def filter(d, predicate):
if not callable(predicate): if not callable(predicate):
raise ValueError('predicate argument must be a callable.') raise ValueError('predicate argument must be a callable.')
new_dict = d.copy() new_dict = clone(d, empty=True)
new_dict.clear()
keys = list(d.keys()) keys = list(d.keys())
for key in keys: for key in keys:
value = d.get(key, None) value = d.get(key, None)

View File

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from benedict.core import clone
from benedict.utils import type_util from benedict.utils import type_util
@ -10,8 +11,7 @@ def _flatten_key(base_key, key, separator):
def flatten(d, separator='_', **kwargs): def flatten(d, separator='_', **kwargs):
new_dict = d.copy() new_dict = clone(d, empty=True)
new_dict.clear()
keys = list(d.keys()) keys = list(d.keys())
base_key = kwargs.pop('base_key', '') base_key = kwargs.pop('base_key', '')
for key in keys: for key in keys:

View File

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from benedict.core import clone
from benedict.utils import type_util from benedict.utils import type_util
@ -16,8 +17,7 @@ def _invert_list(d, key, value, flat):
def invert(d, flat=False): def invert(d, flat=False):
new_dict = d.copy() new_dict = clone(d, empty=True)
new_dict.clear()
for key, value in d.items(): for key, value in d.items():
if type_util.is_list_or_tuple(value): if type_util.is_list_or_tuple(value):
_invert_list(new_dict, key, value, flat) _invert_list(new_dict, key, value, flat)

View File

@ -1,11 +1,11 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from benedict.core import clone
from benedict.utils import type_util from benedict.utils import type_util
def subset(d, keys, *args): def subset(d, keys, *args):
new_dict = d.copy() new_dict = clone(d, empty=True)
new_dict.clear()
if type_util.is_string(keys): if type_util.is_string(keys):
keys = [keys] keys = [keys]
keys += args keys += args

View File

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from benedict.core import clone
from benedict.dicts.keylist import keylist_util from benedict.dicts.keylist import keylist_util
from benedict.utils import type_util from benedict.utils import type_util
@ -12,8 +13,7 @@ def _unflatten_item(key, value, separator):
def unflatten(d, separator='_'): def unflatten(d, separator='_'):
new_dict = d.copy() new_dict = clone(d, empty=True)
new_dict.clear()
keys = list(d.keys()) keys = list(d.keys())
for key in keys: for key in keys:
value = d.get(key, None) value = d.get(key, None)

View File

@ -22,3 +22,15 @@ class clone_test_case(unittest.TestCase):
o['a']['b']['c'] = 2 o['a']['b']['c'] = 2
self.assertEqual(i['a']['b']['c'], 1) self.assertEqual(i['a']['b']['c'], 1)
self.assertEqual(o['a']['b']['c'], 2) 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, {})