vxorg/python_refonly/tree_test.py
modeco80 af49206a16 Rewrite it in C++
rust people are going to be very mad at me now /j
2024-10-21 12:22:35 -04:00

48 lines
No EOL
913 B
Python

import tree
the_tree = tree.Tree()
# create leaf node
virus = the_tree.create_leaf('Virus')
virus2 = the_tree.create_leaf('Worm')
# create child leaf
test = virus.create_child_leaf('Test')
# create test items inside of 'test' leaf
v1 = test.create_child_leaf('a')
v2 = test.create_child_leaf('b')
v3 = test.create_child_leaf('c')
v4 = test.create_child_leaf('884')
def walk_cb(node):
ident = ''
# Walk the parents to figure out current tree depth
# and how many
parent_list = []
parent = node.parent
parent_count = 0
while parent is not None:
if parent.item is not None:
parent_list.append(parent.item)
parent_count += 1
parent = parent.parent
#for item in reversed(parent_list):
# ident += f'{item}.'
if node.item is not None:
ident += node.item
else:
ident = '[root]'
tab = ''
for i in range(0, parent_count):
tab += '\t'
print(f"{tab}{ident}")
the_tree.walk(walk_cb)