---
type: "Source File"
title: "!/usr/bin/env python3"
description: "#!/usr/bin/env python3 \"\"\"Recompute figure aggregates from canonical events using db.py semantics.\"\"\" from __future__ import annotations import argparse import json import os from pathlib import Path def main(): parser."
resource: "https://www.copecheck.com"
tags: ["copecheck", "main", "source-file", "py"]
timestamp: "2026-08-24T12:39:23Z"
site: "main"
source_path: "copecheck/recompute_figure_scores.py"
---

# !/usr/bin/env python3

Source path: `copecheck/recompute_figure_scores.py`  
Index snapshot generated: `2026-11-24T13:42:54Z`  
File modified: `2026-08-24T12:39:23Z`

```py
#!/usr/bin/env python3
"""Recompute figure aggregates from canonical events using db.py semantics."""
from __future__ import annotations

import argparse
import json
import os
from pathlib import Path


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "--db", type=Path,
        default=Path("/home/ben/infra/copecheck/data/copecheck.db"),
    )
    args = parser.parse_args()
    os.environ["COPECHECK_DB"] = str(args.db)

    # db.DB_PATH is selected at import time.
    import db

    db.init()
    changes = []
    with db.conn() as connection:
        connection.execute("BEGIN IMMEDIATE")
        unbackfilled = connection.execute(
            "SELECT COUNT(*) FROM cope_entries WHERE event_fingerprint IS NULL"
        ).fetchone()[0]
        if unbackfilled:
            raise RuntimeError(
                f"{unbackfilled} entries lack event identity; run "
                "migrate_canonical_dedupe.py first"
            )
        figures = list(connection.execute(
            "SELECT id, cope_score FROM figures ORDER BY id"
        ))
        for figure in figures:
            before = float(figure["cope_score"] or 50.0)
            after = db.recompute_figure(connection, figure["id"])
            if before != after:
                changes.append({
                    "figure_id": figure["id"],
                    "before": before,
                    "after": after,
                })
    print(json.dumps({"updated": len(changes), "changes": changes}, indent=2))
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
```
