pyaiml21.ast.node.Node

class pyaiml21.ast.node.Node(*, tag: Optional[str] = None, attributes: Optional[Dict[str, pyaiml21.ast.node.Node]] = None, children: Optional[List[pyaiml21.ast.node.Node]] = None, text: Optional[str] = None, parent: Optional[pyaiml21.ast.node.Node] = None, filename: Optional[str] = None, line: Optional[int] = None, col: Optional[int] = None, version: Optional[pyaiml21.aiml.AIMLVersion] = None)[source][source]

Represents a node in the AST.

Nodes of the parse tree created from parsed xml files are reconstructed using this class to provide uniform access to attributes and children.

Given the nature of the AIML, especially the fact that attributes are allowed to be specified as children, this node represents both the attributes and the children as a list of other Node’s, and it is responsibility of syntax check to make sure, which children are attributes.

To create instances of Node, please use static methods Node.Element and Node.Value.

Methods

Element

Create an XML <tag> element.

Value

Create a text node in the AST.

to_xml

Convert this node to xml representation, applying f to all subnodes.

Attributes

tag

tag of the node, or None for text node

attributes

node's attributes, for text node empty dictionary

children

node's children, text node has no children

text

text content of the node; for non-text nodes None

parent

reference to the parent node in the AST

filename

file, in which the element is defined

line

line of the element's occurrence if known, else None

col

column of the element's occurrence if known, else None

aiml_version

the AIML version declared with the <aiml> root element

is_text_node

Return True if this node is a text node (without a tag).

classmethod Element(name: str, parent: Optional[pyaiml21.ast.node.Node] = None, attribs: Optional[Dict[str, pyaiml21.ast.node.Node]] = None, children: Optional[List[pyaiml21.ast.node.Node]] = None, filename: Optional[str] = None, line: Optional[int] = None, col: Optional[int] = None, version: Optional[pyaiml21.aiml.AIMLVersion] = None)[source][source]

Create an XML <tag> element.

This class method allows creation of arbitrary nodes in the AST representing XML tags. For parameters, see Node.

Example:
>>> root = Node.Element("root")
>>> a = Node.Element("a", parent=root)
>>> root.children.append(a)
>>> [child.tag for child in root.children] == ["a"]
True
>>> root.attributes["name"] = Node.Value("my_node")
>>> root.is_text_node
False
>>> root.text is None
True
classmethod Value(text: str, parent: Optional[pyaiml21.ast.node.Node] = None, filename: Optional[str] = None, line: Optional[int] = None, col: Optional[int] = None, version: Optional[pyaiml21.aiml.AIMLVersion] = None)[source][source]

Create a text node in the AST.

Use this classmethod to construct a text node into the AST.

Example:
>>> node = Node.Value("some text")
>>> node.is_text_node
True
>>> node.text == "some text"
True
>>> node.tag is None
True
aiml_version[source]

the AIML version declared with the <aiml> root element

attributes[source]

node’s attributes, for text node empty dictionary

children: List[pyaiml21.ast.node.Node][source]

node’s children, text node has no children

col[source]

column of the element’s occurrence if known, else None

filename[source]

file, in which the element is defined

property is_text_node: bool[source]

Return True if this node is a text node (without a tag).

line[source]

line of the element’s occurrence if known, else None

parent[source]

reference to the parent node in the AST

tag[source]

tag of the node, or None for text node

text[source]

text content of the node; for non-text nodes None

to_xml(f: Callable[[pyaiml21.ast.node.Node], str]) str[source][source]

Convert this node to xml representation, applying f to all subnodes.

Return xml representation of this node where node.tag is the root element and text attributes (nodes with node.is_text_node == True) are transformed to root’s XML attributes.

If current node is text, return just text.

Examples:
>>> discard = lambda x: ""
>>> text_node = Node.Value("some text")
>>> text_node.to_xml(discard) == "some text"
True
>>> attr = Node.Value("value")
>>> a = Node.Element("a")
>>> b = Node.Element("b", attribs={"attr": attr}, children=[a])
>>> a.parent = b
>>> attr.parent = b
>>> b.to_xml(str)
'<b attr="value"><a></a></b>'