diff --git a/bin/rank_todo.py b/bin/rank_todo.py index fa8ab090..82fb173e 100755 --- a/bin/rank_todo.py +++ b/bin/rank_todo.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -"""Score todo.md's items by the formula it states, weighing each 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 rank in README.md, and hold them to its bar. usage: rank_todo.py [--check] [todo.md] @@ -83,7 +83,7 @@ OPERATORS = (ast.Add, ast.BinOp, ast.Div, ast.Expression, ast.Load, ast.Mult, as WEIGHTS_HEADER = "| Goal | W |" # 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}; reshaping it to the shape rank_todo.py --help shows, after marking README.md's goals **Maintain:** or **Achieve:**, is a chunk of its own" +RESHAPE = f"the items table scores A and G against goal weights todo.md states, the shape before {MOVES}; reshaping it to the shape rank_todo.py --help shows, after marking README.md's goals **Maintain:** or **Achieve:** and setting the bar on the new scale under every item to keep, is a chunk of its own" @dataclasses.dataclass(frozen=True) @@ -403,12 +403,10 @@ def readme_goals(readme: str) -> list[tuple[str, Mark]] | Failure: def achieve_weights(goals: list[tuple[str, Mark]]) -> dict[str, float] | Failure: - """Each achieve goal's sum-reciprocal weight: the one ranked k of n in reading order weighs (1/k) / (1 + 1/2 + ... + 1/n).""" + """Each achieve goal's sum-reciprocal weight: the one ranked k of n in reading order weighs (1/k) / (1 + 1/2 + ... + 1/n). None where README.md marks no goal **Achieve:**, so every item scores V 0 and only an exempt one passes the bar.""" if unmarked := [number for number, mark in goals if mark is None]: return Failure(f"README.md's goal {unmarked[0]} is marked neither **Maintain:** nor **Achieve:**; mark it, as ~/.claude/rules/goals-and-audience.md says, so rank_todo.py knows which goals score") achieve = [number for number, mark in goals if mark == "Achieve"] - if not achieve: - return Failure("README.md marks no goal **Achieve:**, so no item can score; ~/.claude/rules/goals-and-audience.md says how to mark the goals to pursue") harmonic = sum(1 / rank for rank in range(1, len(achieve) + 1)) return {number: 1 / rank / harmonic for rank, number in enumerate(achieve, 1)} @@ -468,8 +466,12 @@ def evaluate(node: ast.AST, values: dict[str, float]) -> float: 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. + 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): + return math.nan try: - return math.pow(evaluate(left, values), evaluate(right, values)) + return math.pow(base, exponent) except (OverflowError, ValueError): return math.nan return math.nan diff --git a/bin/test_rank_todo.py b/bin/test_rank_todo.py index f3682ed1..ef50e9cb 100644 --- a/bin/test_rank_todo.py +++ b/bin/test_rank_todo.py @@ -141,7 +141,8 @@ class Rank(unittest.TestCase): self.assertEqual(rank_todo.readme_goals(README), [("1", "Achieve"), ("2", "Maintain"), ("2.1", "Achieve")]) self.assertEqual(rank_todo.readme_goals(README.replace(" 1.", "\t1.")), [("1", "Achieve"), ("2", "Maintain"), ("2.1", "Achieve")]) self.assertEqual(rank_todo.readme_goals(README.replace("2. **Maintain:** Correct.", "```\n## Goals\n```\n2. Correct.")), [("1", "Achieve"), ("2", None), ("2.1", "Achieve")]) - self.assertIsInstance(rank_todo.readme_goals(README.replace("2. **Maintain:**", "1. **Maintain:**")), Failure) + self.assertEqual(rank_todo.readme_goals(README.replace("2. **Maintain:**", "1. **Maintain:**")), Failure("README.md's ## Goals numbers 1, 1, 1.1; number each goal by its position")) + self.assertEqual(rank_todo.readme_goals("## Goals\n\n1. **Achieve:** A.\n 1. **Achieve:** B.\n\t2. **Achieve:** C.\n"), [("1", "Achieve"), ("1.1", "Achieve"), ("1.2", "Achieve")]) def test_refuses_a_goal_it_cannot_weigh(self): cases = { @@ -149,12 +150,19 @@ class Rank(unittest.TestCase): (TODO.replace("2.1 by 8, 1 by 6", "2.1 by 8, 3 by 6"), README): "item 3 cites goal 3, which README.md's ## Goals does not number", (TODO.replace("2.1 by 8, 1 by 6", "2.1 by 8, 2.1 by 6"), README): "item 3 cites goal 2.1 twice; name each goal once", (TODO, README.replace("**Maintain:** Correct", "Correct")): "README.md's goal 2 is marked neither **Maintain:** nor **Achieve:**; mark it, as ~/.claude/rules/goals-and-audience.md says, so rank_todo.py knows which goals score", - (TODO, README.replace("**Achieve:**", "**Maintain:**")): "README.md marks no goal **Achieve:**, so no item can score; ~/.claude/rules/goals-and-audience.md says how to mark the goals to pursue", } for (todo, readme), reason in cases.items(): with self.subTest(reason=reason): self.assertEqual(self.ranked(todo, readme), Failure(reason)) + def test_scores_every_item_zero_where_readme_marks_no_goal_achieve(self): + ranked = self.ranked(TODO.replace("2.1 by 8, 1 by 6", "").replace("| 2.1 by 1 |", "| |").replace("| 1 by 9 |", "| |"), README.replace("**Achieve:**", "**Maintain:**")) + self.assertEqual([line.split(" scores ")[0] for line in ranked.under_bar], ["item 2", "item 3", "item 1"]) + + def test_accepts_a_move_of_ten_and_a_variable_base(self): + ranked = self.ranked(TODO.replace("| 1 by 9 |", "| 1 by 10 |").replace("100 * V / 2**S", "V * S ** 2")) + self.assertIn("| **Ship the later thing.** | 1 | 1 | 3 | 1 by 10 | 6.7 |", ranked.text) + 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")) @@ -164,6 +172,9 @@ class Rank(unittest.TestCase): with self.subTest(formula=formula): self.assertIsInstance(self.ranked(TODO.replace("100 * V / 2**S", formula)), Failure) self.assertIn("| 1 by 9 | 2.0 |", self.ranked(TODO.replace("100 * V / 2**S", "R + S / S")).text) + self.assertEqual(self.ranked(TODO.replace("100 * V / 2**S", "1 ** (1 / (S - S))")), Failure("the formula gives item 1 no finite score")) + self.assertEqual(self.ranked(TODO.replace("100 * V / 2**S", "W")), Failure("the formula 'W' may hold only numbers, R, S, V, + - * / ** and parentheses")) + self.assertEqual(self.ranked(TODO.replace("2.1 by 8, 1 by 6", "2.1: 8, 1: 6")), Failure("item 3's Moves '2.1: 8, 1: 6' is not empty or goals such as `1.1 by 8, 1.2 by 5`, each moved by a whole number from 1 to 10")) def test_refuses_a_file_off_the_format(self): row = "| 2 | 0.1.0 | | | **Tighten the tests.** | 2 | 4 | 3 | 2.1 by 1 | 0 |" @@ -219,7 +230,7 @@ class OldShape(unittest.TestCase): self.assertEqual(self.ranked(OLD_SHAPE.replace("| 2.1, 1 | 0 |", "| 2.1, 9 | 0 |")), Failure("item 3 cites goal 9, which the goal weights do not list")) def test_refuses_a_formula_over_v(self): - self.assertIsInstance(self.ranked(OLD_SHAPE.replace("-R - S/4 + 2*A + 2*G*W", "V")), Failure) + self.assertEqual(self.ranked(OLD_SHAPE.replace("-R - S/4 + 2*A + 2*G*W", "V")), Failure("the formula 'V' may hold only numbers, A, G, R, S, W, + - * / ** and parentheses")) def test_asks_for_a_c_column_in_a_table_without_one(self): legacy = OLD_SHAPE.split("## Items")[0] + "## Items\n\n| ID | Release | Exempt | Item | R | S | A | G | Goals | Score |\n|---|---|---|---|---|---|---|---|---|---|\n| 2 | 0.1.0 | | **Tighten the tests.** | 2 | 4 | 5 | 5 | 2.1 | 0 |\n"