From e2e32331dc98738ca6615e5ac82f7114b7631e96 Mon Sep 17 00:00:00 2001 From: Paul Lee Date: Tue, 7 Jul 2020 17:20:37 -0400 Subject: [PATCH] add refer_by feature and remove add_num and adding string feature --- dyndict/dyndict.py | 37 ++++++++++++++++++++++++++++++++----- dyndict/test.py | 24 ++++++++++++------------ 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/dyndict/dyndict.py b/dyndict/dyndict.py index e919a15..e029405 100644 --- a/dyndict/dyndict.py +++ b/dyndict/dyndict.py @@ -2,9 +2,9 @@ class dyndict(dict): - def __init__(self, dictionary:dict=dict(), add_num:bool=True): + def __init__(self, dictionary:dict=dict(), refer_by=None): self.update(dictionary) - self.add_num = add_num + self.refer_by = refer_by def __add__(self, b): return self._add_primitive(self, b) @@ -16,8 +16,12 @@ def _add_primitive(self, a, b): try: if isinstance(a, dict) or isinstance(b, dict): raise TypeError - if (not self.add_num) and (isinstance(a, int) or isinstance(a, float)): + if isinstance(a, list) or isinstance(b, list): raise TypeError + if isinstance(a, str) and isinstance(b, str): + return b + if (isinstance(a, int) or isinstance(a, float)) and (isinstance(b, int) or isinstance(b, float)): + return b return a + b except TypeError: if isinstance(a, dict) and isinstance(b, dict): @@ -32,11 +36,34 @@ def _add_primitive(self, a, b): c = copy.deepcopy(a) c.update(b) return c + elif isinstance(a, list) and isinstance(b, list): + c = copy.deepcopy(a) + d = copy.deepcopy(b) + e = c + d + name_dict = dict() + for i in range(0, len(e)): + if isinstance(e[i], dict) and 'name' in e[i]: + if e[i]['name'] in name_dict: + e[name_dict[e[i]['name']]] = self._add_primitive(e[name_dict[e[i]['name']]], e[i]) + else: + name_dict[e[i]['name']] = i + name_set = set() + n = 0 + while n < len(e): + if isinstance(e[n], dict) and 'name' in e[n]: + if e[n]['name'] in name_set: + e.pop(n) + else: + name_set.add(e[n]['name']) + n += 1 + else: + n += 1 + return e elif isinstance(a, type(None)) and isinstance(b, type(None)): return None - elif (isinstance(a, int) or isinstance(a, float)) and (isinstance(b, int) or isinstance(b, float)): - return b else: + print(type(a)) + print(type(b)) raise NotImplementedError def _overwrite_primitive(self, a, b): diff --git a/dyndict/test.py b/dyndict/test.py index 98fdd17..70bf81d 100644 --- a/dyndict/test.py +++ b/dyndict/test.py @@ -43,29 +43,29 @@ def test_adding_set(self): self.dynd += {'a': {4, 5, 6}} self.assertEqual(self.dynd, {'a': {1, 2, 3, 4, 5, 6}}) - def test_adding_int(self): - self.dynd += {'a': 0} - self.assertEqual(self.dynd, {'a': 0}) + def test_overwriting_int(self): + self.dynd += {'a': -1} + self.assertEqual(self.dynd, {'a': -1}) self.dynd += {'a': 1} self.assertEqual(self.dynd, {'a': 1}) - def test_adding_string(self): + def test_overwriting_string(self): self.dynd += {'a': 'abc'} self.assertEqual(self.dynd, {'a': 'abc'}) self.dynd += {'a': 'def'} - self.assertEqual(self.dynd, {'a': 'abcdef'}) - - def test_not_adding_int(self): - self.dynd = dyndict(add_num=False) - self.dynd += {'a': -1} - self.assertEqual(self.dynd, {'a': -1}) - self.dynd += {'a': 1} - self.assertEqual(self.dynd, {'a': 1}) + self.assertEqual(self.dynd, {'a': 'def'}) def test_init_with_dict(self): self.dynd = dyndict({'a': 1}) self.assertEqual(self.dynd, {'a': 1}) + def test_refer_by(self): + self.dynd = dyndict(refer_by='name') + self.dynd += {'list': [{'name': 'a', 'x': 0}, {'name': 'b', 'x': 1}]} + self.assertEqual(self.dynd, {'list': [{'name': 'a', 'x': 0}, {'name': 'b', 'x': 1}]}) + self.dynd += {'list': [{'name': 'a', 'y': 2}, {'name': 'b', 'y': 3}]} + self.assertEqual(self.dynd, {'list': [{'name': 'a', 'x': 0, 'y': 2}, {'name': 'b', 'x': 1, 'y': 3}]}) + if __name__ == '__main__': unittest.main()