p = "output/build_task1.py" s = open(p, encoding="utf-8").read() orig = s def rep(old, new): global s assert s.count(old) == 1, old s = s.replace(old, new) # 1. exceptions: rename misleading column, add effect column rep('''def exc(src, row, key, issue, action, impact): exceptions.append(dict(source_file=src, row_id=row["row_id"], record_id=key, issue=issue, action=action, amount_impact_cad=impact))''', '''def exc(src, row, key, issue, action, impact): effect = ("PROVISIONAL: unknown amount not deducted; totals may be overstated" if impact == "unknown" else "Excluded; recorded amount not counted") exceptions.append(dict(source_file=src, row_id=row["row_id"], record_id=key, issue=issue, action=action, recorded_amount_cad=impact, effect_on_totals=effect))''') rep('fieldnames=["source_file","row_id","record_id","issue","action","amount_impact_cad"]', 'fieldnames=["source_file","row_id","record_id","issue","action","recorded_amount_cad","effect_on_totals"]') # 2. refund product from linked paid order, assigned before duplicate check (informational only) rep(''' r["_status"] = r["status"].strip().lower(); r["_reason"] = ""; r["_prod"] = "" rid = r["refund_id"].strip()''', ''' r["_status"] = r["status"].strip().lower(); r["_reason"] = "" _o = paid_orders.get(r["order_id"].strip()) r["_prod"] = _o["_prod"] if _o else "" rid = r["refund_id"].strip()''') # 3. scripted reconciliation checks (previously done by hand) rep('print("ASSERTIONS PASSED")', '''def total(rows, key): return sum((r[key] for r in rows if r[key] is not None), Decimal(0)) for name, rows, key in (("sales", sales, "_gross"), ("refunds", refunds, "_amt"), ("ads", ads, "_amt")): inc = [r for r in rows if not r["_reason"]]; exc_ = [r for r in rows if r["_reason"]] unknown = sum(1 for r in rows if r[key] is None) assert len(inc) + len(exc_) == len(rows) assert total(inc, key) + total(exc_, key) == total(rows, key) print(f"RECON {name}: rows {len(rows)} = {len(inc)} incl + {len(exc_)} excl; " f"amount {m(total(rows, key))} = {m(total(inc, key))} + {m(total(exc_, key))}; unknown amounts {unknown}") ex_rows = {(e["source_file"], e["row_id"]) for e in exceptions} assert ex_rows == {(f, r["row_id"]) for f, rows in (("sales.csv", sales), ("refunds.csv", refunds), ("ads.csv", ads)) for r in rows if r["_reason"]} assert m(total(sales, "_gross")) == "4140.00" and m(total(refunds, "_amt")) == "960.00" and m(total(ads, "_amt")) == "630.00" # Sensitivity bound for RF05: refund cannot exceed N1011 gross cap = paid_orders["N1011"]["_gross"] print("RF05 cap", m(cap), "combined net range", m(res["Combined"]["net"] - cap), "-", m(res["Combined"]["net"]), "combined contribution range", m(res["Combined"]["contrib"] - cap), "-", m(res["Combined"]["contrib"]), "course per-order low", m((res["Course"]["net"] - cap) / res["Course"]["n"]), "combined per-order low", m((res["Combined"]["net"] - cap) / res["Combined"]["n"])) print("ASSERTIONS PASSED")''') open(p, "w", encoding="utf-8").write(s) print("patched")