Answer stability round 1: 1 🔴, 6 🟡 (0 misread); drop scoring_runs, rename newest_scored and the Entry fields, unroll worse_places, add three tests, tighten the docstrings

This commit is contained in:
2026-11-08 19:39:18 +01:00
parent 690a7d4454
commit 8e64d30fd8
4 changed files with 61 additions and 47 deletions
+36 -38
View File
@@ -21,7 +21,7 @@ MID = "Mid A"
PLACE = re.compile(r"`([^`\s]*):\d+`(?: `(\w+)`)?")
RESTRUCTURE_FIRST = "would restructure first"
RULING = re.compile(r"^Ruling: (better|same|worse)$", re.M)
# A seat's label over its bullets: its name, then what the bullets are.
# A seat's label over its bullets: its name, then the listing's kind.
LABEL = re.compile(rf"^(.+), ({DECIDED_BY}|{HARDEST_FIRST}|{RESTRUCTURE_FIRST}):$")
SENIOR = "Maintainability senior"
TITLE = "# Comprehension history\n"
@@ -58,16 +58,16 @@ class Listing:
class Entry:
"""One paired ruling, which names the commit it was against and its ruling, or one scoring run, which names neither.
`votes` and `scores` hold table rows, each the cells after the first, by the first, with the header left out. `scores` is the table with a Mean row, `votes` any other: a ruling's votes, whose one cell in a ruling before PR #232 is the overall vote. A ruling before PR #255 holds both tables, and scoring seats' listings too."""
`score_rows` holds the rows of the table with a Mean row, and `vote_rows` those of any other table: each row's cells after the first, keyed by the first, without the header. `ruled_listings` holds the decided-by listings, and `scored_listings` the hardest-first and would-restructure-first ones."""
heading: str
time: str
commit: str
against: str | None
ruling: str | None
votes: dict[str, list[str]]
scores: dict[str, list[str]]
decided: list[Listing]
hardest: list[Listing]
vote_rows: dict[str, list[str]]
score_rows: dict[str, list[str]]
ruled_listings: list[Listing]
scored_listings: list[Listing]
@property
def paired(self) -> bool:
@@ -75,16 +75,16 @@ class Entry:
@property
def scored(self) -> bool:
"""Whether the entry counts as scoring the project: a scoring run, or a ruling from before PR #255 that holds scoring seats' places."""
"""Whether the entry scores the project: a scoring run, or a ruling that lists scoring seats' places, as a ruling from before PR #255 may, beside their table."""
return not self.paired or bool(self.scored_places)
@property
def ruled_places(self) -> list[Place]:
return [place for listing in self.decided for place in listing.places]
return [place for listing in self.ruled_listings for place in listing.places]
@property
def scored_places(self) -> list[Place]:
return [place for listing in self.hardest for place in listing.places]
return [place for listing in self.scored_listings for place in listing.places]
@dataclasses.dataclass(frozen=True)
@@ -135,10 +135,10 @@ def entries(history: str) -> list[Entry]:
listed = listings(body)
found.append(Entry(
heading, named.group(1), named.group(3), named.group(4), ruling.group(1) if ruling else None,
votes={name: cells for table in tables if MEAN not in table for name, cells in table.items()},
scores={name: cells for table in tables if MEAN in table for name, cells in table.items()},
decided=[listing for listing in listed if listing.kind == DECIDED_BY],
hardest=[listing for listing in listed if listing.kind != DECIDED_BY],
vote_rows={name: cells for table in tables if MEAN not in table for name, cells in table.items()},
score_rows={name: cells for table in tables if MEAN in table for name, cells in table.items()},
ruled_listings=[listing for listing in listed if listing.kind == DECIDED_BY],
scored_listings=[listing for listing in listed if listing.kind != DECIDED_BY],
))
return found
@@ -146,18 +146,18 @@ def entries(history: str) -> list[Entry]:
def latest_places(history: str) -> str | None:
"""The places seats found hard since the newest scored commit, under each entry's heading, oldest first; None when no place counts.
They start at the first of newest_scored's entries, so a rerun of one head adds to the run before it. Where no entry counts as scoring the project, they start at the oldest entry. From there, each entry gives every scoring seat's place, and each ruled place opening with a dimension its seat voted worse."""
They start at the first entry that scores that commit, so a rerun of one head adds to the run before it. Where no entry scores the project, they start at the oldest entry. From there, each entry gives every scoring seat's place, and each ruled place opening with a dimension its seat voted worse."""
found = by_time(entries(history))
newest = newest_scored(found)
newest = newest_commit_runs(found)
start = found.index(newest[0]) if newest else 0
parts = [placed(entry, worse_places(entry) + entry.hardest) for entry in found[start:]]
parts = [placed(entry, worse_places(entry) + entry.scored_listings) for entry in found[start:]]
return "\n".join(part for part in parts if part) or None
def latest_overall(history: str) -> decimal.Decimal | Failure | None:
"""The mean of the overall means of newest_scored's entries, since a rerun of one head measures it again; None where no entry counts as scoring the project."""
"""The mean of the overall means of the entries that score the newest scored commit, since a rerun of one head measures it again; None where no entry scores the project."""
overalls = []
for entry in newest_scored(by_time(entries(history))):
for entry in newest_commit_runs(by_time(entries(history))):
overall = run_overall(entry)
if isinstance(overall, Failure):
return overall
@@ -165,57 +165,56 @@ def latest_overall(history: str) -> decimal.Decimal | Failure | None:
return sum(overalls, decimal.Decimal(0)) / len(overalls) if overalls else None
def newest_scored(found: list[Entry]) -> list[Entry]:
"""In found's order, the entries that count as scoring the project on the newest scored commit, the commit of the last such entry; empty where none counts. A commit matches by prefix, since a heading may write it at any length from 7 characters."""
scoring = [entry for entry in found if entry.scored]
return [entry for entry in scoring if entry.commit.startswith(scoring[-1].commit) or scoring[-1].commit.startswith(entry.commit)] if scoring else []
def newest_commit_runs(found: list[Entry]) -> list[Entry]:
"""The entries that score the commit of the last entry that scores the project, in found's order; empty where none does. Commits match by prefix, since a heading may write one at any length from 7 characters."""
scored = [entry for entry in found if entry.scored]
if not scored:
return []
newest = scored[-1].commit
return [entry for entry in scored if entry.commit.startswith(newest) or newest.startswith(entry.commit)]
def run_overall(run: Entry) -> decimal.Decimal | Failure:
"""The overall cell of the Mean row of an entry that counts as scoring the project."""
"""The overall cell of the Mean row of an entry that scores the project."""
try:
overall = decimal.Decimal(last_cells(run.scores).get(MEAN, ""))
overall = decimal.Decimal(last_cells(run.score_rows).get(MEAN, ""))
except decimal.InvalidOperation:
overall = None
return overall if overall is not None and overall.is_finite() else Failure(f"{HISTORY}'s scoring run {run.heading} has no Mean row ending in its overall, which sets code health's weight")
def last_cells(rows: dict[str, list[str]]) -> dict[str, str]:
"""Each row's last non-empty cell, by its first cell: a seat's overall vote in an entry's `votes`, or its overall score, or the Mean row's, in its `scores`."""
"""Each row's last cell, by its first cell, leaving out a row whose last cell is empty: a seat's overall vote in an entry's `vote_rows`, or its overall score, or the Mean row's, in its `score_rows`."""
return {name: cells[-1] for name, cells in rows.items() if cells and cells[-1]}
def scoring_runs(found: list[Entry]) -> list[Entry]:
"""The scoring runs among the entries, oldest first. Unlike Entry.scored, it leaves out a ruling from before PR #255 that holds scoring seats' places."""
return [entry for entry in by_time(found) if not entry.paired]
def by_time(found: list[Entry]) -> list[Entry]:
return sorted(found, key=lambda entry: entry.time)
def worse_places(entry: Entry) -> list[Listing]:
"""Each of a ruling's decided-by listings, cut to the bullets that open with a dimension its seat's vote counted as worse; Overall counts as one."""
"""Each of a ruling's decided-by listings, cut to the bullets that open with a dimension its seat's vote counted as worse."""
cut = []
for listing in entry.decided:
worse = worse_dimensions(entry.votes.get(listing.seat, []))
cut.append(dataclasses.replace(listing, bullets=[bullet for bullet in listing.bullets if worse.intersection(place_dimensions(bullet.removeprefix("- ")))]))
for listing in entry.ruled_listings:
worse = worse_dimensions(entry.vote_rows.get(listing.seat, []))
kept = [bullet for bullet in listing.bullets if worse.intersection(place_dimensions(bullet.removeprefix("- ")))]
cut.append(dataclasses.replace(listing, bullets=kept))
return cut
def placed(entry: Entry, listed: list[Listing]) -> str:
"""The entry's heading, then each listing with bullets, its label and its bullets; empty when no listing has any."""
"""The entry's heading, then the label and bullets of each listing that has bullets; empty when none has."""
text = "".join(f"\n{listing.label}\n\n" + "".join(f"{bullet}\n" for bullet in listing.bullets) for listing in listed if listing.bullets)
return f"## {entry.heading}\n{text}" if text else ""
def worse_dimensions(votes: list[str]) -> set[str]:
"""The dimensions, Overall included, whose vote in a seat's row of the ruling's votes counted as worse; `same (voted worse, …)` counted as same. A row without one vote per dimension, as in the older table with a single `Vote`, has none."""
"""The dimensions, Overall included, that a seat's row of votes counted as worse; `same (voted worse, …)` counts as same. A row without one vote per dimension, such as the older single `Vote` table's, gives an empty set."""
return {dimension for dimension, vote in zip(DIMENSIONS, votes) if vote == "worse"} if len(votes) == len(DIMENSIONS) else set()
def table_rows(body: str) -> list[dict[str, list[str]]]:
"""Each table's rows after its header and separator, the cells after the first keyed by the first cell. A table starts at the row above a separator; `sections` has dropped the blank lines between tables."""
"""Each table's rows after its header and separator, the cells after the first keyed by the first cell. Tables may touch, so a table starts at the row above a separator."""
lines = [line for line in body.splitlines() if line.startswith("|")]
separators = [index for index, line in enumerate(lines) if line.startswith("|-")]
ends = [*(index - 1 for index in separators[1:]), len(lines)]
@@ -223,7 +222,6 @@ def table_rows(body: str) -> list[dict[str, list[str]]]:
def row_cells(line: str) -> tuple[str, list[str]]:
"""A table row's first cell, then the cells after it."""
name, *cells = [cell.strip() for cell in line.strip("|").split("|")]
return name, cells