Skip to main content

Create nodes as specified in the tree text.

Project description

ja

TexTree

Create nodes as specified in the tree text.

Features

  • Compatible with python2,3
  • Data can be assigned to nodes
  • Convert between text and node objects

Requirement

  • <time datetime="2019-12-26T10:00:00+0900">2019-12-26</time>
  • Raspbierry Pi 4 Model B Rev 1.2
  • Raspbian buster 10.0 2019-09-26 <small>setup</small>
  • bash 5.0.3(1)-release
  • Python 2.7.16
  • Python 3.7.3

Installation

pip install textree

Usage

Foundations

Convert from text to node object.

import textree
tree_text = """
A
	A1
		A11
			A111
			A112
	A2
B
"""
tree = TexTree()
root = tree.to_node(tree_text)
print(root, root.Name)
for node in tree.Nodes:
    print(node.Line)
print(tree.to_text())

Reference

Convert between text and objects.

root = tree.to_node(tree_text)
       tree.to_text()

Reference and assignment.

node.Name
node.Parent
node.Children
node.Name = 'NewName'
node.Parent = Node('Parent')
node.Children.append(Node('Child'))

Move.

node.to_first()
node.to_last()
node.to_next()
node.to_prev()

Get.

Node.Path.select(root, 'A/A1/A11')
Node.Path.select(A, 'A1/A11')

Insertion / deletion.

node.insert_first(Node('new'))
node.insert_last(Node('new'))
node.insert_next(Node('new'))
node.insert_prev(Node('new'))
node.delete()

Update.

node = Node.Path.select(root, 'A/A1/A11')
node.Name = 'UpdateName'

There are many others. Refer to the code or API list for details.

Attribute of Node

Attributes can be assigned to the same line.

import textree
tree_text = """
A	attrA
	A1	attrA1
		A11	attrA11
			A111	attrA111
			A112	attrA112
	A2	attrA2
B	attrB
"""
tree = TexTree()
root = tree.to_node(tree_text)
print(root, root.Name)
for node in tree.Nodes:
    print(node.Name, node,Attr)

Attribute of RootNode

Attributes can be assigned to RootNode.

import textree
tree_text = """
<ROOT>	root_attr
A	attrA
	A1	attrA1
		A11	attrA11
			A111	attrA111
			A112	attrA112
	A2	attrA2
B	attrB
"""
tree = TexTree()
root = tree.to_node(tree_text)
print(root, root.Name, root.Attr)
for node in tree.Nodes:
    print(node.Name, node,Attr)

Deserialization of attributes

The user can embed the attribute analysis code freely. Of course you can also write code to serialize to text.

The following code gives the node my_name.

class MyNodeDeserializer(NodeDeserializer):
    def deserialize(self, ana, parent, parents=Node):
        node = Node(ana.Line, parent=parent)
        node.my_name = 'My name is ' + node.Name
        return node
tree = TexTree(node_deserializer=MyNodeDeserializer())
root = tree.to_node(tree_text)
for node in tree.Nodes:
    print(node.my_name)
class MyNodeAttributeSerializer(NodeAttributeSerializer):
    def serialize(self, attr): return 'my_name=' + attr
tree = TexTree(node_deserializer=MyNodeDeserializer(), node_serializer=NodeSerializer(MyNodeAttributeSerializer()))
root = tree.to_node(tree_text)
for node in tree.Nodes:
    print(node.my_name)
print(tree.to_text())

Note

  • Alpha version. Checking installation

Author

 ytyaru

  • github
  • hatena
  • mastodon

License

This software is AGPLv3 licensed. (GNU Affero General Public License v3) agpl-3.0

agpl-3.0

Project details


Release history Release notifications

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for textree, version 0.0.1
Filename, size File type Python version Upload date Hashes
Filename, size textree-0.0.1-py2-none-any.whl (41.9 kB) File type Wheel Python version py2 Upload date Hashes View hashes
Filename, size textree-0.0.1-py3-none-any.whl (41.9 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size textree-0.0.1.tar.gz (41.9 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page