From 4d9a9b6602643a506ad2e5e951824d02914377a3 Mon Sep 17 00:00:00 2001 From: Fabio Caccamo Date: Fri, 4 Oct 2019 15:54:28 +0200 Subject: [PATCH] Added move utility method. --- benedict/dicts/__init__.py | 3 +++ tests/test_benedict.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/benedict/dicts/__init__.py b/benedict/dicts/__init__.py index 3dfac0d..70ded17 100644 --- a/benedict/dicts/__init__.py +++ b/benedict/dicts/__init__.py @@ -95,6 +95,9 @@ class benedict(IODict, KeypathDict, ParseDict): for d in dicts: dict_util.merge(self, d) + def move(self, key_src, key_dest): + self[key_dest] = self.pop(key_src) + def remove(self, keys, *args): if isinstance(keys, string_types): keys = [keys] diff --git a/tests/test_benedict.py b/tests/test_benedict.py index 0c43f20..4feab54 100644 --- a/tests/test_benedict.py +++ b/tests/test_benedict.py @@ -896,6 +896,39 @@ class BenedictTestCase(unittest.TestCase): } 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): d = { 'a': 1,