#!/usr/bin/env bash set -euo pipefail cd "$(dirname "$0")" PYTHON_BIN="${PYTHON_BIN:-python3}" "$PYTHON_BIN" - <<'__SKILL_EVOL_SOLVE_PY_0__' from __future__ import annotations import os from pathlib import Path from textwrap import dedent PROJECT_ROOT = Path(os.environ.get("PROJECT_ROOT", "/root/task")).resolve() FILES = { "category_normalizer.py": dedent( """ from __future__ import annotations ZERO_WIDTH = ("\\u200b", "\\u200c", "\\u200d", "\\ufeff") def normalize_category(value: object) -> str: if value is None: return "" text = str(value) for char in ZERO_WIDTH: text = text.replace(char, "") text = text.strip().lower() text = " ".join(text.split()) return text """ ), "rowcount_guard.py": dedent( """ from __future__ import annotations EXPECTED_MIN = 850 EXPECTED_MAX = 1050 def evaluate_rowcount(row_count: int) -> dict[str, object]: return { "expected_min": EXPECTED_MIN, "expected_max": EXPECTED_MAX, "rowcount_reasonable": EXPECTED_MIN <= row_count <= EXPECTED_MAX, } """ ), "query_electronics_report.py": dedent( """ from __future__ import annotations import json import math import sqlite3 from decimal import Decimal, ROUND_HALF_UP from pathlib import Path from category_normalizer import normalize_category from rowcount_guard import evaluate_rowcount DB_PATH = Path("products.db") OUTPUT_PATH = Path("output.json") INVALID_NULLS = {"", "na", "n/a", "null", "none"} def parse_price(value: object) -> float | None: if value is None: return None text = str(value).strip() if not text or text.lower() in INVALID_NULLS: return None if text.startswith("$"): text = text[1:].strip() text = text.replace(",", "") try: price = float(text) except ValueError: return None if not math.isfinite(price) or price < 0: return None return price def parse_stock(value: object) -> int | None: if value is None: return None text = str(value).strip() if not text or text.lower() in INVALID_NULLS: return None try: stock = float(text) except ValueError: return None if not math.isfinite(stock) or stock < 0 or not stock.is_integer(): return None return int(stock) def round_money(value: float) -> float: return float(Decimal(str(value)).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)) def load_rows() -> list[sqlite3.Row]: conn = sqlite3.connect(DB_PATH) conn.row_factory = sqlite3.Row try: return conn.execute( "SELECT id, name, category, price, stock_quantity, active FROM products ORDER BY id" ).fetchall() finally: conn.close() def main() -> None: rows = load_rows() records = [] price_sum = Decimal("0.00") stock_sum = 0 for row in rows: if row["active"] != 1: continue if normalize_category(row["category"]) != "electronics": continue price = parse_price(row["price"]) stock = parse_stock(row["stock_quantity"]) if price is None or stock is None: continue price_sum += Decimal(str(price)) stock_sum += stock records.append( { "id": row["id"], "name": row["name"], "category": "Electronics", "price": round_money(price), "stock_quantity": stock, } ) rowcount_audit = evaluate_rowcount(len(records)) average_price = round_money(float(price_sum / len(records))) if records else 0.0 output = { "category": "Electronics", "row_count": len(records), "average_price": average_price, "total_stock": stock_sum, "rowcount_audit": rowcount_audit, "requery_performed": True, "records": records, } OUTPUT_PATH.write_text(json.dumps(output, indent=2), encoding="utf-8") if __name__ == "__main__": main() """ ), } for relative_path, content in FILES.items(): target = PROJECT_ROOT / relative_path target.write_text(content, encoding="utf-8") print(f"wrote {target}") __SKILL_EVOL_SOLVE_PY_0__