#!/usr/bin/env python """12-record deterministic sample in the DynaWord sample schema. Picks one row from 12 seeded distinct row groups, forcing 12 distinct repos, then resolves each repo's SPDX id via the GitHub licence API (unauthenticated, 12 calls). Falls back to "other" when unknown/rate-limited. """ import json import random from pathlib import Path import pyarrow.parquet as pq import requests HERE = Path(__file__).resolve().parent PARQUET = HERE / "code_search_net_java.parquet" OUT = HERE / "code_search_net_java.sample.jsonl" CACHE = HERE / "sample_licenses.json" ADDED = "2026-12-05" CREATED = "2019" SOURCE = "code_search_net" def gh_license(repo): try: r = requests.get( f"https://api.github.com/repos/{repo}/license", headers={"Accept": "application/vnd.github+json"}, timeout=20, ) if r.status_code == 200: spdx = (r.json().get("license") or {}).get("spdx_id") if spdx and spdx != "NOASSERTION": return spdx except Exception: pass return "other" def main(): pf = pq.ParquetFile(PARQUET) groups = list(range(pf.num_row_groups)) rng = random.Random(0) rng.shuffle(groups) picked, seen = [], set() for g in groups: if len(picked) >= 12: break table = pf.read_row_group( g, columns=["id", "repo", "text", "token_count"]) for r in table.to_pylist(): if r["repo"] not in seen: seen.add(r["repo"]) picked.append(r) break licences = {} for r in picked: licences[r["repo"]] = gh_license(r["repo"]) with OUT.open("w", encoding="utf-8") as f: for r in picked[:12]: rec = { "added": ADDED, "author": r["repo"], "created": CREATED, "id": r["id"], "license": licences[r["repo"]], "source": SOURCE, "text": r["text"], "token_count": r["token_count"], } f.write(json.dumps(rec, ensure_ascii=False) + "\n") CACHE.write_text(json.dumps(licences, indent=2, ensure_ascii=False), encoding="utf-8") print("records:", len(picked[:12])) print("repos:", [r["repo"] for r in picked[:12]]) print("licences:", licences) print("tokens:", sum(r["token_count"] for r in picked[:12])) print("size:", OUT.stat().st_size) if __name__ == "__main__": main()