Xml Node#
MHI’s XML Nodes
- Includes:
XmlNode (a few extra methods, …)
NamedNode <node name=’A-Name’/>
IdNode <node id=’10’/>
NamedIdNode <node name=’A-Name’ id=’10’/>
and so on.
- class mhi.xml.node.XmlNode#
Custom nase for XML nodes
- set_modified()#
Mark the file containing this node as modified.
- append_text(text: str) None#
Append the given text string to the content inside this node.
If the node contains other elements, the text is added to the last child’s tail, instead of as this node’s text.
- append_indented(node: _Element, spaces: int = -1, space_inc: int = 2) None#
Append a child node to the children of the current node, with white-space before and after the element to maintain proper indentation.
Note: The child’s content is not modified; it is assumed to already be properly indented.
- create_param_list_node(**kwargs) ParamListNode#
Add a <paramlist/> child node
- class mhi.xml.node.NamedNode#
An XML node with a read-only name attribute:
<tag name='something'>
- class mhi.xml.node.NamedNodeContainerMapping(container: XmlNode, tag: str, class_name: str | None = None)#
An XML node that contains a subtype of NamedNode elements:
<container> <tag name='name1' /> <tag name='name2' /> </container>
The NamedNode tag must be stored in the _CHILD_TAG of the container mapping.
Each NamedNode can be referenced by either name or index.
- names()#
List all the name keys in the container
- class mhi.xml.node.KeyMapping(container: XmlNode, path: str, attr: str, class_name: str | None = None)#
A container that contains elements identified by a key-value.
The elements do not need to be a direct child of the container. In the following structure, an instance of KeyMapping could be used to map to subcontainer[@key=’…’], while another instance could map to subcontainer/tag[@name=’…’]:
<container> <subcontainer key='key_a'> <tag name='name1' /> <tag name='name2' /> </subcontainer> <subcontainer key='key_b'> <tag name='name3' /> <tag name='name4' /> </subcontainer> </container>
Keyed Nodes can be found or deleted, but not added unless the path is a direct child node.
- items() a set-like object providing a view on D's items#
- class mhi.xml.node.IdNode#
An XML node with a read-only id attribute:
<tag id='123456789'>
- class mhi.xml.node.NamedIdNode#
An XML node with read-only name and id attributes:
<tag name='something' id='123456789'>
- class mhi.xml.node.ParamNode#
A param node, contained in a paramlist node container:
<paramlist> <param name="p1" value="10"/> <param name="p2" value="true"/> ... </paramlist>
A param have both a name and a value. Usually, the value is stored as a value attribute, but may be stored as child nodes for complex values (such as tables).
- class mhi.xml.node.ParamListNode#
A container of <param/> nodes:
<paramlist> <param name="p1" value="10"/> <param name="p2" value="true"/> ... </paramlist>
- missing_keys(*keys: str) str#
Returns a comma-separated string of which keys of the given keys are not present in the <paramlist/>.
Returns an empty string if all keys are found.
- as_dict() dict[str, str]#
Returns all of the <paramlist/> parameters as a dictionary.
No attempt is made to convert values to other types.
- class mhi.xml.node.ParametersBase(param_list: _Element | None)#
A typed-enhanced proxy of a <paramlist/>.
The type for a param’s value is determine using the type-hint for that member name.
Example:
class MyParameters(ParametersBase): enabled: bool time_step: float num_runs: int
- set_defaults()#
Create all parameters, assigning using their default values