Skip to main content

Standard.from_dict()

Create a standard from a dictionary.

Usage

Source

Standard.from_dict(
    data,
    allow_python=False,
)

This is the reverse of to_dict(). Use it when a standard comes from somewhere other than a YAML file, such as a JSON document or a configuration service. To read a YAML file, use from_yaml().

The keys are the standard’s parameter names, and only "name" is required. "steps" is a list of single-key mappings from a method’s name to its arguments, "thresholds" maps any of "warning", "error", and "critical" to a value (or is a number or a list of the levels, as with Validate(thresholds=)), and "applies_to" is a {"python": <code>} block that makes a table selector. A column selector in a step’s arguments is a {"python": <code>} block too.

The code of a block is read without being run when it only calls the selector functions (pb.matches(), pb.tbl_starts_with(), pb.in_schema(), and the others) with literal arguments, combined with &, |, -, and ~. Any other code is only run with allow_python=True, since it can do anything the Python session can.

Parameters

data: dict[str, Any]

A dictionary describing a standard, in the form produced by to_dict().

allow_python: bool = False
Whether to run Python code that isn’t in the subset that’s read without running it (e.g., a Polars expression in a col_vals_expr step). Only allow it for standards you trust. The default is False.

Returns

Standard
A new Standard object.

Raises

ValueError

If "name" is missing, a key isn’t one of the standard’s fields, or a step names something other than a validation method.

TypeError

If "steps" isn’t a list, or "applies_to" doesn’t make a table selector.

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

Examples

Suppose a standard arrives as JSON from a configuration service. Once it’s parsed, it’s a dictionary that from_dict() turns into a Standard:

import json
import pointblank as pb

standard_json = '''
{
  "name": "amount-columns",
  "owner": "finance",
  "applies_to": {"python": "pb.tbl_contains('payment')"},
  "steps": [
    {"col_vals_ge": {"columns": {"python": "pb.ends_with('_amount')"}, "value": 0}}
  ]
}
'''

amount_standard = pb.Standard.from_dict(json.loads(standard_json))

amount_standard
Standard(name='amount-columns', steps=1, applies_to=tbl_contains('payment'))

The selectors were read from the blocks without running any code, so the standard is ready to be applied to the payment tables of a source.