Skip to content

Commit

Permalink
add refer_by feature and remove add_num and adding string feature
Browse files Browse the repository at this point in the history
  • Loading branch information
HillcrestEnigma committed Jul 7, 2020
1 parent 46cc957 commit e2e3233
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 17 deletions.
37 changes: 32 additions & 5 deletions dyndict/dyndict.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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):
Expand All @@ -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):
Expand Down
24 changes: 12 additions & 12 deletions dyndict/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit e2e3233

Please sign in to comment.