/
domain_builder.py
155 lines (135 loc) · 5.13 KB
/
domain_builder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
from abc import ABC, abstractmethod
from typing import Any, Dict, List, Optional, Tuple, Union
from great_expectations.core.batch import Batch, BatchRequestBase
from great_expectations.execution_engine.execution_engine import MetricDomainTypes
from great_expectations.rule_based_profiler.helpers.util import (
get_batch_ids as get_batch_ids_from_batch_list_or_batch_request,
)
from great_expectations.rule_based_profiler.helpers.util import (
get_resolved_metrics_by_key,
)
from great_expectations.rule_based_profiler.helpers.util import (
get_validator as get_validator_using_batch_list_or_batch_request,
)
from great_expectations.rule_based_profiler.types import (
Builder,
Domain,
ParameterContainer,
)
from great_expectations.validator.metric_configuration import MetricConfiguration
class DomainBuilder(ABC, Builder):
"""
A DomainBuilder provides methods to get domains based on one or more batches of data.
"""
def __init__(
self,
data_context: Optional["BaseDataContext"] = None, # noqa: F821
):
"""
Args:
data_context: BaseDataContext associated with DomainBuilder
"""
super().__init__(data_context=data_context)
def get_domains(
self,
variables: Optional[ParameterContainer] = None,
batch_list: Optional[List[Batch]] = None,
batch_request: Optional[Union[BatchRequestBase, dict]] = None,
) -> List[Domain]:
"""
Args:
variables: attribute name/value pairs
batch_list: Explicit list of Batch objects to supply data at runtime.
batch_request: Explicit batch_request used to supply data at runtime.
Returns:
List of Domain objects.
Note: Please do not overwrite the public "get_domains()" method. If a child class needs to check parameters,
then please do so in its implementation of the (private) "_get_domains()" method, or in a utility method.
"""
self.set_batch_list_or_batch_request(
batch_list=batch_list,
batch_request=batch_request,
)
return self._get_domains(variables=variables)
@property
@abstractmethod
def domain_type(self) -> MetricDomainTypes:
pass
@abstractmethod
def _get_domains(
self,
variables: Optional[ParameterContainer] = None,
) -> List[Domain]:
"""
_get_domains is the primary workhorse for the DomainBuilder
"""
pass
def get_table_row_counts(
self,
validator: Optional["Validator"] = None, # noqa: F821
batch_ids: Optional[List[str]] = None,
variables: Optional[ParameterContainer] = None,
) -> Dict[str, int]:
if validator is None:
validator = self.get_validator(variables=variables)
if batch_ids is None:
batch_ids = self.get_batch_ids(variables=variables)
batch_id: str
metric_configurations_by_batch_id: Dict[str, List[MetricConfiguration]] = {
batch_id: [
MetricConfiguration(
metric_name="table.row_count",
metric_domain_kwargs={
"batch_id": batch_id,
},
metric_value_kwargs={
"include_nested": True,
},
metric_dependencies=None,
)
]
for batch_id in batch_ids
}
resolved_metrics_by_batch_id: Dict[
str, Dict[Tuple[str, str, str], Any]
] = get_resolved_metrics_by_key(
validator=validator,
metric_configurations_by_key=metric_configurations_by_batch_id,
)
batch_id: str
resolved_metrics: Dict[Tuple[str, str, str], Any]
metric_value: Any
table_row_count_lists_by_batch_id: Dict[str, List[int]] = {
batch_id: [metric_value for metric_value in resolved_metrics.values()]
for batch_id, resolved_metrics in resolved_metrics_by_batch_id.items()
}
table_row_counts_by_batch_id: Dict[str, int] = {
batch_id: metric_value[0]
for batch_id, metric_value in table_row_count_lists_by_batch_id.items()
}
return table_row_counts_by_batch_id
def get_validator(
self,
variables: Optional[ParameterContainer] = None,
) -> Optional["Validator"]: # noqa: F821
return get_validator_using_batch_list_or_batch_request(
purpose="domain_builder",
data_context=self.data_context,
batch_list=self.batch_list,
batch_request=self.batch_request,
domain=None,
variables=variables,
parameters=None,
)
def get_batch_ids(
self,
variables: Optional[ParameterContainer] = None,
) -> Optional[List[str]]:
return get_batch_ids_from_batch_list_or_batch_request(
data_context=self.data_context,
batch_list=self.batch_list,
batch_request=self.batch_request,
domain=None,
variables=variables,
parameters=None,
)