import dataclasses import re from failure import Failure ARCHITECT = "Inherited architect" DIMENSIONS = ("Navigation", "Locality", "Shape", "Self-sufficiency", "Overall") JUNIOR = "Junior A" # A scoring run moved from a `comprehension floor:` decision entry kept only the seats' means. MEAN = "Mean" MID = "Mid A" SCORES = re.compile(", ".join(rf"{name} ([0-9]+(?:\.[0-9]+)?)" for name in DIMENSIONS)) SENIOR = "Maintainability senior" FIRST_SEATS = (MID, ARCHITECT) SCORING_SEATS = (JUNIOR, MID, SENIOR, ARCHITECT) SEAT_LINE = re.compile(rf"^({'|'.join((*SCORING_SEATS, MEAN))}): (.+)$") SPLIT_SEATS = (SENIOR, JUNIOR) @dataclasses.dataclass(frozen=True) class Seat: name: str said: str places: list[str] @dataclasses.dataclass(frozen=True) class Report: """A seats file: what the seats read, ` at `, with `, against ` for a paired ruling, then each seat's line and its places.""" read: str seats: list[Seat] def report(text: str) -> Report | Failure: lines = [line for line in text.splitlines() if line.strip()] if not lines: return Failure("the seats file is empty; its first line names what the seats read: at ") seats: list[Seat] = [] for line in lines[1:]: if seat := SEAT_LINE.match(line): if seat.group(1) in [known.name for known in seats]: return Failure(f"{seat.group(1)} appears twice in the seats file") seats.append(Seat(seat.group(1), seat.group(2).strip(), [])) elif seats and line.startswith("- "): seats[-1].places.append(line[2:].strip()) elif seats and seats[-1].places and line.startswith(" "): seats[-1].places[-1] += " " + line.strip() else: return Failure(f"the seats file has a line that is neither a seat's line nor one of its places: {line}") return Report(lines[0].strip(), seats) def scores(seats: list[Seat]) -> dict[str, list[str]] | Failure: """Each seat's five scores as written, in DIMENSIONS' order: the four seats', or the Mean line's alone.""" if [seat.name for seat in seats] not in (list(SCORING_SEATS), [MEAN]): return Failure(f"a scoring run's seats are {', '.join(SCORING_SEATS)}, in that order, or a {MEAN} line alone; the file has {', '.join(seat.name for seat in seats) or 'none'}") found = {} for seat in seats: if not (written := SCORES.fullmatch(seat.said)): return Failure(f"{seat.name}'s line is not {', '.join(name + ' ' for name in DIMENSIONS)}: {seat.said}") values = [float(value) for value in written.groups()] if not all(1 <= value <= 10 for value in values): return Failure(f"{seat.name}'s scores are not each from 1 to 10: {seat.said}") if values[-1] > min(values[:-1]) + 1: return Failure(f"{seat.name}'s overall exceeds their lowest dimension by more than 1; ask the seat to rescore: {seat.said}") found[seat.name] = list(written.groups()) return found