Compare commits
	
		
			No commits in common. "7ce5a2bb8ba7c7f1193bafce3b0560a5ea3e764d" and "4bf120ec8d745f66771d1a3c3da6d3fe5b210b1c" have entirely different histories.
		
	
	
		
			7ce5a2bb8b
			...
			4bf120ec8d
		
	
		
@ -1,5 +1,6 @@
 | 
			
		||||
import json
 | 
			
		||||
 | 
			
		||||
from anytree import LevelOrderIter, LevelOrderGroupIter
 | 
			
		||||
from anytree.importer import DictImporter
 | 
			
		||||
from sys import argv
 | 
			
		||||
 | 
			
		||||
@ -55,22 +56,8 @@ recipe_desc = recipe.get("description")
 | 
			
		||||
importer = DictImporter()
 | 
			
		||||
sub_recipes = [importer.import_(data) for data in recipe["sub_recipes"]]
 | 
			
		||||
 | 
			
		||||
print("<!DOCTYPE html>")
 | 
			
		||||
print("<html lang='en'>")
 | 
			
		||||
print(f"<head><title>{recipe_name}</title>")
 | 
			
		||||
print("<link rel='stylesheet' type='text/css' href='style.css'>")
 | 
			
		||||
print("</head>")
 | 
			
		||||
print("<body>")
 | 
			
		||||
 | 
			
		||||
print("<div id='title'>")
 | 
			
		||||
print(f"<h1>{recipe_name}</h1>")
 | 
			
		||||
print("</div>")
 | 
			
		||||
 | 
			
		||||
print("<div id='description'>")
 | 
			
		||||
print(f"<p>{recipe_desc}</p>")
 | 
			
		||||
print("</div>")
 | 
			
		||||
 | 
			
		||||
print("<div id='needs'>")
 | 
			
		||||
print("<h2>Needs</h2>")
 | 
			
		||||
print("<ul>")
 | 
			
		||||
 | 
			
		||||
@ -79,10 +66,8 @@ for sub_recipe in sub_recipes:
 | 
			
		||||
        print(f"  <li>{ingredient.name}</li>")
 | 
			
		||||
 | 
			
		||||
print("</ul>")
 | 
			
		||||
print("</div>")
 | 
			
		||||
 | 
			
		||||
print("<div id='preparation'>")
 | 
			
		||||
print("<h2>Preparation</h2>")
 | 
			
		||||
print("<h2>Steps</h2>")
 | 
			
		||||
print("<ol>")
 | 
			
		||||
 | 
			
		||||
for sub_recipe in sub_recipes:
 | 
			
		||||
@ -91,14 +76,9 @@ for sub_recipe in sub_recipes:
 | 
			
		||||
        print(f"  <li>{task}</li>")
 | 
			
		||||
 | 
			
		||||
print("</ol>")
 | 
			
		||||
print("</div>")
 | 
			
		||||
 | 
			
		||||
print("<div id='tables'>")
 | 
			
		||||
print("<h2>Tabular Layout</h2>")
 | 
			
		||||
 | 
			
		||||
for sub_recipe in sub_recipes:
 | 
			
		||||
    print("<p>")
 | 
			
		||||
    print("<table cellspacing='0' border=true>")
 | 
			
		||||
    print("<table cellspacing='0'>")
 | 
			
		||||
 | 
			
		||||
    output_rows = []
 | 
			
		||||
 | 
			
		||||
@ -114,9 +94,3 @@ for sub_recipe in sub_recipes:
 | 
			
		||||
        print("</tr>")
 | 
			
		||||
 | 
			
		||||
    print("</table>")
 | 
			
		||||
    print("</p>")
 | 
			
		||||
 | 
			
		||||
print("</div>")
 | 
			
		||||
 | 
			
		||||
print("</body>")
 | 
			
		||||
print("</html>")
 | 
			
		||||
							
								
								
									
										89
									
								
								test.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										89
									
								
								test.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,89 @@
 | 
			
		||||
from anytree import Node
 | 
			
		||||
from anytree import RenderTree
 | 
			
		||||
from anytree import LevelOrderIter, LevelOrderGroupIter
 | 
			
		||||
