Improved remove method to accept a single key or args.

This commit is contained in:
Fabio Caccamo 2019-10-04 15:51:29 +02:00
parent 3136b57fdb
commit 50c0439f9b
2 changed files with 36 additions and 2 deletions

View File

@ -93,7 +93,10 @@ class benedict(IODict, KeypathDict, ParseDict):
for d in dicts:
dict_util.merge(self, d)
def remove(self, keys):
def remove(self, keys, *args):
if isinstance(keys, string_types):
keys = [keys]
keys += args
for key in keys:
try:
del self[key]

View File

@ -911,7 +911,21 @@ class BenedictTestCase(unittest.TestCase):
self.assertEqual(b.pop('b.c'), 2)
# self.assertTrue(isinstance(b.pop('b.d'), benedict))
def test_remove(self):
def test_remove_with_key(self):
d = {
'a': 1,
'b': 2,
'c': '4',
}
b = benedict(d)
b.remove('c')
r = {
'a': 1,
'b': 2,
}
self.assertEqual(b, r)
def test_remove_with_keys_list(self):
d = {
'a': 1,
'b': 2,
@ -928,6 +942,23 @@ class BenedictTestCase(unittest.TestCase):
}
self.assertEqual(b, r)
def test_remove_with_keys_args(self):
d = {
'a': 1,
'b': 2,
'c': '4',
'e': '5',
'f': 6,
'g': 7,
}
b = benedict(d)
b.remove('c', 'e', 'f', 'g', 'x', 'y', 'z')
r = {
'a': 1,
'b': 2,
}
self.assertEqual(b, r)
def test_remove_with_keypath(self):
d = {
'a': {