Download ram-boot-image/tests/driver.py from fasterlabeling/terminal-tasks-showcase: direct link, hf CLI and curl.
- Browser
- Download file 3.79 kB
-
https://huggingface.co/datasets/fasterlabeling/terminal-tasks-showcase/resolve/main/ram-boot-image/tests/driver.py
- Command line
-
hf download hf://datasets/fasterlabeling/terminal-tasks-showcase/ram-boot-image/tests/driver.py
-
curl -L -o driver.py https://huggingface.co/datasets/fasterlabeling/terminal-tasks-showcase/resolve/main/ram-boot-image/tests/driver.py
3.79 kB
| """Request loop run by the grader as uid 65534: python3.11 -I -S -B driver.py PKGROOT. | |
| PKGROOT holds a root-owned copy of the .py and .json files of /app/estimator (subdirectories | |
| kept). The standard library only: -S keeps site-packages off sys.path and nothing adds it back. | |
| The modules the driver needs are imported before PKGROOT goes onto sys.path[0], so a file of the | |
| package cannot replace them. Each request line is "ID<TAB>QUERY-JSON"; estimate.estimate(query) | |
| is called with a fresh dict and the result is written to the original stdout as one line | |
| "ID<TAB>OK<TAB>CANON" or "ID<TAB>ERR<TAB>message". CANON is | |
| json.dumps(answer, sort_keys=True, separators=(",", ":")) of the validated answer: a dict with | |
| exactly the keys "class" (str), "cells" (int, not bool), "ffbits" (int, not bool) and "init" | |
| (str: 64 lowercase hexadecimal digits). Anything the agent's code prints goes to stderr. | |
| """ | |
| import os | |
| import sys | |
| proto = os.dup(1) | |
| os.dup2(2, 1) | |
| sys.set_int_max_str_digits(0) | |
| import json # noqa: E402 | |
| MAX_CANON = 200000 | |
| KEYS = ("cells", "class", "ffbits", "init") | |
| def send(text): | |
| data = (text.replace("\n", " ").replace("\r", " ") + "\n").encode("utf-8", "replace") | |
| while data: | |
| n = os.write(proto, data) | |
| data = data[n:] | |
| def tname(v): | |
| return type(v).__name__ | |
| def as_str(v, what): | |
| if not isinstance(v, str): | |
| raise TypeError("%s must be a str, got %s" % (what, tname(v))) | |
| return str.__str__(v) # an exact str copy, even of a subclass | |
| def as_int(v, what): | |
| if isinstance(v, bool) or not isinstance(v, int): | |
| raise TypeError("%s must be an int (not bool), got %s" % (what, tname(v))) | |
| return int.__int__(v) # an exact int copy, even of a subclass | |
| def canon(v): | |
| if not isinstance(v, dict): | |
| raise TypeError("estimate() must return a dict, got %s" % tname(v)) | |
| items = list(dict.items(v)) | |
| keys = sorted(as_str(k, "a key of the answer") for k, _ in items) | |
| if keys != list(KEYS): | |
| raise ValueError("the answer must have exactly the keys class, cells, ffbits, init; got %s" | |
| % ", ".join(keys)[:200]) | |
| got = {as_str(k, "key"): x for k, x in items} | |
| out = {"class": as_str(got["class"], "class"), | |
| "cells": as_int(got["cells"], "cells"), | |
| "ffbits": as_int(got["ffbits"], "ffbits")} | |
| init = as_str(got["init"], "init") | |
| if len(init) != 64 or any(c not in "0123456789abcdef" for c in init): | |
| raise ValueError("init must be 64 lowercase hexadecimal digits, got %r" % init[:80]) | |
| out["init"] = init | |
| text = json.dumps(out, sort_keys=True, separators=(",", ":")) | |
| if len(text) > MAX_CANON: | |
| raise ValueError("the answer is too long (%d characters)" % len(text)) | |
| return text | |
| def errmsg(prefix, exc): | |
| try: | |
| msg = "%s: %s" % (type(exc).__name__, exc) | |
| except BaseException: # noqa: BLE001 | |
| msg = type(exc).__name__ | |
| return (prefix + msg)[:300] | |
| sys.path.insert(0, sys.argv[1]) | |
| try: | |
| from estimate import estimate as solve # noqa: E402 | |
| if not callable(solve): | |
| raise TypeError("estimate.estimate is not callable") | |
| except BaseException as exc: # noqa: BLE001 | |
| send("IMPORTERR\t" + errmsg("", exc)) | |
| sys.exit(3) | |
| send("READY") | |
| for line in sys.stdin: | |
| line = line.rstrip("\n") | |
| if not line: | |
| continue | |
| rid, payload = line.split("\t", 1) | |
| query = json.loads(payload) | |
| try: | |
| result = solve(query) | |
| except BaseException as exc: # noqa: BLE001 | |
| send(rid + "\tERR\t" + errmsg("estimate() raised ", exc)) | |
| continue | |
| try: | |
| send(rid + "\tOK\t" + canon(result)) | |
| except BaseException as exc: # noqa: BLE001 | |
| send(rid + "\tERR\t" + errmsg("invalid return value: ", exc)) | |