Skip to content

Commit

Permalink
Merge pull request #53 from odin-detector/pt-list-update
Browse files Browse the repository at this point in the history
Updated ParameterTree to accept a partial list update
  • Loading branch information
timcnicholls authored Feb 2, 2024
2 parents 0a5e25a + 438c124 commit db405a4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/odin/adapters/base_parameter_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ def _merge_tree(self, node, new_data, cur_path):
raise ParameterTreeError(
'Invalid path: {}{}'.format(cur_path, str(key_error)[1:-1])
)
if isinstance(node, list) and isinstance(new_data, dict):
if isinstance(node, list) and isinstance(new_data, (dict, list)):
try:
for i, val in enumerate(new_data):
node[i] = self._merge_tree(node[i], val, cur_path + str(i) + '/')
Expand Down
34 changes: 34 additions & 0 deletions tests/adapters/test_parameter_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,40 @@ def test_list_tree_set_from_root(self, test_param_tree):
test_param_tree.list_tree.set("",tree_data)
assert test_param_tree.list_tree.get("main") == tree_data

def test_list_tree_set_partial_from_root(self, test_param_tree):
"""Test that it is possible to set part of a list tree from its root."""
tree_data = {
'main' : [
{
'intParam': 3,
'floatParam': 4.56,
'boolParam': True,
'strParam': "test1",
},
[1,2,3,4]
]
}
test_param_tree.list_tree.set("",tree_data)
assert test_param_tree.list_tree.get("main/0/intParam") == {'intParam': 3}
assert test_param_tree.list_tree.get("main/0/floatParam") == {'floatParam': 4.56}
assert test_param_tree.list_tree.get("main/0/boolParam") == {'boolParam': True}
assert test_param_tree.list_tree.get("main/0/strParam") == {'strParam': "test1"}
assert test_param_tree.list_tree.get("main/1") == {'1': [1,2,3,4]}

tree_data = {
'main' : [
{
'intParam': 6,
},
]
}
test_param_tree.list_tree.set("",tree_data)
assert test_param_tree.list_tree.get("main/0/intParam") == {'intParam': 6}
assert test_param_tree.list_tree.get("main/0/floatParam") == {'floatParam': 4.56}
assert test_param_tree.list_tree.get("main/0/boolParam") == {'boolParam': True}
assert test_param_tree.list_tree.get("main/0/strParam") == {'strParam': "test1"}
assert test_param_tree.list_tree.get("main/1") == {'1': [1,2,3,4]}

def test_list_tree_from_dict(self, test_param_tree):
"""TEet that a list tree can be set with a dict of index/values."""
new_list_param = {0: 0, 1: 1, 2: 2, 3: 3}
Expand Down

0 comments on commit db405a4

Please sign in to comment.