"""Same saved36rows, explicit area filters and exact rational means;no API.""" from pathlib import Path from fractions import Fraction from decimal import Decimal,localcontext,ROUND_HALF_UP import json,hashlib,sys def rounded(f): with localcontext()as c: c.prec=65 return str((Decimal(f.numerator)/Decimal(f.denominator)).quantize(Decimal('.01'),rounding=ROUND_HALF_UP)) def main(path): p=Path(path);d=json.loads(p.read_text());groups=[] for s in d['snapshots']: for label in ['all','le85']: rows=[r for r in s['records']if label=='all'or Fraction(str(r['area']))<=85] prices=[Fraction(str(r['amount']))for r in rows];areas=[Fraction(str(r['area']))for r in rows] weighted=sum(prices)/sum(areas);arithmetic=sum(x/y for x,y in zip(prices,areas))/len(rows) groups.append({'month':s['contract_month'],'filter':label,'count':len(rows),'sum_price_manwon':str(sum(prices)),'sum_area_m2':str(sum(areas)),'weighted_unit_exact':str(weighted),'weighted_unit_display':rounded(weighted),'arithmetic_unit_exact':str(arithmetic),'arithmetic_unit_display':rounded(arithmetic),'saved_at_kst':s['saved_at_kst']}) comparisons=[] for label,key in [('all','weighted_unit'),('le85','weighted_unit'),('all','arithmetic_unit')]: a,b=[next(g for g in groups if g['month']==month and g['filter']==label)for month in ['202608','202609']] change=(Fraction(b[key+'_exact'])/Fraction(a[key+'_exact'])-1)*100 comparisons.append({'filter':label,'method':key,'before':a[key+'_display'],'after':b[key+'_display'],'counts':[a['count'],b['count']],'change_percent_exact':str(change),'change_percent_display':rounded(change)}) assert [(g['count'])for g in groups]==[23,12,13,12] assert [r['change_percent_display']for r in comparisons]==['1.80','-5.80','-6.91'] return {'kind':'recalculation of existing saved extract;not current market/index/causal estimate','source_sha256':hashlib.sha256(p.read_bytes()).hexdigest(),'source_reviewed_on_kst':d['reviewed_on_kst'],'groups':groups,'comparisons':comparisons,'limits':'Different homes and source timestamps;85m2 cutoff does not match floor/date/quality;no new upstream data or same-home price change identified.'} if __name__=='__main__':print(json.dumps(main(sys.argv[1]),ensure_ascii=False,indent=2))