- collection/pipeline.yaml declares every source (extract kind, transform, privacy rules, load target, run source); `python -m collection update|build` - extract keeps data/raw/<source>/current + previous only (git-ignored); data/manifest.json records the current raw checksum and change date - transforms are pure; validate checks runs against data/dictionary.yaml before load; load writes byte-stable files only when they change - data/public/runs_clean.csv: every run from every source with a new `source` column (reddit spreadsheet, reddit thread, reported, bot, reddit pdf); the sheet's own row label moves to `entry`; run_id is text - bot importer ported to Python (same output as bot.js on the fixture); labels and crosswalk in collection/specs/bot.yaml, written into graph.js - Reddit through the official API (result links to add by hand); the 12 result pages move to collection/manual/reddit_results.yaml - "Report a run": a GitHub issue form generated from collection/specs/report_form.yaml, collected as source "reported", plus a site tab pointing to it, the community spreadsheet and the megathread - analysis and the route model read runs_clean.csv (sources in measures.yaml); stats.json and policy.json unchanged apart from the factor's new name - daily workflow runs the pipeline; the raw store is kept between runs in the Actions cache, encrypted with RAW_ARCHIVE_KEY Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UCzTd31GphpMXDhQqWrCMQ
45 lines
1.8 KiB
Python
45 lines
1.8 KiB
Python
"""Validate: a table is checked against data/dictionary.yaml before it is loaded. A failure stops the load."""
|
|
import re
|
|
|
|
CHECKS = {
|
|
"integer": lambda v: isinstance(v, int) and not isinstance(v, bool) or bool(re.fullmatch(r"-?\d+", str(v))),
|
|
"boolean": lambda v: isinstance(v, bool) or v in ("True", "False"),
|
|
"date": lambda v: bool(re.fullmatch(r"\d{4}-\d{2}-\d{2}", str(v))),
|
|
"text": lambda v: isinstance(v, str),
|
|
}
|
|
|
|
|
|
class Invalid(Exception):
|
|
pass
|
|
|
|
|
|
def problems(rows, dictionary, header=None):
|
|
"""Every way the rows break the dictionary, as readable lines (empty when the table is valid)."""
|
|
cols = dictionary["columns"]
|
|
out = []
|
|
if header is not None and list(header) != list(cols):
|
|
out.append(f"columns differ from the dictionary: {list(header)} vs {list(cols)}")
|
|
ids = [r.get("run_id") for r in rows]
|
|
if len(set(ids)) != len(ids):
|
|
out.append("run_id is not unique")
|
|
for name, col in cols.items():
|
|
vals = [r.get(name) for r in rows]
|
|
present = [v for v in vals if v is not None and v != ""]
|
|
bad_type = sorted({str(v) for v in present if not CHECKS[col["type"]](v)})
|
|
if bad_type:
|
|
out.append(f"{name}: not {col['type']}: {bad_type[:5]}")
|
|
if "values" in col:
|
|
allowed = {str(v) for v in col["values"]}
|
|
bad = sorted({str(v) for v in present if str(v) not in allowed})
|
|
if bad:
|
|
out.append(f"{name}: values not in the dictionary: {bad[:10]}")
|
|
if col.get("required") and len(present) != len(vals):
|
|
out.append(f"{name}: required, but {len(vals) - len(present)} rows are empty")
|
|
return out
|
|
|
|
|
|
def check(rows, dictionary, what="table"):
|
|
errs = problems(rows, dictionary)
|
|
if errs:
|
|
raise Invalid(f"{what} does not match data/dictionary.yaml:\n " + "\n ".join(errs))
|