Skip to content

Commit

Permalink
feat: enabled patch_dict to add new attributes and elements
Browse files Browse the repository at this point in the history
  • Loading branch information
jonrkarr committed Oct 23, 2021
1 parent ff948d1 commit fd4e3f4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion biosimulators_utils/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.1.139'
__version__ = '0.1.140'
10 changes: 9 additions & 1 deletion biosimulators_utils/utils/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,15 @@ def patch_dict(dictionary, patch):
while patch_queue:
props, props_patch = patch_queue.pop()
for key, new_val in props_patch.items():
value = props[key]
if isinstance(props, dict):
value = props.get(key, None)
else:
if key >= len(props):
props.extend([None] * (key + 1 - len(props)))
value = None
else:
value = props[key]

if isinstance(value, dict):
patch_queue.append((props[key], new_val))

Expand Down
14 changes: 14 additions & 0 deletions tests/utils/test_core_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,18 @@ def test_patch_dict(self):
'version': 'x2',
'image': {
'url': 'y2',
'digest': 'z2',
},
'algorithms': {
1: {
'kisaoId': {
'id': 'KISAO_XXXXXXX'
}
},
4: {
'kisaoId': {
'id': 'KISAO_YYYYYYY'
}
}
},
'authors': ['A', 'B', 'C', 'D'],
Expand All @@ -303,11 +309,19 @@ def test_patch_dict(self):
expected_dictionary = copy.deepcopy(dictionary)
expected_dictionary['version'] = 'x2'
expected_dictionary['image']['url'] = 'y2'
expected_dictionary['image']['digest'] = 'z2'
expected_dictionary['algorithms'][1]['kisaoId']['id'] = 'KISAO_XXXXXXX'
expected_dictionary['algorithms'].append(None)
expected_dictionary['algorithms'].append({
'kisaoId': {
'id': 'KISAO_YYYYYYY'
}
})
expected_dictionary['authors'] = ['A', 'B', 'C', 'D']

utils.patch_dict(dictionary, patch)

print(dictionary['algorithms'])
self.assertEqual(dictionary, expected_dictionary)

def test_flatten_nested_list_of_strings(self):
Expand Down

0 comments on commit fd4e3f4

Please sign in to comment.