"""Job #1670 (pursue route 57). Measure SECTION 2 (the by-question-id table) of a generated research/QUESTIONS.md: rows, ids, statuses, pointer edges into history/staging/ (markdown links in the files cell), distinct targets, shared targets (targets cited by more than one row), the OPEN rows with all their cells; and diff two registries cell by cell. Usage: python measure1670.py []""" import re, sys, json, collections def section2(text): # section 2 is the table whose header starts with '| `id` |' style: rows of the form | `Q-...` | STATUS | question | verdict | todo | files | lines = text.splitlines() # locate the second table: the generator writes '## 2' style headings; robust approach: rows whose first cell is a backticked Q-id rows = [] in_sec2 = False for i, l in enumerate(lines): if re.match(r'^##+\s*2\b', l) or 'by question id' in l.lower() and l.startswith('#'): in_sec2 = True if in_sec2 and re.match(r'^##+\s*[3-9]\b', l): in_sec2 = False m = re.match(r'^\|\s*`(Q-[A-Za-z0-9-]+)`\s*\|\s*([^|]+?)\s*\|(.*)$', l) if in_sec2 and m: cells = [c.strip() for c in l.strip().strip('|').split('|')] rows.append({'line': i + 1, 'id': m.group(1), 'status': m.group(2).strip(), 'cells': cells, 'raw': l}) return rows def edges(rows): E = [] for r in rows: files_cell = r['cells'][-1] if r['cells'] else '' for t in re.findall(r'\]\(([^)]*history/staging/[^)]+)\)', files_cell): E.append((r['id'], t)) return E def measure(path): text = open(path, encoding='utf-8', errors='replace').read() rows = section2(text) E = edges(rows); targets = collections.Counter(t for _, t in E) shared = {t: [i for i, tt in E if tt == t] for t, c in targets.items() if c > 1} st = collections.Counter(r['status'] for r in rows) return {'path': path, 'bytes': len(text.encode('utf-8')), 'rows': len(rows), 'ids': len({r['id'] for r in rows}), 'status_counts': dict(st), 'edges': len(E), 'distinct_targets': len(targets), 'shared_targets': shared, 'open_rows': {r['id']: r['cells'] for r in rows if r['status'].startswith('OPEN')}, '_rows': {r['id']: r for r in rows}} if __name__ == '__main__': a = measure(sys.argv[1]); out = {k: v for k, v in a.items() if k != '_rows'} if len(sys.argv) > 2: b = measure(sys.argv[2]); out['diff_against'] = sys.argv[2] out['other'] = {k: v for k, v in b.items() if k not in ('_rows', 'open_rows')} changed = {} for i, r in a['_rows'].items(): r2 = b['_rows'].get(i) if r2 is None: changed[i] = 'absent in other' elif r2['raw'] != r['raw']: changed[i] = [(k, x, y) for k, (x, y) in enumerate(zip(r['cells'], r2['cells'])) if x != y] or 'cells equal, raw differs' for i in b['_rows']: if i not in a['_rows']: changed[i] = 'absent in this' out['rows_changed'] = changed out['open_rows_byte_identical_except'] = [i for i in a['open_rows'] if i in changed] print(json.dumps(out, indent=1, ensure_ascii=False))