files / problems / hilbert16-degree-8 / compute / q2 / common.py
"""Shared path and census helpers for the q2 searches.
Drivers launch from ``compute/`` or from this folder; we always chdir
to the parent ``compute/`` so the archive, ``certs/``, and Haas tables
resolve.
"""
import json
import os
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.dirname(HERE)
if ROOT not in sys.path:
sys.path.insert(0, ROOT)
def boot():
os.chdir(ROOT)
return ROOT
def resolve_out(path):
"""Interpret a relative output path as under q2/, unless it already is."""
if os.path.isabs(path):
return path
if path.startswith("q2" + os.sep) or path.startswith("q2/"):
return os.path.join(ROOT, path)
return os.path.join(HERE, path)
def known_schemes():
ks = {l.strip() for l in open("census_schemes.txt") if l.strip()}
assert len(ks) == 2367, len(ks)
for c in json.load(open("certs/new_schemes.json")):
ks.add(c["scheme"])
extra = os.path.join(HERE, "certs", "new_schemes.json")
if os.path.exists(extra):
for c in json.load(open(extra)):
ks.add(c["scheme"])
return ks
def census_schemes():
ks = {l.strip() for l in open("census_schemes.txt") if l.strip()}
assert len(ks) == 2367
return ks