Usage Guide#
Installation#
Install DataSpector from PyPI:
pip install dataspector
# or with uv
uv add dataspector
Optional extras (database backends connect through SQLAlchemy; SQLite works out of the box):
pip install "dataspector[postgres]"— PostgreSQL (psycopg[binary]).pip install "dataspector[mysql]"— MySQL / MariaDB (pymysql).pip install "dataspector[oracle]"— Oracle thin mode (oracledb).pip install "dataspector[duckdb]"— DuckDB (duckdb-engine).pip install "dataspector[snowflake]"— Snowflake (snowflake-sqlalchemy).pip install "dataspector[db]"— all pure-wheel SQLAlchemy DB drivers above.pip install "dataspector[odbc]"— SQL Server and MS Access (pyodbc).pip install "dataspector[parquet]"— Apache Parquet viapyarrow.pip install "dataspector[docs]"— Sphinx,numpydoc, andpydata-sphinx-theme.
For local development from a repository checkout using uv:
uv sync --all-groups
Locating Data Sources#
GenericData (and the Location column in Excel
driver workbooks) uses a unified URI/scheme locator syntax and a structured
DataSourceSpec dataclass/dict across single files,
COBOL copybook + data pairs, and any SQLAlchemy-compatible database flavour
(SQLite, PostgreSQL, MySQL, MariaDB, Snowflake, SQL Server, Oracle, DuckDB,
MS Access):
Source Category |
Unified Locator Examples |
|---|---|
Single Files (Auto or Explicit Format) |
|
Excel Sheets & HDF5 Keys |
|
COBOL Copybook + Data Extract |
|
Embedded Databases (SQLite, DuckDB, MS Access) |
|
SQL Databases (Postgres, MySQL, MariaDB, Snowflake, MSSQL, Oracle) |
|
Structured Python / YAML Spec |
|
Tip
Environment Variable Interpolation: Any locator string can reference
environment variables using ${VAR_NAME} (for example,
postgresql+psycopg://${PG_USER}:${PG_PASSWORD}@db.internal:5432/prod#fact_sales or
${DATABASE_URL}#fact_sales) so credentials never need to be stored in
Excel driver workbooks. See Supported Data Sources & Unified Locator Syntax for the complete reference on
SQLAlchemy database backends, optional extras, and DataSourceSpec.
Running Checks from Python#
Use run_checks_on_location() to run a list of checks on
a single source:
import dataspector
history = dataspector.run_checks_on_location(
"tests/data/sample_csv.csv",
[
("source_exists", {}),
("source_has_N_fields", {"n_fields": 5}),
("record_contains", {"field": "Cost Center"}),
("record_count", {"nrows": 31}),
("record_field_numeric_value_between", {"field": "Cost [CAD]", "constraint": (0, 200000)}),
],
)
print(history.all_passed)
dataspector.pretty_print_results(history)
Results, notes, and any captured exceptions are stored on the returned
CheckeredHistory object keyed by check index.
Running Checks from an Excel Driver File#
For batch validation across many sources, define locations and checks in an
Excel workbook — download the sample test_driver.xlsx
(also viewable on GitHub,
alongside the sample data files in
tests/data/):
Location Details (header on row 2): columns
Location,Source Name, andReader Arguments.Checks to Run (header on row 4): columns
Process(Y/1/True),Source Name,Check,Keyword Arguments, andSeverity(ErrororWarning).
Keyword Arguments and Reader Arguments accept JSON objects, brace-less
JSON pairs ("widths": [32, 32]), Python literals, key=value pairs, or
single scalar values (automatically mapped to the check’s first parameter).
Execute from the CLI (powered by jsonargparse and
DataSpectorSettings):
uv run dataspector --driver test_driver.xlsx --output report.xlsx
uv run dataspector --driver test_driver.xlsx --output report.xlsx --overwrite true --use_multiprocess true
uv run dataspector --config run.yaml
Or programmatically via load_and_run_checks() and
write_excel_report():
import dataspector
results = dataspector.load_and_run_checks("test_driver.xlsx", use_multiprocessing=True)
dataspector.write_excel_report(results, "report.xlsx")
Extending DataSpector#
Adding Custom Checks#
Check functions live in dataspector.checkers (organized into
dataspector.checkers.source, dataspector.checkers.record, and
dataspector.checkers.utils) and follow a clean contract:
Source checks begin with
source_and accept aGenericDataas their first argument.Record checks begin with
record_and accept apandas.DataFramechunk as their first argument.Every check returns a
CheckOutcomedataclass withpassed(True/False/Nonewhile streaming), optionalnotes, and updatedkwargs.Multi-chunk checks are decorated with
requires_all_data()and accept_last_chunk: bool = True.
Adding New Storage Formats#
Extend GenericData so that its metadata properties
(exists, columns, last_modified, list_of_tables,
number_of_records) and yield_data()
handle your format.
Guides & Benchmarks
- Supported Data Sources & Unified Locator Syntax
- Read & Convert COBOL Mainframe Extracts (
coboller)- Interactive Exploration & Conversion Walkthrough
- Command-Line Inspection & Conversion (
coboller) - Supported COBOL Data Types
- Structural COBOL Features
- Streaming Large Mainframe Files
- Automatic Pandera Schema Generation
- Low-Level Packing & Unpacking Helpers
- Real-World Mainframe Extract Benchmark & Performance
- Checks, Reporting & Pandera Integration