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 base for XML nodes

set_modified()#

Mark the file containing this node as modified.

make_element(cls: type[ANode], tag: str, **kwargs) ANode#

Create a new XmlNode element

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'>
property name: str#

The value of the name attribute

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'>
property id: int#

The value of the id attribute

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).

property value: str#

The value of the param node, returned as a string

set_value(value: bool | int | float | str | None)#

Set the parameter’s value

Conversion is done from all value types to a PSCAD-esque string value

property rows: list[int | float | str]#

Contents of a parameter with one or more <row/> elements

Raises:

ValueError – if the parameter doesn’t have <row/> elements

Added in version 1.4.0.

property table: list[list[int | float | str]]#

Contents of a table parameter

Raises:

ValueError – if the parameter doesn’t have <row/> elements

Added in version 1.4.0.

class mhi.xml.node.ParamListNode#

A container of <param/> nodes:

<paramlist>
  <param name="p1" value="10"/>
  <param name="p2" value="true"/>
  ...
</paramlist>
get_param(name: str) str#

Return the named parameter’s value, as a string

set_param(name: str, value: Any) None#

Set the named parameter’s value

has_keys(*keys: str) bool#

Test if the <paramlist/> contains all of the given keys

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.

create_param(name: str, val: Any) ParamNode#

Create and add a new <param name={name} value={val}> node

create_params(dct: dict[str, Any] | None = None, **kwargs) None#

Create and add new <param name=”…” value=”…”> nodes

create_rows_param(name: str, rows: list[Any] | int) ParamNode#

Create and add a new “rows” parameter

The rows parameter can be a list of values, [1.0, 2.0, 3.0], or an integer representing the number of rows for the parameter.

Added in version 1.4.0.

create_table_param(name: str, table: list[list[Any]] | tuple[int, int]) ParamNode#

Create and add a new “table” parameter

The table parameter can be a list of lists of values, [[0.0, 1.0], [2.0, 3.0], [4.0, 5.0]], or a tuple defining the number of rows and columns (3, 2).

Added in version 1.4.0.

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
as_dict() dict[str, Any]#

Return all of the parameters as a dictionary

set_defaults()#

Create all parameters, assigning using their default values