Skip to main content

Standard.from_yaml()

Read a standard from a YAML file.

Usage

Source

Standard.from_yaml(
    path,
    allow_python=False,
)

Keeping standards in YAML files makes them data: they can be reviewed and versioned like any other change, shared between teams, and collected in a directory that a StandardsRegistry reads. The file holds the standard under a top-level standard: key, as to_yaml() writes it, or has the standard’s fields at the top level. A hand-written standard might look like this:

standard:
  name: email-fields
  version: 1.0.0
  owner: data-governance
  applies_to:
    python: pb.in_schema("marts") & pb.tbl_matches("^(customers|orders)$")
  steps:
  - col_vals_not_null:
      columns:
        python: pb.matches("email")
  - col_vals_regex:
      columns:
        python: pb.matches("email")
      pattern: ^[^@]+@[^@]+$
  thresholds:
    warning: 1

The python: blocks make the selectors. Code that only calls the selector functions with literal arguments (combined with &, |, -, and ~) is read without being run. Other code is only run with allow_python=True (see from_dict()).

Parameters

path: str | Path

The path to the YAML file.

allow_python: bool = False
Whether to run Python code that isn’t in the subset that’s read without running it. Only allow it for standards you trust. The default is False.

Returns

Standard
A new Standard object.

Raises

FileNotFoundError

If no file exists at path=.

ValueError

If the file is empty or a field has an invalid value (see from_dict()).

YAMLValidationError
If a block has Python code that isn’t allowed to run, or running it fails.

Examples

Let’s write a standard for the key columns of the staging tables to a file and read it.

import pointblank as pb
import polars as pl
import tempfile
from pathlib import Path

standard_yaml = '''
standard:
  name: staging-keys
  applies_to:
    python: pb.tbl_starts_with("stg_")
  steps:
  - col_vals_not_null:
      columns:
        python: pb.ends_with("_id")
'''

standard_path = Path(tempfile.mkdtemp()) / "staging-keys.yaml"
standard_path.write_text(standard_yaml)

keys_standard = pb.Standard.from_yaml(standard_path)

keys_standard
Standard(name='staging-keys', steps=1, applies_to=tbl_starts_with('stg_'))

The standard works just like one written in Python:

keys_standard.apply(
    {
        "stg_orders": pl.DataFrame({"order_id": [1, 2], "customer_id": [7, None]}),
        "stg_customers": pl.DataFrame({"customer_id": [7, 8]}),
        "orders": pl.DataFrame({"order_id": [None]}),
    }
).failing_tables()
['stg_orders']

The missing customer ID fails the stg_orders table, and the orders table isn’t a staging table, so it wasn’t validated.