Answer architecture round 1: 3 🔴, 7 🟡; read the items table by column name, let an item wait while its blocker is held in any order, fill this plan's blockers, file the judge's bundled rung as item 153
This commit is contained in:
+63
-43
@@ -53,6 +53,8 @@ A valid todo.md; ## Details and each item's detail are optional:
|
||||
loop. An ID at or above Next ID fails; one below it that the table no longer holds was finished
|
||||
or deleted, and is dropped.
|
||||
- ## Items holds the table and nothing else: an order goes in the scores or the Blocked by column.
|
||||
- The table holds the columns above, a table from before Blocked by or C joined read by column name;
|
||||
a column this version does not know fails, rather than be dropped.
|
||||
|
||||
Exit 1, saying why, on bad arguments, a file that cannot be read or written, a todo.md, README.md or
|
||||
docs/decisions.md that breaks the rules above, an item that is not exempt scoring under the bar, or
|
||||
@@ -65,7 +67,7 @@ import pathlib
|
||||
import re
|
||||
import sys
|
||||
|
||||
from todo_table import BLOCKED_BY, cells, Item, ITEMS_HEADER, parse_row, table_rows, WITHOUT_BLOCKERS, WITHOUT_C
|
||||
from todo_table import BLOCKED_BY, cells, COLUMNS, DIVIDER, HEADER_START, Item, ITEMS_HEADER, items_table, parse_row, Table, table_rows
|
||||
from failure import Failure
|
||||
from markdown_text import FENCE, sections
|
||||
from file_writes import replace_file
|
||||
@@ -173,9 +175,24 @@ def rank(text: str, readme: str, decisions: str) -> Ranked | Failure:
|
||||
table = read_items(lines, scoring)
|
||||
if isinstance(table, Failure):
|
||||
return table
|
||||
items, start = table
|
||||
items, found = table
|
||||
if uncited := check_citations(items, decisions):
|
||||
return uncited
|
||||
scored = ordered(items, scoring)
|
||||
if isinstance(scored, Failure):
|
||||
return scored
|
||||
# Every table gets the header of COLUMNS, so one from before BLOCKED_BY joined gains the column.
|
||||
lines[found.header:found.start] = [ITEMS_HEADER, DIVIDER]
|
||||
lines[found.start:found.start + len(scored)] = [render_item(item, value) for item, value in scored]
|
||||
ranked = rank_details("\n".join(lines), [item for item, _ in scored])
|
||||
if isinstance(ranked, Failure):
|
||||
return ranked
|
||||
under_bar = [under_the_bar(item, value, scoring.bar, items) for item, value in scored if not item.exempt and round(value, 1) < scoring.bar]
|
||||
return Ranked(ranked, under_bar)
|
||||
|
||||
|
||||
def ordered(items: list[Item], scoring: Scoring) -> list[tuple[Item, float]] | Failure:
|
||||
"""Each item with its score, by release, then security first, then by score from highest, then by ID; then each blocker just above the first item it blocks."""
|
||||
by_score = []
|
||||
for item in items:
|
||||
value = score(scoring.formula, item, scoring.weights)
|
||||
@@ -183,25 +200,18 @@ def rank(text: str, readme: str, decisions: str) -> Ranked | Failure:
|
||||
return value
|
||||
by_score.append((item, value))
|
||||
by_score.sort(key=lambda pair: (pair[0].release, pair[0].exempt != "security", -pair[1], pair[0].id))
|
||||
scored = blockers_first(by_score, scoring.next_id)
|
||||
if isinstance(scored, Failure):
|
||||
return scored
|
||||
# A table without BLOCKED_BY gets it here.
|
||||
lines[start - 2:start] = [ITEMS_HEADER, "|" + "---|" * ITEMS_HEADER.count(" | ") + "---|"]
|
||||
lines[start:start + len(scored)] = [render_item(item, value) for item, value in scored]
|
||||
ranked = rank_details("\n".join(lines), [item for item, _ in scored])
|
||||
if isinstance(ranked, Failure):
|
||||
return ranked
|
||||
under_bar = [f"item {item.id} scores {value:.1f}, under the bar {scoring.bar:g}; delete it and any item that needs it, or mark it with a reason the bar does not apply" for item, value in scored if not item.exempt and round(value, 1) < scoring.bar]
|
||||
return Ranked(ranked, under_bar)
|
||||
return blockers_first(by_score)
|
||||
|
||||
|
||||
def under_the_bar(item: Item, value: float, bar: float, items: list[Item]) -> str:
|
||||
blocks = [str(other.id) for other in items if item.id in other.blocked_by]
|
||||
blocked = f" and the items it blocks, {', '.join(blocks)}" if blocks else ""
|
||||
return f"item {item.id} scores {value:.1f}, under the bar {bar:g}; delete it{blocked}, or mark it with a reason the bar does not apply"
|
||||
|
||||
|
||||
def read_scoring(text: str, lines: list[str], readme: str) -> Scoring | Failure:
|
||||
if ITEMS_HEADER not in lines and WITHOUT_BLOCKERS not in lines and "## Items" not in lines:
|
||||
if "## Items" not in lines and items_table(lines) is None:
|
||||
return Failure("todo.md is not in the scored-table format; rank_todo.py --help shows a valid one")
|
||||
# A table without C fails, so no chunk starts on an unscored item.
|
||||
if WITHOUT_C in lines:
|
||||
return Failure("todo.md's items table has no C column; add one after S, and score each item's C from 1 to 10 per rank_todo.py --help")
|
||||
goals = readme_goals(readme)
|
||||
if isinstance(goals, Failure):
|
||||
return goals
|
||||
@@ -220,17 +230,24 @@ def read_scoring(text: str, lines: list[str], readme: str) -> Scoring | Failure:
|
||||
return Scoring(formula, bar, int(next_id), weights)
|
||||
|
||||
|
||||
def read_items(lines: list[str], scoring: Scoring) -> tuple[list[Item], int] | Failure:
|
||||
"""The items, and the index of the first row in lines."""
|
||||
header = WITHOUT_BLOCKERS if WITHOUT_BLOCKERS in lines and ITEMS_HEADER not in lines else ITEMS_HEADER
|
||||
table = table_rows(lines, header)
|
||||
def read_items(lines: list[str], scoring: Scoring) -> tuple[list[Item], Table] | Failure:
|
||||
"""The items, and the table they were read from."""
|
||||
table = items_table(lines)
|
||||
if table is None:
|
||||
return Failure(f"## Items holds no table whose header opens {HEADER_START}; rank_todo.py --help shows a valid one")
|
||||
if isinstance(table, Failure):
|
||||
return table
|
||||
if beside_table(lines, table.start - 2, table.start + len(table.rows)):
|
||||
# A table without C fails, so no chunk starts on an unscored item.
|
||||
if "C" not in table.columns:
|
||||
return Failure("todo.md's items table has no C column; add one after S, and score each item's C from 1 to 10 per rank_todo.py --help")
|
||||
# Ranking writes COLUMNS, so it would drop a column it does not know.
|
||||
if unknown := [column for column in table.columns if column not in COLUMNS]:
|
||||
return Failure(f"todo.md's items table has the column {', '.join(unknown)}, which this rank_todo.py does not know; move this machine to the release that added it")
|
||||
if beside_table(lines, table.header, table.start + len(table.rows)):
|
||||
return Failure(f"## Items holds text beside its table; an order goes in the scores or the {BLOCKED_BY} column, and a note in the item's detail")
|
||||
items = []
|
||||
for row in table.rows:
|
||||
item = parse_item(row, scoring.weights, header)
|
||||
item = parse_item(row, scoring.weights, table.columns)
|
||||
if isinstance(item, Failure):
|
||||
return item
|
||||
items.append(item)
|
||||
@@ -239,7 +256,10 @@ def read_items(lines: list[str], scoring: Scoring) -> tuple[list[Item], int] | F
|
||||
return Failure("two items share an ID")
|
||||
if ids and max(ids) >= scoring.next_id:
|
||||
return Failure(f"item {max(ids)} is not under Next ID {scoring.next_id}; raise Next ID past every ID")
|
||||
return items, table.start
|
||||
for item in items:
|
||||
if unheld := [blocker for blocker in item.blocked_by if blocker >= scoring.next_id]:
|
||||
return Failure(f"item {item.id} is blocked by item {unheld[0]}, which is not under Next ID {scoring.next_id}; name an item the table holds or held")
|
||||
return items, table
|
||||
|
||||
|
||||
def beside_table(lines: list[str], header: int, end: int) -> bool:
|
||||
@@ -250,40 +270,40 @@ def beside_table(lines: list[str], header: int, end: int) -> bool:
|
||||
return any(line.strip() for line in lines[heading + 1:header] + section)
|
||||
|
||||
|
||||
def blockers_first(scored: list[tuple[Item, float]], next_id: int) -> list[tuple[Item, float]] | Failure:
|
||||
def blockers_first(scored: list[tuple[Item, float]]) -> list[tuple[Item, float]] | Failure:
|
||||
"""scored, each blocker moved up to just above the first item it blocks; a blocker the table no longer holds was finished or deleted, and is dropped."""
|
||||
held = {item.id: (item, value) for item, value in scored}
|
||||
position = {item.id: index for index, (item, _) in enumerate(scored)}
|
||||
for item, _ in scored:
|
||||
for blocker in item.blocked_by:
|
||||
if blocker >= next_id:
|
||||
return Failure(f"item {item.id} is blocked by item {blocker}, which is not under Next ID {next_id}; name an item the table holds or held")
|
||||
if blocker == item.id:
|
||||
return Failure(f"item {item.id} is blocked by itself; drop {item.id} from its {BLOCKED_BY}")
|
||||
if blocker in held and held[blocker][0].release > item.release:
|
||||
later, earlier = release_name(held[blocker][0]), release_name(item)
|
||||
return Failure(f"item {item.id} is blocked by item {blocker}, which release {later} ships after release {earlier}; move item {blocker} to release {earlier} or earlier, or item {item.id} to release {later} or later")
|
||||
ordered: list[tuple[Item, float]] = []
|
||||
result: list[tuple[Item, float]] = []
|
||||
for item, _ in scored:
|
||||
if looped := place(item.id, held, position, ordered, []):
|
||||
return looped
|
||||
return ordered
|
||||
placed = place(item.id, held, position, {placed.id for placed, _ in result}, [])
|
||||
if isinstance(placed, Failure):
|
||||
return placed
|
||||
result += placed
|
||||
return result
|
||||
|
||||
|
||||
def place(id_: int, held: dict[int, tuple[Item, float]], position: dict[int, int], ordered: list[tuple[Item, float]], path: list[int]) -> Failure | None:
|
||||
"""Appends to ordered the item id_ after its blockers, each in score order; path holds the items waiting on this one, to find a loop."""
|
||||
def place(id_: int, held: dict[int, tuple[Item, float]], position: dict[int, int], placed: set[int], path: list[int]) -> list[tuple[Item, float]] | Failure:
|
||||
"""The item id_ preceded by those of its blockers not yet placed, each in score order, and theirs before them; path holds the items waiting on this one, to find a loop."""
|
||||
if id_ in path:
|
||||
loop = sorted(path[path.index(id_):])
|
||||
return Failure(f"items {', '.join(map(str, loop[:-1]))} and {loop[-1]} block each other in a loop; drop one of the blockers")
|
||||
if any(placed.id == id_ for placed, _ in ordered):
|
||||
return None
|
||||
if id_ in placed:
|
||||
return []
|
||||
item, value = held[id_]
|
||||
blockers = sorted((blocker for blocker in set(item.blocked_by) if blocker in held), key=position.__getitem__)
|
||||
added: list[tuple[Item, float]] = []
|
||||
for blocker in blockers:
|
||||
if looped := place(blocker, held, position, ordered, [*path, id_]):
|
||||
return looped
|
||||
ordered.append((dataclasses.replace(item, blocked_by=tuple(sorted(blockers))), value))
|
||||
return None
|
||||
before = place(blocker, held, position, placed | {entry.id for entry, _ in added}, [*path, id_])
|
||||
if isinstance(before, Failure):
|
||||
return before
|
||||
added += before
|
||||
return [*added, (dataclasses.replace(item, blocked_by=tuple(sorted(blockers))), value)]
|
||||
|
||||
|
||||
def release_name(item: Item) -> str:
|
||||
@@ -415,8 +435,8 @@ def parse_weights(lines: list[str], goals: list[str]) -> dict[str, float] | Fail
|
||||
return weights
|
||||
|
||||
|
||||
def parse_item(row: str, weights: dict[str, float], header: str) -> Item | Failure:
|
||||
item = parse_row(row, header)
|
||||
def parse_item(row: str, weights: dict[str, float], columns: tuple[str, ...]) -> Item | Failure:
|
||||
item = parse_row(row, columns)
|
||||
if isinstance(item, Failure):
|
||||
return item
|
||||
if unknown := [goal for goal in item.goals if goal not in weights]:
|
||||
|
||||
Reference in New Issue
Block a user