Skip to content
Snippets Groups Projects
Commit 856e736d authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

more tests

parent d10a10d8
No related branches found
No related tags found
No related merge requests found
......@@ -9,21 +9,25 @@ from .typecheck import typed
def _merge(v1, v2):
if isinstance(v1, list) and isinstance(v2, list):
return v1 + v2
elif isinstance(v1, dict) and isinstance(v2, dict):
if isinstance(v1, dict) and isinstance(v2, dict):
v1.update(v2)
return v1
else:
raise TypeError
if isinstance(v1, list) and isinstance(v2, list):
v1.extend(v2)
return v2
raise TypeError
@typed(dict, dict, returns=dict)
def merge_dicts(first, second):
for key in second.keys():
try:
first[key] = _merge(first[key], second[key])
except (TypeError, KeyError): # overwrite, not a list or dict, or no key in first
except (TypeError, KeyError) as e: # overwrite, not a list or dict, or no key in first
print(e)
first[key] = second[key]
return first
......@@ -28,7 +28,7 @@ class TestMergeDicts(unittest.TestCase):
self.assertEquals(tak['kupujemy'], 'tak')
def test_nest(self):
tak = merge_dicts({'kupujemy': {"y": ['tak']}}, {'kupujemy': {"y":['nie']}})
tak = merge_dicts({'kupujemy': {"y": ['tak']}}, {'kupujemy': {"y": ['nie']}})
q = tak['kupujemy']['y']
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment