Cập nhật Expectation Suites

Nhập môn Data Quality với Great Expectations

Davina Moossazadeh

Data Scientist

Thêm Expectations

expectation = gx.expectations.ExpectTableColumnCountToEqual(
    value=10
)
suite = gx.ExpectationSuite(
    name="my_suite"
)
# Thêm Expectation vào Suite
suite.add_expectation(
    expectation=expectation
)
Nhập môn Data Quality với Great Expectations

Nhiều Expectation Suite

# Tạo một Expectation Suite khác
another_suite = gx.ExpectationSuite(name="my_other_suite")
# Thêm cùng Expectation vào Suite mới
another_suite.add_expectation(expectation=expectation)

Expectations không thể thuộc nhiều Suite cùng lúc:

RuntimeError: Cannot add Expectation because it already belongs to an 
ExpectationSuite. If you want to update an existing Expectation, please call 
Expectation.save(). If you are copying this Expectation to a new ExpectationSuite, 
please copy it first (the core expectations and some others support 
copy(expectation)) and set `Expectation.id = None`.

DNT_CURLLY_TAG_2

Nhập môn Data Quality với Great Expectations

Sao chép Expectations

If you are copying this Expectation to a new ExpectationSuite, please copy it first 
(the core expectations and some others support copy(expectation)) and set 
`Expectation.id = None`.

Sao chép Expectation, đặt .idNone, rồi thêm vào Suite mới mà không lỗi:

expectation_copy = expectation.copy()

expectation_copy.id = None
another_suite.add_expectation(
    expectation=expectation_copy
)
Nhập môn Data Quality với Great Expectations

Kiểm tra Expectations

print(
    expectation_copy in another_suite.expectations
)
True
Nhập môn Data Quality với Great Expectations

Xóa Expectations

Thêm

.add_expectation()

suite.add_expectation(
    expectation=expectation
)

Xóa

.delete_expectation()

suite.delete_expectation(
    expectation=expectation
)
Nhập môn Data Quality với Great Expectations

Điều chỉnh Expectations

Cập nhật thuộc tính .value và lưu thay đổi:

expectation = gx.expectations.ExpectTableColumnCountToEqual(
    value=10
)

expectation.value = 11
expectation.save()

Đảm bảo Expectation thuộc một Suite, nếu không sẽ gặp lỗi:

RuntimeError: Expectation must be added to ExpectationSuite before it can be saved.
Nhập môn Data Quality với Great Expectations

Cập nhật một Expectation Suite

suite = gx.ExpectationSuite(name="my_suite") 
validation_definition = gx.ValidationDefinition(
    data=batch_definition, suite=suite, name="my_validation_definition"
)
# Định nghĩa Expectation
col_name_expectation = gx.expectations.ExpectColumnToExist(column="GHI")

# Thêm Expectation vào Suite suite.add_expectation(expectation=col_name_expectation)
# Chạy Validation Definition gắn với Suite validation_results = validation_definition.run()
Nhập môn Data Quality với Great Expectations

Lưu một Expectation Suite

Lưu thay đổi ở Suite trước khi chạy Validation Definition để tránh lỗi:

validation_results = validation_definition.run()
ResourceFreshnessAggregateError: ExpectationSuite 'my_suite' has changed since it 
has last been saved. Please update with `<SUITE_OBJECT>.save()`, then try your 
action again.
Nhập môn Data Quality với Great Expectations

Lưu một Expectation Suite

Dùng phương thức .save() để lưu Suite và chạy Validation Definition không lỗi:

suite.save()

validation_results = validation_definition.run()
print(validation_results.success)
False
Nhập môn Data Quality với Great Expectations

Ghi nhớ nhanh

Sao chép Expectation:

expectation_copy = expectation.copy()
expectation_copy.id = None

Kiểm tra Expectation có trong Suite không:

expectation in suite.expectations

Xóa Expectation:

suite.delete_expectation(expectation)

Cập nhật giá trị Expectation:

expectation.value = new_value

Lưu thay đổi của Expectation:

expectation.save()

Lưu thay đổi của Expectation Suite:

suite.save()
Nhập môn Data Quality với Great Expectations

Ayo berlatih!

Nhập môn Data Quality với Great Expectations

Preparing Video For Download...