SettingNode#
- class SettingNode(name, **children)#
Bases:
object
A tree-structured
Setting
container.Each child of the node is a
Setting
, or anotherSettingNode
. Iterating over the node returns all children, recursively. Settings can be accessed by dictionary syntax or attribute syntax:>>> from exa.common.data.parameter import Parameter >>> from exa.common.data.setting_node import SettingNode >>> p1 = Parameter("voltage", "Voltage") >>> settings = SettingNode('name', volt=p1) >>> settings.volt.parameter is p1 True >>> settings['volt'].parameter is p1 True >>> settings.volt.value is None True >>> settings.volt = 7 # updates to Setting(p1, 7) >>> settings.volt.value 7
- Parameters:
name (str) – Name of the node.
children – The children given as keyword arguments. Each argument must be a
Setting
,Parameter
, or aSettingNode
. The keywords are used as the names of the nodes. Parameters will be cast into Settings with the valueNone
.
Module:
exa.common.data.setting_node
Attributes
Yields all
Setting
instances inside this node, recursively.ItemsView of immediate child nodes of this node.
ItemsView of settings of this node.
Dictionary of immediate child nodes of this node.
Methods
Return a deepcopy of this SettingNode.
Recursive diff between two SettingNodes.
Find first occurrence of Setting or SettingNode by name, by iterating recursively through all children.
Get the first SettingNode that has a Setting named name.
Recursively combine the tree structures and values of two SettingNodes.
Recursively combine the values from another
SettingNode
to this one.Yields all nodes, filtered by given node_types.
Print a tree representation of the contents of this node.
Recursively delete all branches from this SettingNode that are not found in
other
.Recursively set values to Settings, taking values from a dictionary that has similar tree structure.
Get Setting from
self
where the name of the parameter is replaced with its path inself
.Reduce any subclass of SettingNode and it's contents into instances of cls.
Update an existing Setting in this tree.
- _ipython_key_completions_()#
List items and subtree names, so they occur in IPython autocomplete after
node[<TAB>
- nodes_by_type(node_types=None, recursive=False)#
Yields all nodes, filtered by given node_types.
Used to find and iterate over nodes of specific types.
- Parameters:
node_types (type | tuple[type, ...] | None) – when iterating over the tree, yields only instances that match this type or any of the types in the tuple. By default, yields Settings and SettingNodes.
recursive (bool) – If True, the search is carried recursively. If False, the search is limited to immediate child nodes.
- Returns:
Iterator that yields the filtered nodes.
- Return type:
- update_setting(setting)#
Update an existing Setting in this tree.
- Parameters:
setting (Setting) – Setting that will replace an existing Setting with the same name. Or if the setting is an element-wise setting (i.e. it has a non-empty value of
setting.element_indices
), the corresponding element will be updated in the collection.- Raises:
UnknownSettingError – If no setting is found in the children of this tree.
- Return type:
None
- property all_settings: Generator[Setting]#
Yields all
Setting
instances inside this node, recursively.
- property children: dict[str, Setting | SettingNode]#
Dictionary of immediate child nodes of this node.
- property child_nodes: ItemsView[str, SettingNode]#
ItemsView of immediate child nodes of this node.
- copy()#
Return a deepcopy of this SettingNode.
- Return type:
- get_parent_of(name)#
Get the first SettingNode that has a Setting named name.
- Parameters:
name (str) – Name of the setting to look for.
- Returns:
A SettingNode that has a child name.
- Return type:
- find_by_name(name)#
Find first occurrence of Setting or SettingNode by name, by iterating recursively through all children.
- Parameters:
name (str) – Name of the Setting or SettingNode to look for.
- Returns:
First found item, or None if nothing is found.
- Return type:
SettingNode | Setting | None
- static merge(first, second)#
Recursively combine the tree structures and values of two SettingNodes.
In case of conflicting nodes,values in first take priority regardless of the replaced content in second. None values are never prioritized.
- Parameters:
first (SettingNode) – SettingNode to merge, whose values and structure take priority
second (SettingNode) – SettingNode to merge.
- Returns:
A new SettingNode constructed from arguments.
- Return type:
- merge_values(other, prioritize_other=False)#
Recursively combine the values from another
SettingNode
to this one.The resulting tree structure the same as that of self.
- Parameters:
other (SettingNode) – SettingNode to merge.
prioritize_other (bool) – If True, will prioritize values in other. If False (default), only None values in self will be replaced.
- prune(other)#
Recursively delete all branches from this SettingNode that are not found in
other
.- Parameters:
other (SettingNode) –
- Return type:
None
- print_tree(levels=5)#
Print a tree representation of the contents of this node.
- Parameters:
levels (int) – display this many levels, starting from the root.
- Return type:
None
- classmethod transform_node_types(node)#
Reduce any subclass of SettingNode and it’s contents into instances of cls.
- Parameters:
node (SettingNode) – node to transform.
- Returns:
A new SettingNode with the same structure as the original, but where node instances are of type cls.
- Return type:
- set_from_dict(dct, strict=False)#
Recursively set values to Settings, taking values from a dictionary that has similar tree structure. Keys that are not found in self are ignored, unless strict is True.
- Parameters:
- Raises:
UnknownSettingError – If the condition of strict happens.
- Return type:
None
- setting_with_path_name(setting)#
Get Setting from
self
where the name of the parameter is replaced with its path inself
.The path is defined as the sequence of attribute keys leading to the node with
setting
in it. The method is used for conveniently saving settings tree observations with their settings tree path as thedut_field
.- Parameters:
setting (Setting) – Setting to search for.
- Returns:
Copy of Setting
setting
where the parameter name is replaced with its path inself
.- Raises:
UnknownSettingError – If
name
is not found withinself
.- Return type:
- diff(other, *, path='')#
Recursive diff between two SettingNodes.
This function is meant to produce human-readable output, e.g. for debugging purposes. It returns the differences in a list of strings, each string detailing one specific difference. The diff is non-symmetric.
- Parameters:
other (SettingNode) – second node to compare
self
topath (str) – node path to the currently compared nodes (used in printing the results)
- Returns:
differences from
self
toother
, in depth-first order- Return type:
- _withsiprefix(val, unit)#
Turn a numerical value and unit, and return rescaled value and SI prefixed unit.
Unit must be a whitelisted SI base unit.