You can call (almost) any method name on the Tree to create a new Node.
Very top node in a datatree.
The Tree is the top node used to build a datatree.
Add a CDataNode to the current tree.
Note
CData tags are ignored by some of the renderers such as json and dict. Consult the documentation to find out the behaviour.
Parameters: |
|
---|---|
Returns: | The created CDataNode. |
Adds a comment to the node.
Note
Comments are ignored by some of the renderers such as json and dict. Consult the documentation to find out the behaviour.
Parameters: | text – Text content of the comment. |
---|
Add an xml declaration to the datatree.
Note
Declarations are ignored by some of the renderers such as json and dict. Consult the documentation to find out the behaviour.
Warning
This functionality is pretty limited for the time being, hopefully the API for this will become more clear with time.
Parameters: |
|
---|
Add an xml processing instruction.
Note
Instructions are ignored by some of the renderers such as json and dict. Consult the documentation to find out the behaviour.
Parameters: |
|
---|
Creates and adds Node object to the current tree.
Parameters: |
|
---|---|
Returns: | The created Node. |
Register a renderer class with the datatree rendering system.
Parameters: | klass – Either a string with the fully qualified name of the renderer class to register, or the actual class itself. The name will be read from the class. |
---|
Render the datatree using the provided renderer.
Parameters: |
|
---|
Create an ugly representation of the datatree from this node down.
Warning
This is included as a debug aid and is not good for much else. The output is messy and inconsistent.
Node is not instantiated directly, but is created for every node added to the Tree.
A node is able to be instantiated directly and added to any Vertex.
Add a CDataNode to the current tree.
Note
CData tags are ignored by some of the renderers such as json and dict. Consult the documentation to find out the behaviour.
Parameters: |
|
---|---|
Returns: | The created CDataNode. |
Adds a comment to the node.
Note
Comments are ignored by some of the renderers such as json and dict. Consult the documentation to find out the behaviour.
Parameters: | text – Text content of the comment. |
---|
Creates and adds Node object to the current tree.
Parameters: |
|
---|---|
Returns: | The created Node. |
Register a renderer class with the datatree rendering system.
Parameters: | klass – Either a string with the fully qualified name of the renderer class to register, or the actual class itself. The name will be read from the class. |
---|
Render the datatree using the provided renderer.
Parameters: |
|
---|
Create an ugly representation of the datatree from this node down.
Warning
This is included as a debug aid and is not good for much else. The output is messy and inconsistent.
The renderers are responsible for converting the datatree into a usable format. Usually this format is a string, but sometimes other formats are used.
The examples in this section use this datatree:
from datatree import Tree, Node
tree = Tree()
with tree.node("author") as author:
author.node('name', 'Terry Pratchett')
author.node('genre', 'Fantasy/Comedy')
author.comment("Only 2 books listed")
with author.node('novels', count=2) as novels:
novels.node('novel', 'Small Gods', year=1992)
novels.node('novel', 'The Fifth Elephant', year=1999)
novels.node("novel", "Guards! Guards!", year=1989)
Outputs the tree as an xml string. It is available under the alias 'xml'.
Name | Description | Default |
---|---|---|
pretty | When True, Outputs the xml document with pretty formatting. | False |
indent | Used with pretty formatting. It is the string that will be used to indent each level. | ' ' |
tree('xml', pretty=True)
Or even shorter:
tree(pretty=True)
<author>
<name>Terry Pratchett</name>
<genre>Fantasy/Comedy</genre>
<!-- Only 2 books listed -->
<novels count="2">
<novel year="1992">Small Gods</novel>
<novel year="1999">The Fifth Elephant</novel>
<novel year="1989">Guards! Guards!</novel>
</novels>
</author>
Outputs the tree as json string using the python json module. It is available under the alias 'json', 'jsn' or 'js'.
Name | Description | Default |
---|---|---|
pretty | Outputs the json document with pretty formatting. | False |
sort_keys | Sorts the keys in the json document. | False |
tree('json', pretty=True)
{
"author": {
"genre": "Fantasy/Comedy",
"name": "Terry Pratchett",
"novels": [
"Small Gods",
"The Fifth Elephant",
"Guards! Guards!"
]
}
}
Outputs the tree as yaml string using the PyYAML package (which must be installed). It is available under the alias 'yaml' or 'yml'.
Name | Description | Default |
---|---|---|
None |
tree('yaml')
author:
genre: Fantasy/Comedy
name: Terry Pratchett
novels: [Small Gods, The Fifth Elephant, Guards! Guards!]
Outputs the tree as python dict. It is available under the alias 'dict' and 'dictionary'.
Name | Description | Default |
---|---|---|
pretty_string | When True, outputs the dict as a string with pretty formatting. | False |
allow_node_loss | Determines if a duplicate node name will result in a node loss due to duplicate keys in the dict. | False |
tree('dict', pretty_string=True)
{'author': {'genre': 'Fantasy/Comedy',
'name': 'Terry Pratchett',
'novels': ['Small Gods', 'The Fifth Elephant', 'Guards! Guards!']}}
While xml handles duplicate nodes just fine, python dicts and json for that matter do not allow duplicates. To handle this the DictRenderer will attempt to group nodes with the same name into a sub dictionary. This is why in the above example there is only one key for “novels”.
You can implement your own renderer. Just look at the source for one of the existing renderers and implement the same methods, and then register your plugin with the register_renderer() method.