Answer prose round 1: 0 🔴, 16 🟡; say position not rank, split the changelog entry, tighten messages and details
This commit is contained in:
+16
-14
@@ -1,14 +1,14 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Score todo.md's items by the formula it states, weighing each achieve goal by its rank in README.md, and hold them to its bar.
|
||||
"""Score todo.md's items by the formula it states, weighing each achieve goal by its position in README.md, and hold them to its bar.
|
||||
|
||||
usage: rank_todo.py [--check] [todo.md]
|
||||
|
||||
Reads todo.md (default: ./todo.md), the README.md beside it, and docs/decisions.md when there is
|
||||
one. Rewrites each item's Score from the formula and sorts the items by release; within a release,
|
||||
security rows come first, then defect rows, then the rest, each group by score from highest, then by ID. Then it moves each blocker,
|
||||
an item another's Blocked by cell names, that ranks below an item it blocks to just above the first
|
||||
such item. Orders the details to match. Adds the Blocked by column to a table
|
||||
without it. --check writes nothing and fails when the file is not ranked as it would be.
|
||||
security rows come first, then defect rows, then the rest, each group by score from highest, then
|
||||
by ID. Then it moves each blocker, an item another's Blocked by cell names, that ranks below an item
|
||||
it blocks to just above the first such item. Orders the details to match. Adds the Blocked by column
|
||||
to a table without it. --check writes nothing and fails when the file is not ranked as it would be.
|
||||
|
||||
A valid todo.md; ## Details and each item's detail are optional:
|
||||
|
||||
@@ -83,11 +83,11 @@ LARGEST_FORMULA = 200
|
||||
LONGEST_FORMULA = 4 * LARGEST_FORMULA
|
||||
OPERATORS = (ast.Add, ast.BinOp, ast.Div, ast.Expression, ast.Load, ast.Mult, ast.Pow, ast.Sub, ast.UAdd, ast.UnaryOp, ast.USub)
|
||||
WEIGHTS_HEADER = "| Goal | W |"
|
||||
# The Exempt reasons that rank an item ahead of the rest of its release, in order: a security finding, then a defect, which a 🔴 or a broken maintain goal is.
|
||||
# Exempt reasons that rank ahead of the rest of their release, in this order.
|
||||
FIRST = {"security": 0, "defect": 1}
|
||||
# How README.md marks a goal; None where it marks it neither way.
|
||||
Mark = typing.Literal["Maintain", "Achieve"] | None
|
||||
RESHAPE = f"the items table scores A and G against goal weights todo.md states, the shape before {MOVES}. Until it has the shape rank_todo.py --help shows, reshaping it is the project's only work, as ~/.claude/CLAUDE.md → 'Working loop' says: mark README.md's goals **Maintain:** or **Achieve:**, name each item's {MOVES}, and set the bar on the new scale under every item to keep"
|
||||
RESHAPE = f"todo.md's items table is in the old shape, scoring A and G against goal weights todo.md states. Until todo.md has the shape rank_todo.py --help shows, reshaping it is the project's only work, as ~/.claude/CLAUDE.md → 'Working loop' says: mark README.md's goals **Maintain:** or **Achieve:**, name each item's {MOVES}, and set the bar below the score of every item to keep"
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
@@ -98,7 +98,7 @@ class Scoring:
|
||||
next_id: int
|
||||
# README.md's goal numbers, in reading order.
|
||||
goals: list[str]
|
||||
# The weight of each goal an item may name: in OLD_SHAPE every goal, at the weight todo.md states; else each achieve goal, by its rank in README.md.
|
||||
# The weight of each goal an item may name: in OLD_SHAPE every goal, at the weight todo.md states; else each achieve goal, by its position in README.md.
|
||||
weights: dict[str, float]
|
||||
|
||||
|
||||
@@ -106,7 +106,7 @@ class Scoring:
|
||||
class Ranked:
|
||||
text: str
|
||||
under_bar: list[str]
|
||||
# The lines ranking prints and --check does not: that an old-shape table wants reshaping, and each blocker ID dropped because the table no longer holds it, since a mistyped ID is dropped the same way.
|
||||
# What ranking prints and --check does not: an old-shape table's call to reshape, a README with no achieve goal, and each dropped blocker ID, since a mistyped ID drops the same way.
|
||||
notes: list[str]
|
||||
|
||||
|
||||
@@ -221,7 +221,7 @@ def notes(items: list[Item], scoring: Scoring) -> list[str]:
|
||||
dropped = [f"item {item.id}: dropped {', '.join(map(str, gone))} from its {BLOCKED_BY}; the table no longer holds {'it' if len(gone) == 1 else 'them'}" for item in items if (gone := sorted(set(item.blocked_by) - held))]
|
||||
if scoring.shape is OLD_SHAPE:
|
||||
return [RESHAPE] + dropped
|
||||
# Said, since every item under the bar for that reason reads like one of low value.
|
||||
# Printed so the under-bar lines it causes do not read as items of low value.
|
||||
unweighed = [] if scoring.weights else ["README.md marks no goal **Achieve:**, so every item scores V 0"]
|
||||
return unweighed + dropped
|
||||
|
||||
@@ -271,7 +271,7 @@ def read_scoring(text: str, lines: list[str], readme: str, shape: Shape) -> Scor
|
||||
if shape is OLD_SHAPE:
|
||||
weights = parse_weights(lines, numbers)
|
||||
elif WEIGHTS_HEADER in lines:
|
||||
weights = Failure(f"todo.md holds a goal weights table beside a {MOVES} column; drop the table, since rank_todo.py weighs each achieve goal by its rank in README.md")
|
||||
weights = Failure(f"todo.md holds a goal weights table beside a {MOVES} column; drop the table, since rank_todo.py weighs each achieve goal by its position in README.md")
|
||||
else:
|
||||
weights = achieve_weights(goals)
|
||||
if isinstance(weights, Failure):
|
||||
@@ -299,7 +299,9 @@ def unknown_columns(table: Table) -> Failure | None:
|
||||
known = shape_of(table.columns).columns
|
||||
unknown = [column for column in table.columns if column not in known]
|
||||
if old := [column for column in unknown if column in OLD_SHAPE.columns]:
|
||||
return Failure(f"todo.md's items table has the column {', '.join(old)}, a column of the old shape, which {MOVES} replaced; drop it")
|
||||
listed = old[0] if len(old) == 1 else f"{', '.join(old[:-1])} and {old[-1]}"
|
||||
kind = f"a column of the old shape that {MOVES} replaced; drop it" if len(old) == 1 else f"columns of the old shape that {MOVES} replaced; drop them"
|
||||
return Failure(f"todo.md's items table has {listed}, {kind}")
|
||||
if unknown:
|
||||
return Failure(f"todo.md's items table has the column {', '.join(unknown)}, which this rank_todo.py does not know; the columns it knows are {', '.join(known)}. Rename it, or move this machine to the release that added it")
|
||||
return None
|
||||
@@ -473,7 +475,7 @@ def evaluate(node: ast.AST, values: dict[str, float]) -> float:
|
||||
divisor = evaluate(right, values)
|
||||
return evaluate(left, values) / divisor if divisor else math.nan
|
||||
case ast.BinOp(left, ast.Pow(), right):
|
||||
# Caught in place of checking first: a negative base with a fractional exponent, zero to a negative power and an overflow each raise, and three checks would read worse than one catch.
|
||||
# math.pow raises on a negative base with a fractional exponent, zero to a negative power, and overflow; one catch covers all three.
|
||||
base, exponent = evaluate(left, values), evaluate(right, values)
|
||||
# A nan from a zero divisor stays nan, though math.pow(1, nan) and math.pow(nan, 0) give 1.
|
||||
if math.isnan(base) or math.isnan(exponent):
|
||||
@@ -497,7 +499,7 @@ def variables(shape: Shape) -> tuple[str, ...]:
|
||||
|
||||
def score(formula: ast.Expression, item: Item, scoring: Scoring) -> float | Failure:
|
||||
if goal_term(scoring.shape) == "W":
|
||||
# An item serving several goals is weighed by the heaviest; summing them would reward citing more.
|
||||
# The old shape weighs an item by its heaviest cited goal.
|
||||
term = max(scoring.weights[goal] for goal, _ in item.moves)
|
||||
else:
|
||||
# Only an OLD_SHAPE item names a goal without a move.
|
||||
|
||||
@@ -178,7 +178,7 @@ class Rank(unittest.TestCase):
|
||||
|
||||
def test_asks_to_drop_a_goal_weights_table_from_a_table_with_moves(self):
|
||||
weighted = TODO.replace("## Items\n", "| Goal | W |\n|---|---|\n| 1 | 1.00 |\n\n## Items\n")
|
||||
self.assertEqual(self.ranked(weighted), Failure("todo.md holds a goal weights table beside a Moves column; drop the table, since rank_todo.py weighs each achieve goal by its rank in README.md"))
|
||||
self.assertEqual(self.ranked(weighted), Failure("todo.md holds a goal weights table beside a Moves column; drop the table, since rank_todo.py weighs each achieve goal by its position in README.md"))
|
||||
|
||||
def test_refuses_a_formula_that_is_more_than_arithmetic_over_the_scores(self):
|
||||
for formula in ("__import__('os')", "R.real", "X + 1", "C + 1", "A + V", "G + V", "W * V", "R ^ 2", "R +", "R / (S - 2)", "R\x00", "1" * 400, " + ".join(["R"] * 300), "-" * 100000 + "R", "10 ** 10 ** 10", "(-8) ** (1 / 3)", "0 ** -1"):
|
||||
@@ -252,7 +252,9 @@ class OldShape(unittest.TestCase):
|
||||
|
||||
def test_refuses_a_table_with_both_moves_and_goals(self):
|
||||
both = OLD_SHAPE.replace("| Goals | Score |\n|---|", "| Goals | Moves | Score |\n|---|---|").replace(" | 0 |\n", " | 1 by 1 | 0 |\n")
|
||||
self.assertEqual(self.ranked(both), Failure("todo.md's items table has the column A, G, Goals, a column of the old shape, which Moves replaced; drop it"))
|
||||
self.assertEqual(self.ranked(both), Failure("todo.md's items table has A, G and Goals, columns of the old shape that Moves replaced; drop them"))
|
||||
one = TODO.replace("| Moves | Score |\n|---|", "| Moves | G | Score |\n|---|---|").replace(" | 0 |\n", " | 1 | 0 |\n")
|
||||
self.assertEqual(self.ranked(one), Failure("todo.md's items table has G, a column of the old shape that Moves replaced; drop it"))
|
||||
|
||||
def test_names_the_goals_column_a_table_misspells_by_its_shape(self):
|
||||
misspelt = TODO.replace("| C | Moves |", "| C | Move |")
|
||||
|
||||
+2
-3
@@ -65,7 +65,6 @@ 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)
|
||||
@@ -101,7 +100,7 @@ class Table:
|
||||
|
||||
|
||||
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_SHAPE where the columns include A, G or Goals and not MOVES; else SHAPE, so a misspelt MOVES is reported 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
|
||||
|
||||
@@ -200,7 +199,7 @@ def check_cells(row: str, named: dict[str, str]) -> Failure | 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."""
|
||||
"""Each score in names that the row holds; only C may be missing, since the table holds every needed column."""
|
||||
scores: dict[str, int] = {}
|
||||
for name in names:
|
||||
if name not in named:
|
||||
|
||||
Reference in New Issue
Block a user