| |
| """Statistics over listings.csv, shared by the dataset build and the PDF. |
| |
| Both `build.py` and `scripts/build_reference_pdf.py` state per-vendor internal |
| dimensions as fact. They must therefore compute them the same way, from one |
| definition — hence this module rather than a copy in each. |
| |
| Pure functions over already-loaded rows; nothing here reads a path except |
| `load_listings`, and nothing touches the network. |
| """ |
|
|
| import csv |
|
|
| |
| |
| EXCLUDE_FROM_INTERNAL_STATS = ("Nesting or folding crate", "Sold with a lid") |
|
|
|
|
| def load_listings(path): |
| if not path.exists(): |
| return [] |
| with path.open(encoding="utf-8") as f: |
| return list(csv.DictReader(f)) |
|
|
|
|
| def median(xs): |
| s = sorted(xs) |
| mid = len(s) // 2 |
| return s[mid] if len(s) % 2 else (s[mid - 1] + s[mid]) / 2 |
|
|
|
|
| def footprint(row): |
| """(type, L_mm, W_mm) for a listing, or None if unparseable.""" |
| try: |
| return (row["type"], round(float(row["external_length_cm"]) * 10), |
| round(float(row["external_width_cm"]) * 10)) |
| except ValueError: |
| return None |
|
|
|
|
| def internal_index(listings): |
| """(type, L_mm, W_mm) -> {vendor: (median_int_L, median_int_W, median_height_deduction)}, mm. |
| |
| One figure per *vendor*, not per listing. Salesbridges alone accounts for two |
| thirds of the 600x400 listings, mostly colour variants of the same mould; a |
| straight median over listings would just report Salesbridges' number and call |
| it a consensus. Taking each vendor's median first gives every catalogue one |
| vote, which is what the vendor capacity span means. |
| """ |
| per_vendor = {} |
| for r in listings: |
| key = footprint(r) |
| if (not key or not r["internal_length_cm"] |
| or any(x in r["notes"] for x in EXCLUDE_FROM_INTERNAL_STATS)): |
| continue |
| try: |
| il, iw = float(r["internal_length_cm"]) * 10, float(r["internal_width_cm"]) * 10 |
| ded = float(r["external_height_cm"]) * 10 - float(r["internal_height_cm"]) * 10 |
| except ValueError: |
| continue |
| per_vendor.setdefault(key, {}).setdefault(r["vendor"], []).append((il, iw, ded)) |
|
|
| return { |
| key: {v: tuple(median([o[i] for o in obs]) for i in range(3)) |
| for v, obs in vendors.items()} |
| for key, vendors in per_vendor.items() |
| } |
|
|
|
|
| def vendor_listing_counts(listings): |
| """(type, L_mm, W_mm) -> {vendor: n listings publishing internal dimensions}.""" |
| counts = {} |
| for r in listings: |
| key = footprint(r) |
| if (not key or not r["internal_length_cm"] |
| or any(x in r["notes"] for x in EXCLUDE_FROM_INTERNAL_STATS)): |
| continue |
| counts.setdefault(key, {}) |
| counts[key][r["vendor"]] = counts[key].get(r["vendor"], 0) + 1 |
| return counts |
|
|
|
|
| def capacity_span(index, kind, L, W, H): |
| """(low, high) litres implied by the least and most generous vendor, or ("", ""). |
| |
| Same box, same external size, different published internal dimensions: at |
| 600x400 the vendors range from 555x355 (Plastic Box Shop, measured near the |
| base) to 570x370 (Salesbridges, the top opening), which is an 11 % spread in |
| litres. Quoting one number without that range would be false precision. |
| """ |
| vendors = index.get((kind, L, W)) |
| if not vendors: |
| return "", "" |
| caps = [il * iw * max(H - ded, 0) / 1e6 for il, iw, ded in vendors.values()] |
| return round(min(caps), 1), round(max(caps), 1) |
|
|