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

tests

parent 5bfb454e
No related branches found
No related tags found
No related merge requests found
......@@ -9,3 +9,4 @@ from .typecheck import typed, List, Tuple, Dict, NewType, Callable, Sequence, \
from .structures import TimeBasedHeap, CallableGroup
from .monitor import Monitor, RMonitor
from .algos import merge_dicts
# coding=UTF-8
from __future__ import print_function, absolute_import, division
import six
import logging
import types
import copy
import itertools
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):
v1.update(v2)
return v1
else:
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
first[key] = second[key]
return first
......@@ -2,7 +2,7 @@
from setuptools import setup, find_packages
setup(name='satella',
version='2.0.8',
version='2.0.9',
description=u'Utilities for writing servers in Python',
author=u'Piotr Maślanka',
author_email='piotrm@smok.co',
......
# coding=UTF-8
from __future__ import print_function, absolute_import, division
import six
import unittest
from satella.coding import merge_dicts
class TestMergeDicts(unittest.TestCase):
def test_merge_dicts(self):
tak = merge_dicts({'kupujemy': 'tak'}, {'kupujemy': 'nie'})
nie = merge_dicts({'kupujemy': 'nie'}, {'kupujemy': 'tak'})
self.assertEquals(tak['kupujemy'], 'nie')
self.assertEquals(nie['kupujemy'], 'tak')
def test_merge_lists(self):
tak = merge_dicts({'kupujemy': ['tak']}, {'kupujemy': ['nie']})
self.assertEqual(set(tak), set(['tak', 'nie']))
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