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
Create an XML <tag> element.
Create a text node in the AST.
Convert this node to xml representation, applying f to all subnodes.
Attributes
tag of the node, or None for text node
node's attributes, for text node empty dictionary
node's children, text node has no children
text content of the node; for non-text nodes None
reference to the parent node in the AST
file, in which the element is defined
line of the element's occurrence if known, else None
column of the element's occurrence if known, else None
the AIML version declared with the <aiml> root element
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
- children: List[pyaiml21.ast.node.Node][source]¶
node’s children, text node has no children
- 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>'