Answer architecture round 1: 5 🔴, 8 🟡; one Shape per table shape, one moves field, end item 156 for the old shape

This commit is contained in:
2026-10-16 00:41:39 +02:00
parent b9c9162093
commit e2fe71b842
8 changed files with 205 additions and 99 deletions
+102 -33
View File
@@ -1,24 +1,72 @@
import dataclasses
import re
import typing
from failure import Failure
# Each of R, S, C, A and G, and each goal's move, runs from 1 to TOP_SCORE.
# Each score a Shape names, and each goal's move, runs from 1 to TOP_SCORE.
TOP_SCORE = 10
EXEMPTIONS = ("", "decision", "defect", "owner", "principle", "question", "security")
# The column naming the items that block an item: each must be done before the item starts.
BLOCKED_BY = "Blocked by"
# The column naming each achieve goal an item moves and how far, such as `1.1 by 8, 1.2 by 5`.
MOVES = "Moves"
# The columns rank_todo writes, in order. A reader takes cells by column name, so it reads a table from before BLOCKED_BY or C joined, or with a column a later release adds.
COLUMNS = ("ID", "Release", BLOCKED_BY, "Exempt", "Item", "R", "S", "C", MOVES, "Score")
# The columns of a table from before MOVES replaced A, G and Goals, which rank_todo still ranks until its project reshapes it.
OLD_COLUMNS = ("ID", "Release", BLOCKED_BY, "Exempt", "Item", "R", "S", "C", "A", "G", "Goals", "Score")
# The columns without which no row can be read; a missing BLOCKED_BY reads as blocked by nothing, a missing C as no C.
NEEDED = ("ID", "Release", "Exempt", "Item", "R", "S", MOVES)
OLD_NEEDED = ("ID", "Release", "Exempt", "Item", "R", "S", "A", "G", "Goals")
# How the items table's header opens, which tells it from the goal weights table.
# How the items table's header opens, which tells it from any other table in todo.md.
HEADER_START = "| ID | Release |"
# Each goal an item names, in its order, with how far the item moves it; None in OLD_SHAPE, whose Goals cell names no move.
Moves = tuple[tuple[str, int | None], ...]
@dataclasses.dataclass(frozen=True)
class Shape:
"""A shape of the items table: its columns, and how its goals cell reads and writes."""
# The score columns, in the order the table holds them.
score_columns: tuple[str, ...]
# The column naming the goals an item serves; read_goals reads it, given the item's ID to name in a failure, and write_goals writes it back.
goals_column: str
read_goals: typing.Callable[[str, str], Moves | Failure]
write_goals: typing.Callable[[Moves], str]
@property
def columns(self) -> tuple[str, ...]:
"""Every column of the shape, in order. A reader takes cells by column name, so it reads a table from before BLOCKED_BY or C joined, or with a column a later release adds."""
return ("ID", "Release", BLOCKED_BY, "Exempt", "Item", *self.score_columns, self.goals_column, "Score")
@property
def needed(self) -> tuple[str, ...]:
"""The columns without which no row can be read: a missing BLOCKED_BY reads as blocked by nothing, a missing C as no C, and a missing Score as no score yet."""
return tuple(column for column in self.columns if column not in (BLOCKED_BY, "C", "Score"))
def parse_moves(id_: str, cell: str) -> Moves | Failure:
"""A Moves cell: empty, or each goal the item moves, named once, and how far."""
matches = [re.fullmatch(r"([0-9]+(?:\.[0-9]+)*) by ([0-9]+)", move) for move in cell.split(", ")] if cell else []
moves = tuple((match.group(1), int(match.group(2))) for match in matches if match)
if len(moves) < len(matches) or any(not 1 <= by <= TOP_SCORE for _, by in moves):
return Failure(f"item {id_}'s {MOVES} {cell!r} is not empty or goals such as `1.1 by 8, 1.2 by 5`, each moved by a whole number from 1 to {TOP_SCORE}")
goals = [goal for goal, _ in moves]
if repeated := [goal for goal in goals if goals.count(goal) > 1]:
return Failure(f"item {id_} cites goal {repeated[0]} twice; name each goal once")
return moves
def write_moves(moves: Moves) -> str:
return ", ".join(f"{goal} by {by}" for goal, by in moves)
def parse_cited(id_: str, cell: str) -> Moves:
"""An old-shape Goals cell, such as `2.1, 1`: each goal it cites, with no move."""
return tuple((goal.strip(), None) for goal in cell.split(","))
def write_cited(moves: Moves) -> str:
return ", ".join(goal for goal, _ in moves)
# The current shape.
SHAPE = Shape(("R", "S", "C"), MOVES, parse_moves, write_moves)
# The shape from before MOVES replaced A, G and Goals, which older todo.md files hold.
OLD_SHAPE = Shape(("R", "S", "C", "A", "G"), "Goals", parse_cited, write_cited)
@dataclasses.dataclass(frozen=True)
@@ -28,10 +76,8 @@ class Item:
exempt: str
tagline: str
scores: dict[str, int]
goals: tuple[str, ...]
moves: Moves
blocked_by: tuple[int, ...]
# How far the item moves each of goals, in their order; empty in an old-shape table.
moves: tuple[int, ...] = ()
@property
def risk_and_complexity(self) -> int:
@@ -52,6 +98,12 @@ class Table:
return self.header + 2
def shape_of(columns: tuple[str, ...]) -> Shape:
"""The items table's shape: OLD_SHAPE where columns hold one only that shape has and no MOVES, else SHAPE, so a misspelt MOVES is named as missing."""
old_only = set(OLD_SHAPE.columns) - set(SHAPE.columns)
return OLD_SHAPE if MOVES not in columns and old_only.intersection(columns) else SHAPE
def items(todo: str) -> list[Item] | None | Failure:
"""Each row of a todo.md's items table; None where todo.md holds no items table."""
table = items_table(todo.splitlines())
@@ -59,7 +111,7 @@ def items(todo: str) -> list[Item] | None | Failure:
return table
found = []
for row in table.rows:
item = parse_row(row, table.columns)
item = parse_row(row, table)
if isinstance(item, Failure):
return item
found.append(item)
@@ -78,8 +130,7 @@ def items_table(lines: list[str]) -> Table | None | Failure:
return table
if repeated := sorted({column for column in table.columns if table.columns.count(column) > 1}):
return Failure(f"todo.md's items table names the column {', '.join(repeated)} twice; keep one")
needed = NEEDED if MOVES in table.columns else OLD_NEEDED
if missing := [column for column in needed if column not in table.columns]:
if missing := [column for column in shape_of(table.columns).needed if column not in table.columns]:
return Failure(f"todo.md's items table has no {', '.join(missing)} column; rank_todo.py --help shows a valid one")
return table
@@ -110,12 +161,30 @@ def cells(row: str, count: int) -> list[str] | Failure:
return found if len(found) == count else Failure(f"this row needs {count} cells: {row}")
def parse_row(row: str, columns: tuple[str, ...]) -> Item | Failure:
"""An item row under columns, every cell checked but the goals it cites."""
found = cells(row, len(columns))
def parse_row(row: str, table: Table) -> Item | Failure:
"""An item row of table, every cell checked but whether README.md numbers and weighs the goals it names."""
found = cells(row, len(table.columns))
if isinstance(found, Failure):
return found
named = dict(zip(columns, found))
named = dict(zip(table.columns, found))
if misformed := check_cells(row, named):
return misformed
shape = shape_of(table.columns)
scores = parse_scores(named["ID"], named, shape.score_columns)
if isinstance(scores, Failure):
return scores
blocked_by = parse_blockers(named["ID"], named.get(BLOCKED_BY, ""))
if isinstance(blocked_by, Failure):
return blocked_by
moves = shape.read_goals(named["ID"], named[shape.goals_column])
if isinstance(moves, Failure):
return moves
major, minor, patch = (int(part) for part in named["Release"].split("."))
return Item(int(named["ID"]), (major, minor, patch), named["Exempt"], named["Item"], scores, moves, blocked_by)
def check_cells(row: str, named: dict[str, str]) -> Failure | None:
"""Holds the row's ID, Release, Exempt and Item cells to the format: why one breaks it, None when none does."""
id_, release, exempt, tagline = named["ID"], named["Release"], named["Exempt"], named["Item"]
if not re.fullmatch(r"[0-9]+", id_) or int(id_) < 1:
return Failure(f"the ID {id_!r} is not a positive whole number: {row}")
@@ -125,25 +194,25 @@ def parse_row(row: str, columns: tuple[str, ...]) -> Item | Failure:
return Failure(f"item {id_}'s Exempt {exempt!r} is not empty or one of {', '.join(EXEMPTIONS[1:])}")
if not re.fullmatch(r"\*\*[^*].*[^*]\*\*", tagline):
return Failure(f"item {id_}'s Item cell is not one bold tagline: {tagline}")
return None
def parse_scores(id_: str, named: dict[str, str], names: tuple[str, ...]) -> dict[str, int] | Failure:
"""Each score of names the row holds; only C may be missing, since the table holds every needed column."""
scores: dict[str, int] = {}
for name in ("R", "S", "C", "A", "G"):
for name in names:
if name not in named:
continue
if not re.fullmatch(r"[0-9]+", named[name]) or not 1 <= int(named[name]) <= TOP_SCORE:
return Failure(f"item {id_}'s {name} {named[name]!r} is not a whole number from 1 to {TOP_SCORE}")
scores[name] = int(named[name])
blockers = named.get(BLOCKED_BY, "")
if not re.fullmatch(r"([0-9]+(, [0-9]+)*)?", blockers):
return Failure(f"item {id_}'s {BLOCKED_BY} {blockers!r} is not empty or IDs separated by \", \"")
blocked_by = tuple(int(blocker) for blocker in blockers.split(", ") if blocker)
return scores
def parse_blockers(id_: str, cell: str) -> tuple[int, ...] | Failure:
if not re.fullmatch(r"([0-9]+(, [0-9]+)*)?", cell):
return Failure(f"item {id_}'s {BLOCKED_BY} {cell!r} is not empty or IDs separated by \", \"")
blocked_by = tuple(int(blocker) for blocker in cell.split(", ") if blocker)
if int(id_) in blocked_by:
return Failure(f"item {id_} is blocked by itself; drop {id_} from its {BLOCKED_BY}")
major, minor, patch = (int(part) for part in release.split("."))
if MOVES not in named:
cited = tuple(goal.strip() for goal in named["Goals"].split(","))
return Item(int(id_), (major, minor, patch), exempt, tagline, scores, cited, blocked_by)
matches = [re.fullmatch(r"([0-9]+(?:\.[0-9]+)*) by ([0-9]+)", move) for move in named[MOVES].split(", ")] if named[MOVES] else []
moves = [(match.group(1), int(match.group(2))) for match in matches if match]
if len(moves) < len(matches) or any(not 1 <= by <= TOP_SCORE for _, by in moves):
return Failure(f"item {id_}'s {MOVES} {named[MOVES]!r} is not empty or goals such as `1.1 by 8, 1.2 by 5`, each moved by a whole number from 1 to {TOP_SCORE}")
return Item(int(id_), (major, minor, patch), exempt, tagline, scores, tuple(goal for goal, _ in moves), blocked_by, tuple(by for _, by in moves))
return blocked_by