Added move utility method.

This commit is contained in:
Fabio Caccamo 2019-10-04 15:54:28 +02:00
parent eace6f7134
commit 4d9a9b6602
2 changed files with 36 additions and 0 deletions

View File

@ -95,6 +95,9 @@ class benedict(IODict, KeypathDict, ParseDict):
for d in dicts: for d in dicts:
dict_util.merge(self, d) dict_util.merge(self, d)
def move(self, key_src, key_dest):
self[key_dest] = self.pop(key_src)
def remove(self, keys, *args): def remove(self, keys, *args):
if isinstance(keys, string_types): if isinstance(keys, string_types):
keys = [keys] keys = [keys]

View File

@ -896,6 +896,39 @@ class BenedictTestCase(unittest.TestCase):
} }
self.assertEqual(d, r) self.assertEqual(d, r)
def test_move(self):
d = {
'a': {
'x': 1,
'y': 1,
},
'b': {
'x': 2,
'y': 2,
},
'c': {
'x': 3,
'y': 3,
},
}
b = benedict(d)
b.move('a', 'c.z')
r = {
'b': {
'x': 2,
'y': 2,
},
'c': {
'x': 3,
'y': 3,
'z': {
'x': 1,
'y': 1,
},
},
}
self.assertEqual(b, r)
def test_pop(self): def test_pop(self):
d = { d = {
'a': 1, 'a': 1,