From da92c9612b1b6b717497d422362ebd7c1e16629e Mon Sep 17 00:00:00 2001
From: Kenzo-Hugo Hillion <kenzo-hugo.hillion1@pasteur.fr>
Date: Tue, 16 Jul 2019 13:58:54 +0200
Subject: [PATCH] add option to keep original source dict

---
 backend/metagenedb/utils/dict_operations.py      | 5 +++--
 backend/metagenedb/utils/test_dict_operations.py | 9 +++++++++
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/backend/metagenedb/utils/dict_operations.py b/backend/metagenedb/utils/dict_operations.py
index f8cf801..5fab860 100644
--- a/backend/metagenedb/utils/dict_operations.py
+++ b/backend/metagenedb/utils/dict_operations.py
@@ -4,7 +4,7 @@ logging.basicConfig(level=logging.INFO)
 _LOGGER = logging.getLogger(__name__)
 
 
-def extract_dict(source_dict, keys):
+def extract_dict(source_dict, keys, keep_original_source=False):
     """
     Extract a dict from a given dict based on a given set of keys
     """
@@ -12,7 +12,8 @@ def extract_dict(source_dict, keys):
     for key in keys:
         try:
             extracted_dict[key] = source_dict[key]
-            del source_dict[key]
+            if not keep_original_source:
+                del source_dict[key]
         except KeyError:
             _LOGGER.warning(f"[{key}] is not found in the source dict, extraction skipped for this key.")
     return extracted_dict
diff --git a/backend/metagenedb/utils/test_dict_operations.py b/backend/metagenedb/utils/test_dict_operations.py
index 7de1f3f..155156b 100644
--- a/backend/metagenedb/utils/test_dict_operations.py
+++ b/backend/metagenedb/utils/test_dict_operations.py
@@ -22,3 +22,12 @@ class TestExtractDict(TestCase):
         test_extract_dict = extract_dict(source_dict, extract_keys)
         self.assertDictEqual(source_dict, expected_source_dict)
         self.assertDictEqual(test_extract_dict, expected_extract_dict)
+
+    def test_extract_dict_original_source(self):
+        source_dict = {'a': 1, 'b': 2}
+        extract_keys = ['b']
+        expected_source_dict = {'a': 1, 'b': 2}
+        expected_extract_dict = {'b': 2}
+        test_extract_dict = extract_dict(source_dict, extract_keys, keep_original_source=True)
+        self.assertDictEqual(source_dict, expected_source_dict)
+        self.assertDictEqual(test_extract_dict, expected_extract_dict)
-- 
GitLab