#!/usr/bin/env python3 """Job #4548: extract the brief's step object and its named comparison ids. The brief is parsed, never retyped: the step is the JSON object on the line after "The step:" and the named comparison set is every "#" in the "Returns to compare it with" block, with the route's own returns listed separately in the "The route's own returns" line. """ import json import os import re HERE = os.path.dirname(os.path.abspath(__file__)) brief = open(os.path.join(HERE, "brief.md"), encoding="utf-8").read() i = brief.index("The step:\n") + len("The step:\n") step_line = brief[i:].split("\n")[0] step = json.loads(step_line) j = brief.index("Returns to compare it with") k = brief.index("The route's own returns:", j) block = brief[j:k] named = sorted({int(x) for x in re.findall(r"Return #(\d+)", block)}) own = sorted({int(x) for x in re.findall(r"#(\d+)", brief[k:k + 400])}) assert own == [1315, 1317, 1834], own with open(os.path.join(HERE, "brief_step.json"), "w", encoding="utf-8", newline="\n") as fh: json.dump(step, fh, indent=1, sort_keys=True) with open(os.path.join(HERE, "brief_named.json"), "w", encoding="utf-8", newline="\n") as fh: json.dump({"named_comparison_ids": named, "own_route_ids": own, "named_line": " ".join(block.split())[:400]}, fh, indent=1) print("brief step keys: %s" % sorted(step)) print("named comparison ids (%d): %s" % (len(named), named)) print("route's own ids: %s" % own)