Cleanup broken algorithm and some simple refactors.
This commit is contained in:
parent
418b5ae28d
commit
d993a70ac7
@ -2,6 +2,24 @@ from anytree import LevelOrderGroupIter
|
|||||||
from anytree.importer import JsonImporter
|
from anytree.importer import JsonImporter
|
||||||
from sys import argv
|
from sys import argv
|
||||||
|
|
||||||
|
|
||||||
|
def build_rows_depth_first(node, rows, leaf_count=0):
|
||||||
|
|
||||||
|
for child in sorted(node.children, key=lambda x: x.size, reverse=True):
|
||||||
|
leaf_count = build_rows_depth_first(child, rows, leaf_count)
|
||||||
|
|
||||||
|
if node.is_leaf:
|
||||||
|
cur_row = leaf_count
|
||||||
|
rows[cur_row].append(f'<td>{node.name}</td>')
|
||||||
|
leaf_count += 1
|
||||||
|
else:
|
||||||
|
child_leaves = len(node.leaves)
|
||||||
|
cur_row = leaf_count - child_leaves
|
||||||
|
rows[cur_row].append(f'<td rowspan="{child_leaves}">{node.name}</td>')
|
||||||
|
|
||||||
|
return leaf_count
|
||||||
|
|
||||||
|
|
||||||
if len(argv) < 2:
|
if len(argv) < 2:
|
||||||
print(f"usage: {argv[0]} <json file>")
|
print(f"usage: {argv[0]} <json file>")
|
||||||
exit()
|
exit()
|
||||||
@ -9,62 +27,12 @@ if len(argv) < 2:
|
|||||||
with open(argv[1], 'r') as f:
|
with open(argv[1], 'r') as f:
|
||||||
root = JsonImporter().read(f)
|
root = JsonImporter().read(f)
|
||||||
|
|
||||||
rows = []
|
|
||||||
leaves = []
|
|
||||||
|
|
||||||
for i in range(len(root.leaves)):
|
|
||||||
rows.append([])
|
|
||||||
leaves.append({"name":"wrong"})
|
|
||||||
|
|
||||||
cur_row = 0
|
|
||||||
for level in LevelOrderGroupIter(root):
|
|
||||||
cur_row = 0
|
|
||||||
|
|
||||||
for node in sorted(level, key=lambda x: x.size, reverse=True):
|
|
||||||
leaf_count = len(node.leaves)
|
|
||||||
if False: #node in root.leaves:
|
|
||||||
leaves[cur_row] = node
|
|
||||||
else:
|
|
||||||
rows[cur_row].append(f'<td rowspan="{leaf_count}">{node.name}</td>')
|
|
||||||
|
|
||||||
cur_row += max(leaf_count, 1)
|
|
||||||
last_depth = node.depth
|
|
||||||
|
|
||||||
print(leaves)
|
|
||||||
|
|
||||||
for i, leaf in enumerate(leaves):
|
|
||||||
#rows[i].append(f"<td>{leaf.name}</td>")
|
|
||||||
rows[i].reverse()
|
|
||||||
|
|
||||||
print("<tr>")
|
|
||||||
for cell in rows[i]:
|
|
||||||
print(f" {cell}")
|
|
||||||
print("</tr>")
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
output_rows = []
|
output_rows = []
|
||||||
rows = []
|
|
||||||
|
|
||||||
for i in range(len(root.leaves)):
|
for i in range(len(root.leaves)):
|
||||||
output_rows.append([])
|
output_rows.append([])
|
||||||
|
|
||||||
def depth_first_search(node, rows, leaf_count):
|
build_rows_depth_first(root, output_rows)
|
||||||
|
|
||||||
for child in sorted(node.children, key=lambda x: x.size, reverse=True):
|
|
||||||
leaf_count = depth_first_search(child, rows, leaf_count)
|
|
||||||
|
|
||||||
if node.is_leaf:
|
|
||||||
cur_row = leaf_count
|
|
||||||
rows[cur_row].append(f'<td>{node.name}</td>')
|
|
||||||
leaf_count += 1
|
|
||||||
else:
|
|
||||||
cur_row = leaf_count - len(node.leaves)
|
|
||||||
rows[cur_row].append(f'<td rowspan="{len(node.leaves)}">{node.name}</td>')
|
|
||||||
|
|
||||||
return leaf_count
|
|
||||||
|
|
||||||
depth_first_search(root, output_rows, 0)
|
|
||||||
|
|
||||||
for row in output_rows:
|
for row in output_rows:
|
||||||
print("<tr>")
|
print("<tr>")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user