I had a little puzzle that I wanted to share: I had a specific expectation in mind, already in my suite, that I wanted to update, but I wasn’t sure how to get at it.
Turns out there are two ways! This is as of GX 1.2.1.
1. Find it by expectation type
Go through all your expectations until you find the type of the one you were looking for.
import great_expectations as gx
context = gx.get_context()
# Add a suite
suite_name = "my_suite"
suite = gx.ExpectationSuite(name=suite_name)
suite = context.suites.add(suite)
# Add my first expectation
num_rows_positive = gx.expectations.ExpectTableRowCountToBeBetween(
min_value=1,
max_value=None,
)
suite.add_expectation(num_rows_positive)
# Add my second expectation
num_rows_range = gx.expectations.ExpectTableRowCountToBeBetween(
min_value={"$PARAMETER": "expect_min_num_rows"},
max_value={"$PARAMETER": "expect_max_num_rows"},
)
suite.add_expectation(num_rows_range)
# Check the type of each expectation. This returns the first expectation of that type.
exp = next(
e for e in suite.expectations if isinstance(e, gx.expectations.ExpectTableRowCountToBeBetween)
)
print(exp)
2. Find it using custom meta data
In my case, I have two expectations of the same type. I expect my table to have a positive row count, always. AND, I expect the row count to be within a given range. To distinguish between the two expectations, I will set some custom meta data on each one, specifically, a name.
suite_name = "my_suite"
suite = gx.ExpectationSuite(name=suite_name)
suite = context.suites.add(suite)
# Add my static-valued expectation
num_rows_positive = gx.expectations.ExpectTableRowCountToBeBetween(
min_value=1,
max_value=None,
meta={"name": "positive number of rows"},
)
suite.add_expectation(num_rows_positive)
# Add my parameterized expectation
num_rows_range = gx.expectations.ExpectTableRowCountToBeBetween(
min_value={"$PARAMETER": "expect_min_num_rows"},
max_value={"$PARAMETER": "expect_max_num_rows"},
meta={"name": "row range parameterized"},
)
suite.add_expectation(num_rows_range)
# Find just the expectation that I want, by the name that I gave it
exp = next(
e for e in suite.expectations if e.meta["name"] == "row range parameterized"
)
print(exp)
Easy! The world is your oyster with custom meta data on your expectations.