| """ |
| export_parquet.py — Generate seed Parquet files for Kasualdad LFED. |
| |
| Run once to produce enrollment, attendance, students, discipline, and grades |
| Parquet files. Place them in /data/ (HF Space persistent storage) or ship |
| with the image (Modal local dir) for fast loading. |
| |
| Usage: |
| python data/export_parquet.py # → data/ |
| python data/export_parquet.py --out /data # → /data/ (HF Space) |
| """ |
|
|
| import argparse |
| import sys |
| from pathlib import Path |
|
|
| |
| sys.path.insert(0, str(Path(__file__).parent.parent)) |
|
|
| import duckdb |
| from data.generate_seed import generate_seed_data |
|
|
|
|
| |
| TABLES = ["enrollment", "attendance", "students", "discipline", "grades"] |
|
|
|
|
| def export_parquet(out_dir: Path) -> None: |
| """Generate seed data and export all tables as Parquet.""" |
| out_dir.mkdir(parents=True, exist_ok=True) |
|
|
| conn = duckdb.connect(":memory:") |
| conn.execute("SET enable_progress_bar = false;") |
| generate_seed_data(conn) |
|
|
| paths = {} |
| for table in TABLES: |
| out_path = out_dir / f"{table}.parquet" |
| conn.execute(f"COPY {table} TO '{out_path}' (FORMAT PARQUET)") |
| paths[table] = out_path |
|
|
| total_size = 0 |
| print(f"✅ Exported:") |
| for table, path in paths.items(): |
| size = path.stat().st_size |
| total_size += size |
| print(f" {path} ({size:,} bytes)") |
| print(f" Total: {total_size:,} bytes") |
|
|
| |
| verify = duckdb.connect(":memory:") |
| for table in TABLES: |
| path = paths[table] |
| verify.execute(f"CREATE TABLE {table} AS SELECT * FROM read_parquet('{path}')") |
| n = verify.execute(f"SELECT COUNT(*) FROM {table}").fetchone()[0] |
| print(f" ✓ {table}: {n} rows") |
| verify.close() |
| conn.close() |
|
|
|
|
| if __name__ == "__main__": |
| parser = argparse.ArgumentParser(description="Export seed data to Parquet") |
| parser.add_argument( |
| "--out", |
| type=Path, |
| default=Path(__file__).parent, |
| help="Output directory (default: data/)", |
| ) |
| args = parser.parse_args() |
| export_parquet(args.out) |
|
|