-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DOCS] Puts 1.0 example code for Checkpoints, Actions, and Result For…
…mat under test. (#10232)
- Loading branch information
1 parent
e025895
commit 798e2bd
Showing
11 changed files
with
363 additions
and
205 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/choose_result_format.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
""" | ||
This is an example script for how to choose a Result Format. | ||
To test, run: | ||
pytest --docs-tests -k "docs_example_choose_result_format" tests/integration/test_script_runner.py | ||
""" | ||
|
||
|
||
def set_up_context_for_example(context): | ||
# Create a Batch Definition (monthly) | ||
source_folder = "./data/folder_with_data" | ||
data_source_name = "my_data_source" | ||
asset_name = "my_data_asset" | ||
batch_definition_name = "my_batch_definition" | ||
batch_definition_regex = ( | ||
r"yellow_tripdata_sample_(?P<year>\d{4})-(?P<month>\d{2})\.csv" | ||
) | ||
batch_definition = ( | ||
context.data_sources.add_pandas_filesystem( | ||
name=data_source_name, base_directory=source_folder | ||
) | ||
.add_csv_asset(name=asset_name) | ||
.add_batch_definition_monthly( | ||
name=batch_definition_name, regex=batch_definition_regex | ||
) | ||
) | ||
assert batch_definition.name == batch_definition_name | ||
|
||
# Create an Expectation Suite | ||
expectation_suite = context.suites.add( | ||
gx.ExpectationSuite(name="my_expectation_suite") | ||
) | ||
# Add some Expectations | ||
# ColumnMap | ||
expectation = gx.expectations.ExpectColumnValuesToBeInSet( | ||
column="passenger_count", value_set=[1, 2, 3, 4, 5] | ||
) | ||
expectation_suite.add_expectation(expectation) | ||
# ColumnAggregate | ||
expectation = gx.expectations.ExpectColumnMaxToBeBetween( | ||
column="passenger_count", min_value=4, max_value=5 | ||
) | ||
expectation_suite.add_expectation(expectation) | ||
|
||
# Create a Validation Definition | ||
validation_definition = context.validation_definitions.add( | ||
gx.ValidationDefinition( | ||
data=batch_definition, | ||
suite=expectation_suite, | ||
name="my_validation_definition", | ||
) | ||
) | ||
|
||
# Create a Checkpoint | ||
checkpoint_name = "my_checkpoint" | ||
validation_definitions = [validation_definition] | ||
action_list = [] | ||
context.checkpoints.add( | ||
gx.Checkpoint( | ||
name=checkpoint_name, | ||
validation_definitions=validation_definitions, | ||
actions=action_list, | ||
result_format={"result_format": "COMPLETE"}, | ||
) | ||
) | ||
|
||
|
||
# EXAMPLE SCRIPT STARTS HERE: | ||
# <snippet name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/choose_result_format.py - full code example"> | ||
import great_expectations as gx | ||
|
||
context = gx.get_context() | ||
# Hide this | ||
set_up_context_for_example(context) | ||
|
||
validation_name = "my_validation_definition" | ||
validation_definition = context.validation_definitions.get(validation_name) | ||
|
||
checkpoint_name = "my_checkpoint" | ||
checkpoint = context.checkpoints.get(checkpoint_name) | ||
|
||
# BOOLEAN_ONLY Result Format | ||
# <snippet name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/choose_result_format.py - boolean_only Result Format"> | ||
boolean_result_format_dict = {"result_format": "BOOLEAN_ONLY"} | ||
# </snippet> | ||
validation_definition.run(result_format=boolean_result_format_dict) | ||
# checkpoint.run(result_format=boolean_result_format_dict) | ||
|
||
# BASIC Result Format | ||
# <snippet name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/choose_result_format.py - basic Result Format"> | ||
basic_result_format_dict = {"result_format": "BASIC"} | ||
# </snippet> | ||
validation_definition.run(result_format=basic_result_format_dict) | ||
# checkpoint.run(result_format=basic_result_format_dict) | ||
|
||
# SUMMARY Result Format | ||
# <snippet name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/choose_result_format.py - summary Result Format"> | ||
summary_result_format_dict = {"result_format": "SUMMARY"} | ||
# </snippet> | ||
validation_definition.run(result_format=summary_result_format_dict) | ||
# checkpoint.run(result_format=summary_result_format_dict) | ||
|
||
# COMPLETE Result Format | ||
# <snippet name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/choose_result_format.py - complete Result Format"> | ||
complete_result_format_dict = {"result_format": "COMPLETE"} | ||
# </snippet> | ||
validation_definition.run(result_format=complete_result_format_dict) | ||
# checkpoint.run(result_format=complete_result_format_dict) | ||
# </snippet> |
98 changes: 98 additions & 0 deletions
98
.../docs/core/trigger_actions_based_on_results/_examples/create_a_checkpoint_with_actions.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
""" | ||
This is an example script for how to create a Checkpoint with Actions. | ||
To test, run: | ||
pytest --docs-tests -k "docs_example_create_a_checkpoint" tests/integration/test_script_runner.py | ||
""" | ||
|
||
|
||
def set_up_context_for_example(context): | ||
# Create a Batch Definition | ||
batch_definition = ( | ||
context.data_sources.add_pandas_filesystem( | ||
name="my_data_source", base_directory="./data/folder_with_data" | ||
) | ||
.add_csv_asset(name="my_data_asset") | ||
.add_batch_definition_path( | ||
name="my_batch_definition", path="yellow_tripdata_sample_2019-01.csv" | ||
) | ||
) | ||
|
||
# Create an Expectation Suite | ||
expectation_suite = context.suites.add( | ||
gx.ExpectationSuite(name="my_expectation_suite") | ||
) | ||
# Add some Expectations | ||
expectation_suite.add_expectation( | ||
gx.expectations.ExpectColumnValuesToNotBeNull(column="pickup_datetime") | ||
) | ||
expectation_suite.add_expectation( | ||
gx.expectations.ExpectColumnValuesToNotBeNull(column="passenger_count") | ||
) | ||
|
||
# Create a Validation Definition | ||
context.validation_definitions.add( | ||
gx.ValidationDefinition( | ||
data=batch_definition, | ||
suite=expectation_suite, | ||
name="my_validation_definition", | ||
) | ||
) | ||
|
||
|
||
# EXAMPLE SCRIPT STARTS HERE: | ||
# <snippet name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/create_a_checkpoint_with_actions.py - full code example"> | ||
import great_expectations as gx | ||
|
||
context = gx.get_context() | ||
# Hide this | ||
set_up_context_for_example(context) | ||
|
||
# Create a list of one or more Validation Definitions for the Checkpoint to run | ||
# <snippet name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/create_a_checkpoint_with_actions.py - create a Validation Definitions list"> | ||
validation_definitions = [ | ||
context.validation_definitions.get("my_validation_definition") | ||
] | ||
# </snippet> | ||
|
||
# Create a list of Actions for the Checkpoint to perform | ||
# <snippet name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/create_a_checkpoint_with_actions.py - define an Action list"> | ||
action_list = [ | ||
# This Action sends a Slack Notification if an Expectation fails. | ||
gx.checkpoint.SlackNotificationAction( | ||
name="send_slack_notification_on_failed_expectations", | ||
slack_token="${validation_notification_slack_webhook}", | ||
slack_channel="${validation_notification_slack_channel}", | ||
notify_on="failure", | ||
show_failed_expectations=True, | ||
), | ||
# This Action updates the Data Docs static website with the Validation | ||
# Results after the Checkpoint is run. | ||
gx.checkpoint.UpdateDataDocsAction( | ||
name="update_all_data_docs", | ||
), | ||
] | ||
# </snippet> | ||
|
||
# Create the Checkpoint | ||
# <snippet name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/create_a_checkpoint_with_actions.py - create a Checkpoint"> | ||
checkpoint_name = "my_checkpoint" | ||
checkpoint = gx.Checkpoint( | ||
name=checkpoint_name, | ||
validation_definitions=validation_definitions, | ||
actions=action_list, | ||
result_format={"result_format": "COMPLETE"}, | ||
) | ||
# </snippet> | ||
|
||
# Save the Checkpoint to the Data Context | ||
# <snippet name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/create_a_checkpoint_with_actions.py - save the Checkpoint to the Data Context"> | ||
context.checkpoints.add(checkpoint) | ||
# </snippet> | ||
|
||
# Retrieve the Checkpoint later | ||
# <snippet name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/create_a_checkpoint_with_actions.py - retrieve a Checkpoint from the Data Context"> | ||
checkpoint_name = "my_checkpoint" | ||
checkpoint = context.checkpoints.get(checkpoint_name) | ||
# </snippet> | ||
# </snippet> |
94 changes: 94 additions & 0 deletions
94
docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/run_a_checkpoint.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
""" | ||
This is an example script for how to run a Checkpoint. | ||
To test, run: | ||
pytest --docs-tests -k "docs_example_run_a_checkpoint" tests/integration/test_script_runner.py | ||
""" | ||
|
||
|
||
def set_up_context_for_example(context): | ||
# Create a Batch Definition (monthly) | ||
source_folder = "./data/folder_with_data" | ||
data_source_name = "my_data_source" | ||
asset_name = "my_data_asset" | ||
batch_definition_name = "my_batch_definition" | ||
batch_definition_regex = ( | ||
r"yellow_tripdata_sample_(?P<year>\d{4})-(?P<month>\d{2})\.csv" | ||
) | ||
batch_definition = ( | ||
context.data_sources.add_pandas_filesystem( | ||
name=data_source_name, base_directory=source_folder | ||
) | ||
.add_csv_asset(name=asset_name) | ||
.add_batch_definition_monthly( | ||
name=batch_definition_name, regex=batch_definition_regex | ||
) | ||
) | ||
assert batch_definition.name == batch_definition_name | ||
|
||
# Create an Expectation Suite | ||
expectation_suite = context.suites.add( | ||
gx.ExpectationSuite(name="my_expectation_suite") | ||
) | ||
# Add some Expectations | ||
expectation_suite.add_expectation( | ||
gx.expectations.ExpectColumnValuesToNotBeNull(column="pickup_datetime") | ||
) | ||
expectation_suite.add_expectation( | ||
# <snippet name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/run_a_checkpoint.py - example Expectation"> | ||
gx.expectations.ExpectColumnMaxToBeBetween( | ||
column="fare", | ||
min_value={"$PARAMETER": "expect_fare_max_to_be_above"}, | ||
max_value={"$PARAMETER": "expect_fare_max_to_be_below"}, | ||
) | ||
# </snippet> | ||
) | ||
|
||
# Create a Validation Definition | ||
validation_definition = context.validation_definitions.add( | ||
gx.ValidationDefinition( | ||
data=batch_definition, | ||
suite=expectation_suite, | ||
name="my_validation_definition", | ||
) | ||
) | ||
|
||
# Create a Checkpoint | ||
checkpoint_name = "my_checkpoint" | ||
validation_definitions = [validation_definition] | ||
action_list = [] | ||
context.checkpoints.add( | ||
gx.Checkpoint( | ||
name=checkpoint_name, | ||
validation_definitions=validation_definitions, | ||
actions=action_list, | ||
result_format={"result_format": "COMPLETE"}, | ||
) | ||
) | ||
|
||
|
||
# EXAMPLE SCRIPT STARTS HERE: | ||
# <snippet name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/run_a_checkpoint.py - full code example"> | ||
import great_expectations as gx | ||
|
||
context = gx.get_context() | ||
# Hide this | ||
set_up_context_for_example(context) | ||
|
||
checkpoint = context.checkpoints.get("my_checkpoint") | ||
|
||
batch_parameters = {"month": "01", "year": "2019"} | ||
|
||
# <snippet name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/run_a_checkpoint.py - define Expectation Parameters"> | ||
expectation_parameters = { | ||
"expect_fare_max_to_be_above": 5.00, | ||
"expect_fare_max_to_be_below": 1000.00, | ||
} | ||
# </snippet> | ||
|
||
# <snippet name="docs/docusaurus/docs/core/trigger_actions_based_on_results/_examples/run_a_checkpoint.py - run a Checkpoint"> | ||
validation_results = checkpoint.run( | ||
batch_parameters=batch_parameters, expectation_parameters=expectation_parameters | ||
) | ||
# </snippet> | ||
# </snippet> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.