Skip to content

Commit

Permalink
Added list capability to ParameterTree
Browse files Browse the repository at this point in the history
ParamterTree can now return lists as nodes in the tree, rather than
simply expanding them into indexed dicts. This is required to allow
efficient iteration over parameters in e.g. the Excalibur adapter.
  • Loading branch information
timcnicholls committed Apr 18, 2017
1 parent 11b9d74 commit 220035d
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions odin/adapters/parameter_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
James Hogge, Tim Nicholls, STFC Application Engineering Group.
"""
from platform import node


class ParameterTreeError(Exception):
Expand Down Expand Up @@ -203,13 +204,20 @@ def __recursive_build_tree(self, node, path=''):

# Convert list or non-callable tuple to enumerated dict ; TODO - remove this?
if isinstance(node, list) or isinstance(node, tuple):
node = {str(i): node[i] for i in range(len(node))}
#print "BUILD 1 I AM AT ", type(node), "node", node, "path", path

return [self.__recursive_build_tree(elem, path=path) for elem in node]
#node = {str(i): node[i] for i in range(len(node))}

# Recursively check child elements
if isinstance(node, dict):
#print "BUILD 2 I AM AT ", type(node), "node", node, "path", path
return {k: self.__recursive_build_tree(
v, path=path + k + '/') for k, v in node.items()}


#if isinstance(node, list) or isinstance(node, tuple):
#print "BUILD 3 I AM AT ", type(node), "node", node, "path", path

return node

def __recursive_populate_tree(self, node):
Expand All @@ -224,13 +232,20 @@ def __recursive_populate_tree(self, node):
"""
# If this is a branch node recurse down the tree
if isinstance(node, dict):
#print "POPULATE 1 I AM AT", type(node), "node", node
return {k: self.__recursive_populate_tree(v) for k, v in node.items()}

if isinstance(node, list) or isinstance(node, tuple):
#print "POPULATE 2 I AM AT", type(node), "node", node
return [self.__recursive_populate_tree(item) for item in node]

# If this is a leaf node, check if the leaf is a r/w tuple and substitute the
# read element of that tuple into the node
if isinstance(node, ParameterAccessor):
#print "POPULATE 3 I AM AT", type(node), "node", node
return node.get()

#print "POPULATE 4 I AM AT", type(node), "node", node
return node

# Replaces values in data_tree with values from new_data
Expand Down

0 comments on commit 220035d

Please sign in to comment.