"""Independent re-check for revision 2 (standard library only). 1. Recompute every headline figure from the cleaned CSVs alone. 2. Rule checks on cleaned files (unique IDs, status, period, refund <= gross, 2-dp money, ISO dates). 3. Row-level reconciliation: every source row_id is in exactly one of cleaned / exceptions. 4. Compare inputs/*.csv with the file listing printed in the assignment prompt. 5. Compare revision-2 data files with output/original-submission/. """ import csv, os, re, difflib from decimal import Decimal base = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) J = lambda *p: os.path.join(base, *p) def rd(p): with open(J(p), newline="", encoding="utf-8") as f: return list(csv.DictReader(f)) s, r, a, e = (rd("output/cleaned-sales.csv"), rd("output/cleaned-refunds.csv"), rd("output/cleaned-ads.csv"), rd("output/exceptions.csv")) print("1. headline figures recomputed from cleaned CSVs") exp = {"Workshop": "5 1920.00 240.00 1680.00 300.00 1380.00 336.00", "Course": "4 900.00 60.00 840.00 150.00 690.00 210.00", "Combined": "9 2820.00 300.00 2520.00 450.00 2070.00 280.00"} txt = open(J("output/summary.md"), encoding="utf-8").read() for p in ("Workshop", "Course", "Combined"): f = lambda x: p == "Combined" or x["product"] == p n = sum(1 for x in s if f(x)) g = sum(Decimal(x["gross_cad"]) for x in s if f(x)) rf = sum(Decimal(x["amount_cad"]) for x in r if f(x)) ad = sum(Decimal(x["spend_cad"]) for x in a if f(x)) line = f"{n} {g} {rf} {g - rf} {ad} {g - rf - ad} {((g - rf) / n).quantize(Decimal('0.01'))}" print(" ", p, line) assert line == exp[p], (p, line) # the summary table must show the same numbers row = lambda label: [c.strip() for c in re.search(r"\| " + re.escape(label) + r" \|(.*)\|", txt).group(1).split("|")] cols = list(zip(*[row(l) for l in ["Paid orders", "Gross sales", "Applied refunds (settled in October, calculable)", "Net revenue", "Advertising spend", "Contribution after advertising", "Net revenue per paid order"]])) for p, c in zip(("Workshop", "Course", "Combined"), cols): assert " ".join(c) == exp[p], (p, c) print(" summary.md headline table matches") print("2. rule checks") money = re.compile(r"^\d+\.\d{2}$") isod = re.compile(r"^\d{4}-\d{2}-\d{2}$") assert len({x["order_id"] for x in s}) == len(s) assert len({x["refund_id"] for x in r}) == len(r) assert len({x["ad_id"] for x in a}) == len(a) assert all(x["status"] == "paid" and isod.match(x["date"]) and "2026-10-01" <= x["date"] <= "2026-10-31" for x in s) assert all(x["status"] == "settled" and isod.match(x["settled_date"]) and "2026-10-01" <= x["settled_date"] <= "2026-10-31" for x in r) assert all(isod.match(x["date"]) and "2026-10-01" <= x["date"] <= "2026-10-31" for x in a) assert all(money.match(x["gross_cad"]) for x in s) and all(money.match(x["amount_cad"]) for x in r) and all(money.match(x["spend_cad"]) for x in a) assert all(x["product"] in ("Workshop", "Course") for x in s + r + a) assert all(x["channel"] in ("search", "social") for x in a) orders = {x["order_id"]: x for x in s} assert all(x["order_id"] in orders and Decimal(x["amount_cad"]) <= Decimal(orders[x["order_id"]]["gross_cad"]) and x["product"] == orders[x["order_id"]]["product"] for x in r) assert all(x["normalized_date"] == "" or isod.match(x["normalized_date"]) for x in e) assert all(x["amount_cad"] == "" or money.match(x["amount_cad"]) for x in e) r06 = [x for x in e if x["row_id"] == "R06"][0] assert r06["raw_amount"] == "" and r06["amount_cad"] == "" print(" ok") print("3. row-level reconciliation against inputs") for src, clean in (("sales.csv", s), ("refunds.csv", r), ("ads.csv", a)): raw = rd("inputs/" + src) ids = [x["row_id"] for x in raw] inc = [x["row_id"] for x in clean] exc = [x["row_id"] for x in e if x["source_file"] == src] assert sorted(inc + exc) == sorted(ids) and not set(inc) & set(exc), src print(f" {src}: {len(ids)} rows = {len(inc)} included + {len(exc)} in exceptions") print("4. inputs compared with the listing in the assignment prompt") PROMPT = { "sales.csv": """row_id,order_id,date,product,status,gross_cad,updated_at S01,N1001,2026-10-02, Workshop ,paid,CAD 240.00,2026-10-02T12:00:00 S02,N1002,03/10/2026,course,paid,$120.00,2026-10-03T12:00:00 S03,N1003,Oct 05 2026,Workshop,paid,480,2026-10-05T12:00:00 S04,N1003,Oct 05 2026,Workshop,paid,480,2026-10-05T12:00:00 S05,N1004,2026-10-07,Course,pending,120,2026-10-07T12:00:00 S06,N1005,2026-10-10,Workshop,paid,240,2026-10-10T12:00:00 S07,N1006,11/10/2026,Course,paid,240,2026-10-11T12:00:00 S08,N1007,2026-10-12,Workshop,cancelled,240,2026-10-12T12:00:00 S09,N1008,2026-10-15,Course,paid,120,2026-10-15T12:00:00 S10,N1008,2026-10-15,Course,paid,180,2026-10-16T09:00:00 S11,N1009,2026-09-30,Workshop,paid,240,2026-09-30T12:00:00 S12,N1010,2026-10-20,Workshop,paid,720,2026-10-20T12:00:00 S13,N1011,2026-10-22,Course,paid,360,2026-10-22T12:00:00 S14,N1012,2026-10-31,Workshop,paid,240,2026-10-31T12:00:00 S15,N1013,2026-11-01,Course,paid,120,2026-11-01T12:00:00""", "refunds.csv": """row_id,refund_id,order_id,settled_date,status,amount_cad R01,RF01,N1003,2026-10-09,settled,240 R02,RF02,N1006,14/10/2026,settled,60 R03,RF02,N1006,14/10/2026,settled,60 R04,RF03,N1005,2026-10-18,pending,240 R05,RF04,N9999,2026-10-19,settled,120 R06,RF05,N1011,2026-10-25,settled, R07,RF06,N1012,2026-11-02,settled,240""", "ads.csv": """row_id,ad_id,date,product,channel,spend_cad A01,AD01,2026-10-03,Workshop, Search ,150 A02,AD02,2026-10-04,Course,social,90 A03,AD03,2026-10-10,Workshop,SOCIAL,100 A04,AD03,2026-10-10,Workshop,SOCIAL,100 A05,AD04,2026-10-17,Course,Search,60 A06,AD05,2026-10-28,Workshop,Search,$50.00 A07,AD06,2026-11-01,Course,social,80""", } for f, want in PROMPT.items(): got = open(J("inputs", f), encoding="utf-8").read().replace("\r\n", "\n").rstrip("\n") assert got == want, f + " differs from prompt listing" print(f" inputs/{f}: identical to prompt listing (ignoring trailing newline)") print("5. revision 2 vs output/original-submission/") for f in ("cleaned-sales.csv", "cleaned-refunds.csv", "cleaned-ads.csv", "exceptions.csv"): same = open(J("output", f), encoding="utf-8").read() == open(J("output", "original-submission", f), encoding="utf-8").read() print(f" {f}: {'identical' if same else 'CHANGED'}") assert same, f old = open(J("output", "original-submission", "summary.md"), encoding="utf-8").read().splitlines() new = txt.splitlines() n_changed = 0 for d in difflib.unified_diff(old, new, lineterm="", n=0): if d.startswith(("+", "-")) and not d.startswith(("+++", "---")): n_changed += 1 print(" " + d[:230] + ("..." if len(d) > 230 else "")) print(f" summary.md: {n_changed} changed lines (shown above)") print("verify ok")