Data structure for model hierarchy

model Module

Classes for representing the model.

class nsdf.model.ModelComponent(name, uid=None, parent=None, attrs=None, hdfgroup=None)[source]

Bases: object

Tree node for model tree.

parent

ModelComponent – parent of this component.

children

dict – dict of child components - name is key and ModelComponent is value.

attrs

dict – attributes of the component. These become HDF5 attributes when it is written to file.

hdfgroup

hdf5 Group – the group that this component corresponds to in NSDF file.

add_child(child)[source]

Add a child component under this model component.

Parameters:child (ModelComponent) – child component to add to this component
Returns:None
Raises:TypeError
add_children(children)[source]

Add a list of children to current component.

Parameters:children (list) – list of children to be added.
Returns:None
Raises:TypeError
check_uid(uid_dict)[source]

Check that uid are indeed unique.

Parameters:
  • uid_dict (dict) – an empty dict for storing the uids
  • to all components that have the same uid. (mapping) –

Note

If any uid is not set, this function as a side effect creates the uids in the form parentuid/name - similar to unix file paths.

get_id_path_dict()[source]

Return a dictionary mapping the unique id of the model components to their path in modeltree.

See also

update_id_path_dict

get_node(path)[source]

Get node at path relative to this node.

Parameters:path (str) – path obtained by concatenating component names with / as separator.
Returns:ModelComponent at the specified path
Raises:KeyError if there is no element at the specified path.
path

Path of this component

print_tree(indent='')[source]

Recursively print subtree rooted at this component.

Parameters:indent (str) – indentation.
Returns:None
update_id_path_dict()[source]

Update the id->path mapping.

This must be called before using get_id_path_dict whenever the model tree is been modified

See also

get_id_path_dict

visit(function, *args, **kwargs)[source]

Visit the subtree starting with node recursively, applying function fn to each node.

Parameters:
  • node (ModelComponent) – node to start with.
  • fn (node, *args, **kwargs) – a function to apply on each node.
Returns:

None

nsdf.model.common_prefix(paths, sep='/')[source]

Find the common prefix of paths.

Note: does not check for malformed paths right now.