from anytree.search import find, findall
 | 
			
		||||
 | 
			
		||||
cut = Node("cut")
 | 
			
		||||
cool = Node("cool", parent=cut)
 | 
			
		||||
press = Node("press into pan", parent=cool)
 | 
			
		||||
stir = Node("stir until coated", parent=press)
 | 
			
		||||
cereal = Node("crispy rice cereal (6 cups)", parent=stir)
 | 
			
		||||
melt_stir = Node("stir until melted", parent=stir)
 | 
			
		||||
marshmellows = Node("marshmellows (10 oz.)", parent=melt_stir)
 | 
			
		||||
melt = Node("melt", parent=melt_stir)
 | 
			
		||||
butter = Node("butter (3 tbsp)", parent=melt)
 | 
			
		||||
 | 
			
		||||
output_lines = {}
 | 
			
		||||
leaves = [node for node in sorted(cut.leaves, key=lambda node: node.depth, reverse=True)]
 | 
			
		||||
 | 
			
		||||
max_leaf_name_length = max([len(leaf.name) for leaf in leaves])
 | 
			
		||||
 | 
			
		||||
for leaf in leaves:
 | 
			
		||||
    line = f"| {leaf.name.ljust(max_leaf_name_length)} |"
 | 
			
		||||
    output_lines[leaf] = line
 | 
			
		||||
 | 
			
		||||
max_depth = max([leaf.depth for leaf in leaves])
 | 
			
		||||
 | 
			
		||||
for cur_depth in range(max_depth-1, -1, -1):
 | 
			
		||||
    max_name_length = max([len(node.name) for node in findall(cut, lambda node: node.depth == cur_depth and not node in cut.leaves)])
 | 
			
		||||
    max_name_length = max(max_name_length, 10)
 | 
			
		||||
    for leaf in leaves:
 | 
			
		||||
        task = find(cut, lambda node: node.depth == cur_depth and node in leaf.path)
 | 
			
		||||
        if task and not task in cut.leaves:
 | 
			
		||||
            output_lines[leaf] += f" {task.name.center(max_name_length)} |"
 | 
			
		||||
        else:
 | 
			
		||||
            output_lines[leaf] += f" {"".ljust(max_name_length)}  "
 | 
			
		||||
 | 
			
		||||
for key in output_lines.keys():
 | 
			
		||||
    print(output_lines[key])
 | 
			
		||||
 | 
			
		||||
print("")
 | 
			
		||||
 | 
			
		||||
output_lines = [""] * len(cut.leaves)
 | 
			
		||||
 | 
			
		||||
cur_line = 0
 | 
			
		||||
last_depth = cut.depth
 | 
			
		||||
for node in LevelOrderIter(cut):
 | 
			
		||||
    if node in cut.leaves:
 | 
			
		||||
        continue
 | 
			
		||||
    if last_depth != node.depth:
 | 
			
		||||
        cur_line = 0
 | 
			
		||||
 | 
			
		||||
    output_lines[cur_line] = f" {node.name} " + output_lines[cur_line]
 | 
			
		||||
    cur_line += len(node.children) + 1
 | 
			
		||||
    last_depth = node.depth
 | 
			
		||||
 | 
			
		||||
for line in output_lines:
 | 
			
		||||
    print(line)
 | 
			
		||||
 | 
			
		||||
rows = []
 | 
			
		||||
leaves = []
 | 
			
		||||
 | 
			
		||||
for i in range(len(cut.leaves)):
 | 
			
		||||
    rows.append([])
 | 
			
		||||
    leaves.append({"name":"wrong"})
 | 
			
		||||
 | 
			
		||||
cur_row = 0
 | 
			
		||||
for level in LevelOrderGroupIter(cut):
 | 
			
		||||
    cur_row = 0
 | 
			
		||||
 | 
			
		||||
    for node in reversed(level):
 | 
			
		||||
        leaf_count = len(node.leaves)
 | 
			
		||||
        if node in cut.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
 | 
			
		||||
 | 
			
		||||
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(cell)
 | 
			
		||||
    print("</tr>")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user