#!/usr/bin/env python3 """Offline comparison coverage on the published saved CSV; no network.""" import csv,json,sys,hashlib,itertools,datetime from decimal import Decimal def analyze(filename): raw=open(filename,'rb').read() with open(filename,encoding='utf-8-sig',newline='') as f:allrows=list(csv.DictReader(f)) rows=[dict(r,source_csv_row=i+2) for i,r in enumerate(allrows) if r['집계포함']=='포함'] a=[r for r in rows if r['계약월']=='202608'];b=[r for r in rows if r['계약월']=='202609'] pairs=list(itertools.product(a,b)) def match(x,y,fields): return all(x[f] and y[f] and (Decimal(x[f])==Decimal(y[f]) if f=='전용면적(㎡)' else x[f]==y[f]) for f in fields) definitions=[('cross_month_all',[]),('same_complex',['단지식별자']),('same_complex_and_exact_area',['단지식별자','전용면적(㎡)']),('same_complex_area_floor',['단지식별자','전용면적(㎡)','층'])] stages=[{'id':name,'fields':fields,'pair_count':sum(match(x,y,fields) for x,y in pairs)} for name,fields in definitions] details=[] for x,y in pairs: if not match(x,y,['단지식별자']):continue details.append({'aug_csv_row':x['source_csv_row'],'sep_csv_row':y['source_csv_row'],'complex_id':x['단지식별자'],'complex_name':x['단지명'],'aug_date':x['계약일'],'sep_date':y['계약일'],'aug_area':x['전용면적(㎡)'],'sep_area':y['전용면적(㎡)'],'aug_floor':x['층'],'sep_floor':y['층'],'aug_amount_manwon':x['금액(만원)'],'sep_amount_manwon':y['금액(만원)'],'exact_area_equal':Decimal(x['전용면적(㎡)'])==Decimal(y['전용면적(㎡)']),'same_floor':x['층']==y['층']}) return {'input_sha256':hashlib.sha256(raw).hexdigest(),'source_kind':'Existing saved August/September2026 Jongno sample, not a new market query.','source_saved_at_kst':sorted({r['자료저장시각(KST)'] for r in rows}),'input_rows':len(rows),'aug_rows':len(a),'sep_rows':len(b),'missing_complex_id':sum(not r['단지식별자'] for r in rows),'stages':stages,'same_complex_pairs':details,'limits':['Pair count is combinations, not independent new sales. No unit-level identifier appears in this CSV.','Exact area and floor matching does not establish same dwelling or causal price change.','Saved-sample inclusion only; no new transaction API, correction or current-price query.']} if __name__=='__main__': result=analyze(sys.argv[1]);result['calculated_at_utc']=datetime.datetime.now(datetime.timezone.utc).isoformat() with open(sys.argv[2],'x',encoding='utf-8') as f:json.dump(result,f,ensure_ascii=False,indent=2) print(json.dumps({'stages':result['stages'],'rows':result['input_rows']},ensure_ascii=False))