A directory of standards, kept as YAML files.
StandardsRegistry(
path,
allow_python=False,
)
A StandardsRegistry reads every standard in a directory (the .yaml and .yml files written by Standard.to_yaml(), or written by hand) and applies them all to a source at once. Keeping an organization’s standards in one directory under version control makes them the shared, reviewed definition of what its tables should look like, and a scheduled job can apply the whole registry with apply_all() (or with pb standard apply <directory> --source <source> on the command line).
The files of subdirectories are read too, so standards can be grouped by domain or team. Files and directories whose names start with a dot are skipped. Each file holds one standard, and the names of the standards must be unique within the registry.
Parameters
path: str | Path
-
The directory of the standards.
allow_python: bool = False
-
Whether to run Python code in the standards that isn’t in the subset that’s read without running it (see
Standard.from_yaml()). Only allow it for standards you trust. The default is False.
Returns
StandardsRegistry
-
A registry of the standards of the directory, read when it’s created.
Raises
FileNotFoundError
-
If the directory doesn’t exist.
NotADirectoryError
-
If path= isn’t a directory.
ValueError
-
If a file can’t be read as a standard (the message names the file), or two files have standards of the same name.
Warns
UserWarning
-
If the directory has no standards.
Examples
Let’s write two standards to a directory: one for email columns and one for the uniqueness of the dimension tables.
import pointblank as pb
import polars as pl
import tempfile
from pathlib import Path
standards_dir = Path(tempfile.mkdtemp())
pb.Standard(
name="email-fields",
steps=[pb.Step("col_vals_regex", columns=pb.matches("email"), pattern="^[^@]+@[^@]+$")],
).to_yaml(standards_dir / "email-fields.yaml")
pb.Standard(
name="dimension-keys",
steps=[pb.Step("rows_distinct")],
applies_to=pb.tbl_starts_with("dim_"),
).to_yaml(standards_dir / "dimensions" / "dimension-keys.yaml")
registry = pb.StandardsRegistry(standards_dir)
registry
StandardsRegistry: /tmp/tmps7bf5ez3 (2 standards)
email-fields 1 step, applies to every table [email-fields.yaml]
dimension-keys 1 step, applies to tbl_starts_with('dim_') [dimensions/dimension-keys.yaml]
The standard in the subdirectory was read too. Applying the registry to a source applies each standard to the tables it selects:
tables = {
"dim_customers": pl.DataFrame({"id": [1, 1], "email": ["a@x.com", "a@x.com"]}),
"orders": pl.DataFrame({"id": [7], "contact_email": ["not-an-address"]}),
}
results = registry.apply_all(tables)
{name: result.failing_tables() for name, result in results.items()}
{'email-fields': ['orders'], 'dimension-keys': ['dim_customers']}
The duplicate row fails dim_customers against the dimension standard, and the malformed address fails orders against the email standard.