A dependency-free Python CLI that validates inventory and maintenance CSV exports using explicit JSON rules. It separates accepted records from rejects, preserves the source file, and produces deterministic reports with source/schema hashes.
This is new independent portfolio work using synthetic data. It is not a historical client delivery or a connector to a live factory, ERP or warehouse system.
Requires Python 3.11 or newer. No package installation, API key, network request or external service is needed to run the code or tests.
git clone https://github.com/efanntyo/python-data-quality-lab.git
cd python-data-quality-lab
python3 demo.pyOn Windows, use python in place of python3. The demo runs the real CLI in subprocesses, verifies the artifacts and refusal to overwrite, then removes its temporary outputs.
Expected output:
inventory: 8 total / 2 accepted / 6 rejected; source unchanged; overwrite refused
maintenance: 4 total / 2 accepted / 2 rejected; source unchanged; overwrite refused
cross-field: 4 total / 2 accepted / 2 rejected; source unchanged; overwrite refused
PASS: all three CLI workflows verified; temporary outputs removed
These small examples exercise behavior; they are not a benchmark or a measurement of real company data quality.
python3 -m dq_lab examples/inventory.csv --schema examples/inventory.schema.json --output out-inventoryThe example intentionally contains errors, so this command writes its reports and exits with code 1. The output directory must not exist; its parent must exist.
| Output | Contents |
|---|---|
clean.csv |
Accepted rows in schema column order, with conservative normalization |
rejected.jsonl |
One rejected record per line: original parsed cells, partial normalized values, logical record number, physical end line and reasons |
report.json |
Total/accepted/rejected counts, error counts, issues and SHA-256 source/schema hashes |
| Exit code | Meaning |
|---|---|
0 |
The file was processed and all records were accepted; a header-only export also succeeds |
1 |
The file was processed and at least one record was rejected; all three artifacts were written |
2 |
Invalid input/schema, CLI usage, existing output path or filesystem error; no new report directory is published |
Example summary:
{"accepted": 2, "errors_by_code": {"duplicate_key": 2, "invalid_value": 4}, "rejected": 6, "total": 8}Example normalized row:
sku,warehouse,quantity,unit_cost,snapshot_date
PIPE-001,WH-A,12,12500,2026-12-05For a different domain:
python3 -m dq_lab examples/maintenance.csv --schema examples/maintenance.schema.json --output out-maintenance{
"version": 1,
"columns": {
"sku": {"type": "string"},
"quantity": {"type": "integer", "min": "0"},
"checked_on": {"type": "date", "required": false}
},
"unique_by": ["sku"]
}- Supported types:
string,integer,decimal,dateandenumwith avalueslist. - Fields are required by default.
required: falseallows an empty value and does not infer a default. All schema columns still need to exist in the CSV header. - Numeric
min/maxbounds use decimal strings to avoid JSON float rounding. Decimals use PythonDecimal, not binary floating-point conversion. - Outer whitespace is trimmed. Integers and decimals use canonical text (
0012→12,12500.00→12500,-0.00→0). Leading zeros in string identifiers are preserved. - Dates must be real calendar dates in
YYYY-MM-DD. Dates, number separators, units, enum case and missing values are never guessed. - Scientific notation, non-finite numbers, thousands separators and numbers longer than 128 characters are rejected. Enum matching is case-sensitive.
unique_byis one composite key over required columns. All rows with the same normalized key are rejected, including the first. A row invalid in a non-key field still participates when its key is valid. Empty/omittedunique_bydisables this check.- Headers must match schema column names exactly. Header order may differ. Duplicate, missing, unexpected or whitespace-padded column names fail the entire file.
- Bad row widths reject the record. Invalid UTF-8, NUL characters or malformed CSV quoting fail the entire file. UTF-8 BOM and quoted multiline cells are supported.
- Schema typos, unknown options and duplicate JSON keys are rejected rather than silently ignored.
The source bytes are never changed. Rejects contain the original parsed cell values, not a byte-for-byte copy of each source line. Keep the original export for forensic reproduction; its SHA-256 is recorded in the report. Schema hashes use sorted-key JSON with insignificant JSON formatting removed.
flowchart TD
A["CSV bytes + JSON schema"] --> B["Parse and check structure"]
B --> C["Normalize and validate fields"]
C --> R["Check explicit cross-field rules"]
R --> D["Reject all duplicate keys"]
D --> E["Stage complete artifacts"]
E --> F["Publish new output directory"]
dq_lab/core.py is a pure validation layer. dq_lab/__main__.py handles schema loading, CLI exit status and artifact publication. It validates the complete file before staging output next to the destination; a final rename exposes the finished directory. Temporary staging is removed on failure.
The all-duplicates policy avoids choosing an arbitrary winner based on record order. The cost is retaining rows and normalized keys in memory. The CLI targets modest local exports, not multi-gigabyte streams. It assumes one writer per destination and a local filesystem; concurrent hostile writers, crash durability and distributed filesystems are outside this demo's guarantees.
Reports have no wall-clock timestamps or absolute input paths, so identical input/schema produces identical artifact bytes. Record order follows the input. Error counts count individual issues, so their sum can exceed rejected records.
python3 -m unittest discover -s tests -v
python3 demo.pyThe suite has 36 test methods, including subprocess CLI checks, decimal precision, strict dates, duplicate groups, malformed encodings/CSV, multiline cells, conservative normalization, source preservation, deterministic artifacts and refusal to overwrite. GitHub Actions runs the same commands on Python 3.11, 3.12 and 3.13. See validation evidence for the initial local result and scope.
Optional installation for a dq-lab command:
python3 -m pip install .
dq-lab --helpInstallation uses setuptools and may access a package registry for build dependencies. The source-checkout quickstart does not.
- Cross-field numeric/date ordering with explicit rule IDs is implemented; see contract and runnable example.
- Evaluate larger synthetic exports and document memory/time measurements.
- Add an optional review UI only after the CLI contract is stable.
There is no live ERP integration, automatic imputation, streaming engine or machine-learning anomaly detector in this version. CSV outputs are data files; spreadsheet-formula escaping is not performed, so review untrusted exports before opening them in a spreadsheet application.
Original implementation by Efanntyo. Runtime uses only the Python standard library; no upstream project source or company/client data is bundled. MIT licensed. Contribution guide.