Skip to content
Snippets Groups Projects
Commit aee3182a authored by Piotr Maślanka's avatar Piotr Maślanka Committed by GitHub
Browse files

Merge pull request #6 from piotrmaslanka/add_smart_dictionary_join_(JSON_based)

Add smart dictionary join (json based)
parents 5bfb454e 4a809704
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, \ ...@@ -9,3 +9,4 @@ from .typecheck import typed, List, Tuple, Dict, NewType, Callable, Sequence, \
from .structures import TimeBasedHeap, CallableGroup from .structures import TimeBasedHeap, CallableGroup
from .monitor import Monitor, RMonitor 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, dict) and isinstance(v2, dict):
for k in v2.keys():
try:
v1[k] = _merge(v1[k], v2[k])
except KeyError:
v1[k] = v2[k]
return v1
if isinstance(v1, list) and isinstance(v2, list):
v1.extend(v2)
return v1
return v2
@typed(dict, dict, returns=dict)
def merge_dicts(first, second):
return _merge(first, second)
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
from setuptools import setup, find_packages from setuptools import setup, find_packages
setup(name='satella', setup(name='satella',
version='2.0.8', version='2.0.9',
description=u'Utilities for writing servers in Python', description=u'Utilities for writing servers in Python',
author=u'Piotr Maślanka', author=u'Piotr Maślanka',
author_email='piotrm@smok.co', 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['kupujemy']), set(['tak', 'nie']))
def test_adds(self):
tak = merge_dicts({'kupujemy': 'tak'}, {'xupujemy': 'nie'})
self.assertEquals(tak['xupujemy'], 'nie')
self.assertEquals(tak['kupujemy'], 'tak')
def test_nest(self):
tak = merge_dicts({'kupujemy': {"y": ['tak']}}, {'kupujemy': {"y": ['nie']}})
q = tak['kupujemy']['y']
self.assertIn('tak', q)
self.assertIn('nie', q)
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