"""Recompute compact published BLS extraction. Optional --raw verifies original bulk file.""" from pathlib import Path from decimal import Decimal as D import argparse,csv,json,hashlib P=Path(__file__).resolve().parent ap=argparse.ArgumentParser();ap.add_argument('--raw',type=Path);args=ap.parse_args() with (P/'annual-employment.csv').open(encoding='utf-8',newline='') as f:rows=list(csv.DictReader(f)) assert len(rows)==39 assert [int(r['year']) for r in rows]==list(range(1987,2026)) v={int(r['year']):D(r['employment_thousand_jobs']) for r in rows} assert all(x>0 for x in v.values()) if args.raw: expected=json.loads((P/'input-hashes.json').read_text())['ip.data.1.AllData'] assert hashlib.sha256(args.raw.read_bytes()).hexdigest()==expected,'Raw hash mismatch: different file/vintage. Investigate before updating.' raw=[] with args.raw.open(encoding='utf-8',newline='') as f: for r in csv.DictReader(f,delimiter='\t'): r={k.strip():x.strip() for k,x in r.items()} if r['series_id']=='IPUJN511130W200000000':raw.append(r) raw.sort(key=lambda r:int(r['year'])) assert len(raw)==39 and [int(r['year']) for r in raw]==list(range(1987,2026)) assert all(r['period']=='A01' and not r['footnote_codes'] for r in raw) assert [(int(r['year']),D(r['value'])) for r in raw]==list(v.items()),'Extraction differs' pairs=[(1987,2025),(1997,2021),(1997,2025),(2015,2025),(2019,2025),(2021,2025),(2024,2025)] comparisons=[{'start_year':a,'end_year':b,'start_thousand_jobs':str(v[a]),'end_thousand_jobs':str(v[b]),'change_thousand_jobs':str(v[b]-v[a]),'total_percent_change':str(100*(v[b]-v[a])/v[a])} for a,b in pairs] peaks=[y for y,x in v.items() if x==max(v.values())];lows=[y for y,x in v.items() if x==min(v.values())] assert peaks==[1997] and lows==[2021] print(json.dumps({'series_id':'IPUJN511130W200000000','observations':len(v),'peak_years':peaks,'trough_years':lows,'comparisons':comparisons,'loss_recovered_percent':str(100*(v[2025]-v[2021])/(v[1997]-v[2021])),'raw_verified':bool(args.raw)},indent=2))