From ea5631d2029b6c7ecae9554073d5cd8ed93c4cbf Mon Sep 17 00:00:00 2001 From: Johnny Wilson Dougherty Date: Sat, 28 Nov 2026 16:33:41 +0530 Subject: [PATCH] docs: make the README quickstart run with no edits, and check it in CI A new user who copies the quickstart gets an error before the first freshdata call: - `pip install freshdata-cleaner` does not install Polars, so line 1 fails with `ModuleNotFoundError: No module named 'polars'`. - With Polars, `pl.scan_parquet("s3://bucket/...")` sends a request to real S3 and fails with `OSError: object-store error: Generic S3 error`. The install line now names the `[polars]` extra. The quickstart starts from a small inline LazyFrame and keeps the scan as a comment. The example output is now the real output. scripts/check_readme_blocks.py runs each python block of a Markdown file in order, in one namespace. The new CI job readme-code-blocks builds the wheel, installs it with the extra the README names, and runs the script with `python -I` outside the checkout, so it tests the wheel and not src/. --- .github/workflows/ci-verification-gates.yml | 28 +++++++++++++++ README.md | 27 ++++++++++---- scripts/check_readme_blocks.py | 40 +++++++++++++++++++++ 3 files changed, 88 insertions(+), 7 deletions(-) create mode 100644 scripts/check_readme_blocks.py diff --git a/.github/workflows/ci-verification-gates.yml b/.github/workflows/ci-verification-gates.yml index b73428bc..4afa0fe0 100644 --- a/.github/workflows/ci-verification-gates.yml +++ b/.github/workflows/ci-verification-gates.yml @@ -130,6 +130,34 @@ jobs: --cov-fail-under=40 \ -v + readme-code-blocks: + name: README Code Blocks Against Built Wheel + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set up Python 3.12 + uses: actions/setup-python@v5 + with: + python-version: "3.12" + + - name: Build the wheel + run: | + python -m pip install --upgrade pip build + python -m build --wheel --outdir dist/ + + - name: Install the wheel with the extra the README names + run: | + python -m venv "$RUNNER_TEMP/readme-venv" + WHEEL="$(ls "$PWD"/dist/*.whl)" + "$RUNNER_TEMP/readme-venv/bin/pip" install "freshdata-cleaner[polars] @ file://$WHEEL" + + - name: Run each README python block (python -I, outside the checkout) + working-directory: ${{ runner.temp }} + run: | + "$RUNNER_TEMP/readme-venv/bin/python" -I "$GITHUB_WORKSPACE/scripts/check_readme_blocks.py" "$GITHUB_WORKSPACE/README.md" + clean-room-artifact-audit: name: Clean-Room Wheel & Sdist Audit (/tmp Isolated Venv) needs: guard-no-cov-bypass diff --git a/README.md b/README.md index 6bb5ae93..995179a2 100644 --- a/README.md +++ b/README.md @@ -21,9 +21,11 @@ Most automated data-cleaning libraries fail in production for three reasons: ## Install ```bash -pip install freshdata-cleaner +pip install "freshdata-cleaner[polars]" ``` +For pandas only, `pip install freshdata-cleaner` is enough. + To install the AST codemod tool for upgrading legacy `2.x` scripts: ```bash @@ -38,8 +40,15 @@ pip install "freshdata-cleaner[migrate]" import polars as pl import freshdata as fd -# 1. Point to a lazy Parquet scan (no rows loaded into memory) -lf = pl.scan_parquet("s3://bucket/raw_customer_events.parquet") +# 1. Start from a LazyFrame. In production this is a scan, for example +# lf = pl.scan_parquet("s3://bucket/raw_customer_events.parquet") +lf = pl.LazyFrame({ + "Customer ID": [1, 2, 3, 4, 5, 5], + "Signup Age": [34, None, 29, 120, 41, 41], + "Segment": [" retail", "b2b ", "retail", None, "b2b", "b2b"], + "Invoice Total": [12.5, 9.0, 11.0, 10.5, 14.0, 14.0], + "churned": [0, 1, 0, 0, 1, 1], +}) # 2. Define explicit column protections and rules config = fd.CleanConfig( @@ -58,13 +67,17 @@ plan = fd.suggest_plan(lf, config=config) for action in plan.actions: print(f"[{action.stage}] {action.column}: {action.action} {action.parameters}") -# Example output (abridged) for raw columns "Customer ID", "Signup Age", "Segment", ...: +# Output: +# [column_names] Customer ID: normalize_name {'to': 'customer_id'} # [column_names] Signup Age: normalize_name {'to': 'signup_age'} # [missing] signup_age: impute_median {} +# [column_names] Segment: normalize_name {'to': 'segment'} # [strings] segment: strip_strings {} -# [outliers] signup_age: clip_iqr {'lower': 27.125, 'upper': 48.125} +# [column_names] Invoice Total: normalize_name {'to': 'invoice_total'} +# [missing] invoice_total: impute_median {} +# [outliers] signup_age: clip_iqr {'lower': 27.875, 'upper': 48.875} # [duplicates] *: drop_duplicates {} -# customer_id (id) and churned (target) get no repair actions. +# customer_id (id) and churned (target) get no imputation, stripping, or clipping. # 4. Compile the approved plan onto the LazyFrame (zero materialization) cleaned_lf, report = fd.apply_plan(lf, plan, return_report=True) @@ -94,7 +107,7 @@ for action in diff["removed"]: print(f"- [{action.stage}] {action.column}: {action.action}") for change in diff["modified"]: print(f"~ [{change['stage']}] {change['column']}: {change['before']['parameters']} -> {change['after']['parameters']}") -# + [outliers] invoice_total: clip_iqr {'lower': 7.125, 'upper': 15.125} +# + [outliers] invoice_total: clip_iqr {'lower': 6.125, 'upper': 18.125} ``` --- diff --git a/scripts/check_readme_blocks.py b/scripts/check_readme_blocks.py new file mode 100644 index 00000000..bdecdb9c --- /dev/null +++ b/scripts/check_readme_blocks.py @@ -0,0 +1,40 @@ +"""Run each ```python block of a Markdown file, in order, in one namespace. + +A later block can use names from an earlier block, as a reader does. +Blocks in other languages (bash, text) are listed and skipped. +Run with ``python -I`` so ``import freshdata`` loads the installed wheel, not src/. + + python -I scripts/check_readme_blocks.py README.md +""" + +from __future__ import annotations + +import pathlib +import re +import sys + +FENCE = re.compile(r"^```(\w*)\n(.*?)^```", re.DOTALL | re.MULTILINE) + + +def main(path: str) -> int: + text = pathlib.Path(path).read_text(encoding="utf-8") + namespace: dict[str, object] = {"__name__": "__main__"} + ran = 0 + for block in FENCE.finditer(text): + line = text.count("\n", 0, block.start()) + 1 + if block.group(1) != "python": + print(f"skip {path}:{line} ({block.group(1) or 'no language'})") + continue + print(f"run {path}:{line}") + # Pad with newlines so traceback line numbers are README line numbers. + exec(compile("\n" * line + block.group(2), path, "exec"), namespace) + ran += 1 + if not ran: + print(f"error: {path} has no ```python blocks", file=sys.stderr) + return 1 + print(f"ok: {ran} python block(s) ran") + return 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv[1] if len(sys.argv) > 1 else "README.md"))