Skip to main content

from_dict()

Create a contract from a dictionary.

Usage

Source

from_dict(data)

This is the reverse of to_dict(). Use it when the contract comes from somewhere other than a YAML file: a JSON document, an API response, a database record, or a dictionary built in code. To read a YAML file directly, use from_yaml().

The dictionary uses the contract’s parameter names as keys. Only "name" is required. "schema" should map column names to type strings, "steps" should be a list of single-key mappings from method name to arguments, and "thresholds" should map any of "warning", "error", and "critical" to a value. The parsed contract is checked as usual, so an invalid direction, on_violation, or step method raises an error.

Parameters

data: dict[str, Any]
A dictionary describing a contract, in the form produced by to_dict().

Returns

Contract
A new Contract object.

Raises

ValueError

If "name" is missing or empty, if "direction" or "on_violation" has an invalid value, or if a step names a method that contracts don’t support.

TypeError
If "steps" isn’t a list, or if a step’s arguments aren’t a dictionary.

Examples

Suppose a contract definition arrives as JSON from a configuration service. After parsing, it’s a dictionary that from_dict() can turn into a Contract.

import json
import pointblank as pb

contract_json = '''
{
  "name": "shipments",
  "version": "0.3.0",
  "owner": "logistics",
  "schema": {"shipment_id": "String", "weight_kg": "Float64"},
  "steps": [
    {"col_vals_not_null": {"columns": "shipment_id"}},
    {"col_vals_between": {"columns": "weight_kg", "left": 0, "right": 1000}}
  ],
  "thresholds": {"warning": 1, "error": 0.05}
}
'''

shipments_contract = pb.Contract.from_dict(json.loads(contract_json))

shipments_contract
Contract(name='shipments', direction='source', version='0.3.0', schema=<defined>, steps=2)

The result is a regular Contract, ready to use on data:

import polars as pl

shipments = pl.DataFrame(
    {"shipment_id": ["S1", "S2", "S3"], "weight_kg": [12.5, 980.0, 1250.0]}
)

shipments_contract.validate(shipments)
Pointblank Validation
Contract: shipments v0.3.0
PolarsshipmentsWARNING1ERROR0.05CRITICAL—
STEP COLUMNS VALUES TBL EVAL UNITS PASS FAIL W E C EXT
#4CA64C 1
col_schema_match
col_schema_match()
— SCHEMA ✓ 1 1
1.00
0
0.00
○ ○ — —
#4CA64C 2
col_vals_not_null
col_vals_not_null()
shipment_id — ✓ 3 3
1.00
0
0.00
○ ○ — —
#EBBC14 3
col_vals_between
col_vals_between()
weight_kg [0, 1000] ✓ 3 2
0.67
1
0.33
● ● —
Owner: logisticsVersion: 0.3.0

Notes

Step 1 (schema_check) ✓ Schema validation passed.

Schema Comparison
TABLE SCHEMA
COLUMN DATA TYPE COLUMN DATA TYPE
1 shipment_id String 1 shipment_id ✓ String ✓
2 weight_kg Float64 2 weight_kg ✓ Float64 ✓
Supplied Column Schema:
[('shipment_id', 'String'), ('weight_kg', 'Float64')]
Schema Match Settings
COMPLETE
IN ORDER
COLUMN ≠ column
DTYPE ≠ dtype
float ≠ float64

The schema check and the not-null check pass, and the range check flags the shipment weighing 1250 kg.