Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-rosetta/blob/main/dataset/export.py # omega-rosetta aien-dev/omega-rosetta Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-rosetta / dataset / export.py` 117 lines ยท 4335 bytes Raw Blame History 1 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 2 """Export the omega-rosetta corpus as one Parquet file for a Hugging Face dataset. 3 4 python3 dataset/export.py [--out dataset/omega-rosetta.parquet] 5 6 Needs pyarrow (use a virtual environment). Reads only files in this repository; it does not run 7 omc or any oracle. Run `sh run.sh` first: the dataset card records that gate's result, and this 8 script refuses a task whose expected.txt is missing. 9 10 One row per task (tasks/) and per exercise (exercises/), sorted by kind then name. 11 """ 12 import argparse 13 import hashlib 14 import os 15 import re 16 import sys 17 18 import pyarrow as pa 19 import pyarrow.parquet as pq 20 21 HERE = os.path.dirname(os.path.abspath(__file__)) 22 ROOT = os.path.dirname(HERE) 23 24 SCHEMA = pa.schema([ 25 ("name", pa.string()), 26 ("kind", pa.string()), 27 ("rosetta_url", pa.string()), 28 ("exercism_url", pa.string()), 29 ("title", pa.string()), 30 ("source_om", pa.string()), 31 ("stub_om", pa.string()), 32 ("test_om", pa.string()), 33 ("expected_stdout", pa.string()), 34 ("expected_exit", pa.int32()), 35 ("oracle_py", pa.string()), 36 ("omc_sha256", pa.string()), 37 ("omega_mv_commit", pa.string()), 38 ("lines", pa.int32()), 39 ]) 40 41 HEADER = re.compile(r'^note Rosetta Code task "(.+)": (https://rosettacode\.org/wiki/\S+)$') 42 43 44 def read(path): 45 with open(path, encoding="utf-8") as f: 46 return f.read() 47 48 49 def pin(): 50 ref = {} 51 for line in read(os.path.join(ROOT, "omega.ref")).splitlines(): 52 parts = line.split() 53 if len(parts) == 2 and not line.startswith("#"): 54 ref[parts[0]] = parts[1] 55 return ref["commit"], ref["omc_sha256"] 56 57 58 def code_lines(src): 59 """Lines that are not note comments, as the README table counts them.""" 60 return sum(1 for l in src.splitlines() if not (l.startswith("note ") or l == "note")) 61 62 63 def exercism_url(readme, name): 64 m = re.search(r"\| \[`%s`\]\([^)]*\) \| \[[^\]]*\]\((https://exercism\.org/[^)]+)\) \|" % re.escape(name), readme) 65 if not m: 66 sys.exit("export.py: no README row with an exercism.org link for exercise " + name) 67 return m.group(1) 68 69 70 def rows(): 71 commit, sha = pin() 72 readme = read(os.path.join(ROOT, "README.md")) 73 out = [] 74 tasks = os.path.join(ROOT, "tasks") 75 for name in sorted(os.listdir(tasks)): 76 d = os.path.join(tasks, name) 77 src = read(os.path.join(d, name + ".om")) 78 m = HEADER.match(src.splitlines()[0]) 79 if not m: 80 sys.exit("export.py: %s.om does not start with the Rosetta Code task note" % name) 81 out.append({ 82 "name": name, "kind": "task", "rosetta_url": m.group(2), "exercism_url": None, 83 "title": m.group(1), "source_om": src, "stub_om": None, "test_om": None, 84 "expected_stdout": read(os.path.join(d, "expected.txt")), "expected_exit": 0, 85 "oracle_py": read(os.path.join(d, "oracle.py")), "omc_sha256": sha, 86 "omega_mv_commit": commit, "lines": code_lines(src), 87 }) 88 exercises = os.path.join(ROOT, "exercises") 89 for name in sorted(os.listdir(exercises)): 90 d = os.path.join(exercises, name) 91 src = read(os.path.join(d, ".meta", "example.om")) 92 out.append({ 93 "name": name, "kind": "exercise", "rosetta_url": None, 94 "exercism_url": exercism_url(readme, name), "title": name, "source_om": src, 95 "stub_om": read(os.path.join(d, name + ".om")), "test_om": read(os.path.join(d, "test.om")), 96 "expected_stdout": None, "expected_exit": 0, "oracle_py": None, "omc_sha256": sha, 97 "omega_mv_commit": commit, "lines": code_lines(src), 98 }) 99 return out 100 101 102 def main(): 103 ap = argparse.ArgumentParser(description=__doc__.splitlines()[0]) 104 ap.add_argument("--out", default=os.path.join(HERE, "omega-rosetta.parquet")) 105 args = ap.parse_args() 106 data = rows() 107 table = pa.Table.from_pylist(data, schema=SCHEMA) 108 pq.write_table(table, args.out, compression="zstd") 109 h = hashlib.sha256() 110 with open(args.out, "rb") as f: 111 h.update(f.read()) 112 kinds = {k: sum(1 for r in data if r["kind"] == k) for k in ("task", "exercise")} 113 print("%s rows=%d tasks=%d exercises=%d sha256=%s" % (args.out, len(data), kinds["task"], kinds["exercise"], h.hexdigest())) 114 115 116 if __name__ == "__main__": 117 main()