phlo-core-plugins 0.2.3__tar.gz → 0.2.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {phlo_core_plugins-0.2.3 → phlo_core_plugins-0.2.4}/PKG-INFO +1 -1
- {phlo_core_plugins-0.2.3 → phlo_core_plugins-0.2.4}/pyproject.toml +1 -1
- phlo_core_plugins-0.2.4/src/phlo_core/__init__.py +42 -0
- phlo_core_plugins-0.2.4/src/phlo_core/quality/__init__.py +52 -0
- phlo_core_plugins-0.2.4/src/phlo_core/quality/freshness_check.py +155 -0
- phlo_core_plugins-0.2.4/src/phlo_core/quality/null_check.py +155 -0
- phlo_core_plugins-0.2.4/src/phlo_core/quality/schema_check.py +130 -0
- phlo_core_plugins-0.2.4/src/phlo_core/quality/uniqueness_check.py +156 -0
- phlo_core_plugins-0.2.4/src/phlo_core/sources/__init__.py +40 -0
- phlo_core_plugins-0.2.4/src/phlo_core/sources/rest_api.py +296 -0
- {phlo_core_plugins-0.2.3 → phlo_core_plugins-0.2.4}/src/phlo_core_plugins.egg-info/PKG-INFO +1 -1
- {phlo_core_plugins-0.2.3 → phlo_core_plugins-0.2.4}/tests/test_quality_plugins.py +1 -0
- {phlo_core_plugins-0.2.3 → phlo_core_plugins-0.2.4}/tests/test_rest_api_plugin.py +3 -2
- phlo_core_plugins-0.2.3/src/phlo_core/__init__.py +0 -16
- phlo_core_plugins-0.2.3/src/phlo_core/quality/__init__.py +0 -13
- phlo_core_plugins-0.2.3/src/phlo_core/quality/freshness_check.py +0 -45
- phlo_core_plugins-0.2.3/src/phlo_core/quality/null_check.py +0 -34
- phlo_core_plugins-0.2.3/src/phlo_core/quality/schema_check.py +0 -34
- phlo_core_plugins-0.2.3/src/phlo_core/quality/uniqueness_check.py +0 -34
- phlo_core_plugins-0.2.3/src/phlo_core/sources/__init__.py +0 -5
- phlo_core_plugins-0.2.3/src/phlo_core/sources/rest_api.py +0 -90
- {phlo_core_plugins-0.2.3 → phlo_core_plugins-0.2.4}/README.md +0 -0
- {phlo_core_plugins-0.2.3 → phlo_core_plugins-0.2.4}/setup.cfg +0 -0
- {phlo_core_plugins-0.2.3 → phlo_core_plugins-0.2.4}/src/phlo_core_plugins.egg-info/SOURCES.txt +0 -0
- {phlo_core_plugins-0.2.3 → phlo_core_plugins-0.2.4}/src/phlo_core_plugins.egg-info/dependency_links.txt +0 -0
- {phlo_core_plugins-0.2.3 → phlo_core_plugins-0.2.4}/src/phlo_core_plugins.egg-info/entry_points.txt +0 -0
- {phlo_core_plugins-0.2.3 → phlo_core_plugins-0.2.4}/src/phlo_core_plugins.egg-info/requires.txt +0 -0
- {phlo_core_plugins-0.2.3 → phlo_core_plugins-0.2.4}/src/phlo_core_plugins.egg-info/top_level.txt +0 -0
- {phlo_core_plugins-0.2.3 → phlo_core_plugins-0.2.4}/tests/test_integration_core_plugins.py +0 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""Core plugins for Phlo.
|
|
2
|
+
|
|
3
|
+
This package provides the foundational set of plugins bundled with Phlo,
|
|
4
|
+
including quality check plugins and source connector plugins. These plugins
|
|
5
|
+
are automatically available when Phlo is installed.
|
|
6
|
+
|
|
7
|
+
Quality Checks:
|
|
8
|
+
- NullCheckPlugin: Validates column completeness by checking for null values.
|
|
9
|
+
- UniquenessCheckPlugin: Validates primary key uniqueness.
|
|
10
|
+
- FreshnessCheckPlugin: Validates data freshness based on timestamps.
|
|
11
|
+
- SchemaCheckPlugin: Validates column presence and data types.
|
|
12
|
+
|
|
13
|
+
Source Connectors:
|
|
14
|
+
- RestAPIPlugin: Generic REST API connector for fetching data.
|
|
15
|
+
|
|
16
|
+
Example:
|
|
17
|
+
To use these plugins in your Phlo project::
|
|
18
|
+
|
|
19
|
+
from phlo_core import NullCheckPlugin, RestAPIPlugin
|
|
20
|
+
|
|
21
|
+
null_check = NullCheckPlugin()
|
|
22
|
+
rest_source = RestAPIPlugin()
|
|
23
|
+
|
|
24
|
+
Attributes:
|
|
25
|
+
__version__: The version string for the phlo-core-plugins package.
|
|
26
|
+
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
from phlo_core.quality.freshness_check import FreshnessCheckPlugin
|
|
30
|
+
from phlo_core.quality.null_check import NullCheckPlugin
|
|
31
|
+
from phlo_core.quality.schema_check import SchemaCheckPlugin
|
|
32
|
+
from phlo_core.quality.uniqueness_check import UniquenessCheckPlugin
|
|
33
|
+
from phlo_core.sources.rest_api import RestAPIPlugin
|
|
34
|
+
|
|
35
|
+
__all__ = [
|
|
36
|
+
"NullCheckPlugin",
|
|
37
|
+
"UniquenessCheckPlugin",
|
|
38
|
+
"FreshnessCheckPlugin",
|
|
39
|
+
"SchemaCheckPlugin",
|
|
40
|
+
"RestAPIPlugin",
|
|
41
|
+
]
|
|
42
|
+
__version__ = "0.2.3"
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"""Quality check plugins bundled with Phlo.
|
|
2
|
+
|
|
3
|
+
This module provides a collection of quality check plugins that can be used
|
|
4
|
+
to validate data integrity, completeness, freshness, and schema conformance.
|
|
5
|
+
These plugins integrate with the Phlo quality framework and can be applied to
|
|
6
|
+
Pandera schemas or used directly in data pipelines.
|
|
7
|
+
|
|
8
|
+
Available Plugins:
|
|
9
|
+
- NullCheckPlugin: Checks for null values in specified columns with
|
|
10
|
+
configurable thresholds.
|
|
11
|
+
- UniquenessCheckPlugin: Validates that specified columns contain unique
|
|
12
|
+
values, with optional tolerance for duplicates.
|
|
13
|
+
- FreshnessCheckPlugin: Validates that timestamped data is within an
|
|
14
|
+
acceptable age range.
|
|
15
|
+
- SchemaCheckPlugin: Validates that data conforms to an expected schema
|
|
16
|
+
with correct columns and types.
|
|
17
|
+
|
|
18
|
+
Each plugin follows the QualityCheckPlugin interface and provides a
|
|
19
|
+
``create_check()`` method to instantiate the actual check object.
|
|
20
|
+
|
|
21
|
+
Example:
|
|
22
|
+
Import and use quality plugins::
|
|
23
|
+
|
|
24
|
+
from phlo_core.quality import NullCheckPlugin, FreshnessCheckPlugin
|
|
25
|
+
|
|
26
|
+
# Create a null check for required columns
|
|
27
|
+
null_plugin = NullCheckPlugin()
|
|
28
|
+
null_check = null_plugin.create_check(
|
|
29
|
+
columns=["id", "name", "email"],
|
|
30
|
+
allow_threshold=0.01 # Allow up to 1% nulls
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
# Create a freshness check for data age
|
|
34
|
+
freshness_plugin = FreshnessCheckPlugin()
|
|
35
|
+
freshness_check = freshness_plugin.create_check(
|
|
36
|
+
timestamp_column="created_at",
|
|
37
|
+
max_age_hours=24
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
from phlo_core.quality.freshness_check import FreshnessCheckPlugin
|
|
43
|
+
from phlo_core.quality.null_check import NullCheckPlugin
|
|
44
|
+
from phlo_core.quality.schema_check import SchemaCheckPlugin
|
|
45
|
+
from phlo_core.quality.uniqueness_check import UniquenessCheckPlugin
|
|
46
|
+
|
|
47
|
+
__all__ = [
|
|
48
|
+
"NullCheckPlugin",
|
|
49
|
+
"UniquenessCheckPlugin",
|
|
50
|
+
"FreshnessCheckPlugin",
|
|
51
|
+
"SchemaCheckPlugin",
|
|
52
|
+
]
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"""Freshness check plugin for validating data timeliness.
|
|
2
|
+
|
|
3
|
+
This module provides the FreshnessCheckPlugin, which enables validation of
|
|
4
|
+
data freshness based on timestamp columns. It helps ensure that data is
|
|
5
|
+
being updated within acceptable timeframes, critical for time-sensitive
|
|
6
|
+
analytics and operational dashboards.
|
|
7
|
+
|
|
8
|
+
Example:
|
|
9
|
+
Using the freshness check plugin::
|
|
10
|
+
|
|
11
|
+
from datetime import datetime, timedelta
|
|
12
|
+
from phlo_core.quality.freshness_check import FreshnessCheckPlugin
|
|
13
|
+
|
|
14
|
+
# Create the plugin
|
|
15
|
+
plugin = FreshnessCheckPlugin()
|
|
16
|
+
|
|
17
|
+
# Check that data is no more than 24 hours old
|
|
18
|
+
check = plugin.create_check(
|
|
19
|
+
timestamp_column="updated_at",
|
|
20
|
+
max_age_hours=24.0
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
# Check against a specific reference time
|
|
24
|
+
reference = datetime.now() - timedelta(hours=12)
|
|
25
|
+
check_with_ref = plugin.create_check(
|
|
26
|
+
timestamp_column="created_at",
|
|
27
|
+
max_age_hours=6.0,
|
|
28
|
+
reference_time=reference
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
"""
|
|
32
|
+
|
|
33
|
+
from datetime import datetime
|
|
34
|
+
from typing import Any
|
|
35
|
+
|
|
36
|
+
from phlo.plugins import PluginMetadata, QualityCheckPlugin
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class FreshnessCheckPlugin(QualityCheckPlugin[Any]):
|
|
40
|
+
"""Plugin for performing freshness validation on timestamped data.
|
|
41
|
+
|
|
42
|
+
This plugin creates freshness check instances that validate whether data
|
|
43
|
+
is within an acceptable age based on a timestamp column. It compares
|
|
44
|
+
the maximum timestamp value in the data against a reference time (defaults
|
|
45
|
+
to current time) to ensure data is fresh enough for use.
|
|
46
|
+
|
|
47
|
+
The freshness check is particularly useful for:
|
|
48
|
+
- Monitoring data pipeline latency
|
|
49
|
+
- Ensuring dashboards show current data
|
|
50
|
+
- Detecting stale data sources
|
|
51
|
+
- Validating ETL job success
|
|
52
|
+
|
|
53
|
+
Attributes:
|
|
54
|
+
metadata: PluginMetadata containing name, version, description,
|
|
55
|
+
author, and tags for this plugin.
|
|
56
|
+
|
|
57
|
+
Example:
|
|
58
|
+
Basic freshness check with current time as reference::
|
|
59
|
+
|
|
60
|
+
from phlo_core.quality.freshness_check import FreshnessCheckPlugin
|
|
61
|
+
|
|
62
|
+
plugin = FreshnessCheckPlugin()
|
|
63
|
+
check = plugin.create_check(
|
|
64
|
+
timestamp_column="event_time",
|
|
65
|
+
max_age_hours=2.0
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
Freshness check with custom reference time::
|
|
69
|
+
|
|
70
|
+
from datetime import datetime, timedelta
|
|
71
|
+
|
|
72
|
+
yesterday = datetime.now() - timedelta(days=1)
|
|
73
|
+
check = plugin.create_check(
|
|
74
|
+
timestamp_column="ingested_at",
|
|
75
|
+
max_age_hours=1.0,
|
|
76
|
+
reference_time=yesterday
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
"""
|
|
80
|
+
|
|
81
|
+
@property
|
|
82
|
+
def metadata(self) -> PluginMetadata:
|
|
83
|
+
"""Return plugin metadata for the freshness-check plugin.
|
|
84
|
+
|
|
85
|
+
Returns:
|
|
86
|
+
PluginMetadata: Metadata including name ("freshness_check"),
|
|
87
|
+
version ("0.1.0"), description ("Freshness checks for timestamped data"),
|
|
88
|
+
author ("Phlo Team"), and tags (["quality", "freshness"]).
|
|
89
|
+
|
|
90
|
+
"""
|
|
91
|
+
return PluginMetadata(
|
|
92
|
+
name="freshness_check",
|
|
93
|
+
version="0.1.0",
|
|
94
|
+
description="Freshness checks for timestamped data",
|
|
95
|
+
author="Phlo Team",
|
|
96
|
+
tags=["quality", "freshness"],
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
def create_check(
|
|
100
|
+
self,
|
|
101
|
+
timestamp_column: str,
|
|
102
|
+
max_age_hours: float,
|
|
103
|
+
reference_time: datetime | None = None,
|
|
104
|
+
) -> Any:
|
|
105
|
+
"""Create a freshness check instance.
|
|
106
|
+
|
|
107
|
+
Creates and returns a configured FreshnessCheck instance from phlo_pandera
|
|
108
|
+
that validates data freshness based on timestamps.
|
|
109
|
+
|
|
110
|
+
Args:
|
|
111
|
+
timestamp_column: Name of the timestamp column used for freshness
|
|
112
|
+
calculations. This column must exist in the data and contain
|
|
113
|
+
datetime values.
|
|
114
|
+
max_age_hours: Maximum allowed age of data in hours. If the data's
|
|
115
|
+
newest timestamp is older than this threshold relative to the
|
|
116
|
+
reference time, the check fails.
|
|
117
|
+
reference_time: Optional reference datetime for age evaluation.
|
|
118
|
+
If None, uses the current time. Useful for testing or when
|
|
119
|
+
validating against a specific point in time.
|
|
120
|
+
|
|
121
|
+
Returns:
|
|
122
|
+
Any: Configured FreshnessCheck instance ready to validate data.
|
|
123
|
+
The returned object can be used with Pandera schemas or called
|
|
124
|
+
directly with DataFrames.
|
|
125
|
+
|
|
126
|
+
Example:
|
|
127
|
+
Create a freshness check for recent data::
|
|
128
|
+
|
|
129
|
+
from phlo_core.quality.freshness_check import FreshnessCheckPlugin
|
|
130
|
+
|
|
131
|
+
plugin = FreshnessCheckPlugin()
|
|
132
|
+
check = plugin.create_check(
|
|
133
|
+
timestamp_column="created_at",
|
|
134
|
+
max_age_hours=24.0
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
Create a freshness check with custom reference::
|
|
138
|
+
|
|
139
|
+
from datetime import datetime, timedelta
|
|
140
|
+
|
|
141
|
+
check_time = datetime.now() - timedelta(hours=12)
|
|
142
|
+
check = plugin.create_check(
|
|
143
|
+
timestamp_column="updated_at",
|
|
144
|
+
max_age_hours=6.0,
|
|
145
|
+
reference_time=check_time
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
"""
|
|
149
|
+
from phlo_pandera.checks import FreshnessCheck
|
|
150
|
+
|
|
151
|
+
return FreshnessCheck(
|
|
152
|
+
timestamp_column=timestamp_column,
|
|
153
|
+
max_age_hours=max_age_hours,
|
|
154
|
+
reference_time=reference_time,
|
|
155
|
+
)
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"""Null check plugin for validating column completeness.
|
|
2
|
+
|
|
3
|
+
This module provides the NullCheckPlugin, which enables validation of
|
|
4
|
+
null value presence in specified columns. It helps ensure data completeness
|
|
5
|
+
by detecting missing values and enforcing thresholds for acceptable null rates.
|
|
6
|
+
|
|
7
|
+
Example:
|
|
8
|
+
Using the null check plugin::
|
|
9
|
+
|
|
10
|
+
from phlo_core.quality.null_check import NullCheckPlugin
|
|
11
|
+
|
|
12
|
+
# Create the plugin
|
|
13
|
+
plugin = NullCheckPlugin()
|
|
14
|
+
|
|
15
|
+
# Strict null check (no nulls allowed)
|
|
16
|
+
strict_check = plugin.create_check(
|
|
17
|
+
columns=["id", "email", "created_at"],
|
|
18
|
+
allow_threshold=0.0
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
# Lenient null check (allow up to 10% nulls in optional fields)
|
|
22
|
+
lenient_check = plugin.create_check(
|
|
23
|
+
columns=["middle_name", "phone_number"],
|
|
24
|
+
allow_threshold=0.10
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
# Mixed columns with different requirements
|
|
28
|
+
mixed_check = plugin.create_check(
|
|
29
|
+
columns=["required_field", "optional_field"],
|
|
30
|
+
allow_threshold=0.0 # Applies to all columns
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
from typing import Any
|
|
36
|
+
|
|
37
|
+
from phlo.plugins import PluginMetadata, QualityCheckPlugin
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class NullCheckPlugin(QualityCheckPlugin[Any]):
|
|
41
|
+
"""Plugin for performing null value validation on data columns.
|
|
42
|
+
|
|
43
|
+
This plugin creates null check instances that validate whether specified
|
|
44
|
+
columns contain null values within acceptable thresholds. It supports both
|
|
45
|
+
strict validation (no nulls allowed) and lenient validation (allows a
|
|
46
|
+
configurable percentage of nulls per column).
|
|
47
|
+
|
|
48
|
+
The null check is particularly useful for:
|
|
49
|
+
- Validating required field completeness
|
|
50
|
+
- Detecting data quality issues in ETL pipelines
|
|
51
|
+
- Enforcing data completeness SLAs
|
|
52
|
+
- Identifying sparse columns that may need attention
|
|
53
|
+
|
|
54
|
+
Attributes:
|
|
55
|
+
metadata: PluginMetadata containing name, version, description,
|
|
56
|
+
author, and tags for this plugin.
|
|
57
|
+
|
|
58
|
+
Example:
|
|
59
|
+
Strict null check for required fields::
|
|
60
|
+
|
|
61
|
+
from phlo_core.quality.null_check import NullCheckPlugin
|
|
62
|
+
|
|
63
|
+
plugin = NullCheckPlugin()
|
|
64
|
+
check = plugin.create_check(
|
|
65
|
+
columns=["user_id", "email", "registration_date"],
|
|
66
|
+
allow_threshold=0.0 # No nulls allowed
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
Lenient null check for optional fields::
|
|
70
|
+
|
|
71
|
+
check = plugin.create_check(
|
|
72
|
+
columns=["phone", "address_line_2"],
|
|
73
|
+
allow_threshold=0.20 # Allow up to 20% nulls
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
Using with Pandera schema::
|
|
77
|
+
|
|
78
|
+
import pandera as pa
|
|
79
|
+
|
|
80
|
+
schema = pa.DataFrameSchema(
|
|
81
|
+
columns={
|
|
82
|
+
"id": pa.Column(pa.Int64, checks=check),
|
|
83
|
+
"name": pa.Column(pa.String, checks=check),
|
|
84
|
+
}
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
"""
|
|
88
|
+
|
|
89
|
+
@property
|
|
90
|
+
def metadata(self) -> PluginMetadata:
|
|
91
|
+
"""Return plugin metadata for the null-check quality plugin.
|
|
92
|
+
|
|
93
|
+
Returns:
|
|
94
|
+
PluginMetadata: Metadata including name ("null_check"),
|
|
95
|
+
version ("0.1.0"), description ("Null checks for column completeness"),
|
|
96
|
+
author ("Phlo Team"), and tags (["quality", "nulls"]).
|
|
97
|
+
|
|
98
|
+
"""
|
|
99
|
+
return PluginMetadata(
|
|
100
|
+
name="null_check",
|
|
101
|
+
version="0.1.0",
|
|
102
|
+
description="Null checks for column completeness",
|
|
103
|
+
author="Phlo Team",
|
|
104
|
+
tags=["quality", "nulls"],
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
def create_check(self, columns: list[str], allow_threshold: float = 0.0) -> Any:
|
|
108
|
+
"""Create a null check instance.
|
|
109
|
+
|
|
110
|
+
Creates and returns a configured NullCheck instance from phlo_pandera
|
|
111
|
+
that validates null value presence in specified columns.
|
|
112
|
+
|
|
113
|
+
Args:
|
|
114
|
+
columns: List of column names to validate for null values.
|
|
115
|
+
Each column in the list will be checked individually for
|
|
116
|
+
null value presence against the threshold.
|
|
117
|
+
allow_threshold: Maximum allowed null ratio per column as a float
|
|
118
|
+
between 0.0 and 1.0. Defaults to 0.0 (strict validation, no
|
|
119
|
+
nulls allowed). A threshold of 0.10 allows up to 10% of values
|
|
120
|
+
in each column to be null.
|
|
121
|
+
|
|
122
|
+
Returns:
|
|
123
|
+
Any: Configured NullCheck instance ready to validate data.
|
|
124
|
+
The returned object can be used with Pandera schemas or called
|
|
125
|
+
directly with DataFrames.
|
|
126
|
+
|
|
127
|
+
Raises:
|
|
128
|
+
ValueError: If allow_threshold is not between 0.0 and 1.0.
|
|
129
|
+
|
|
130
|
+
Example:
|
|
131
|
+
Create a strict null check::
|
|
132
|
+
|
|
133
|
+
from phlo_core.quality.null_check import NullCheckPlugin
|
|
134
|
+
|
|
135
|
+
plugin = NullCheckPlugin()
|
|
136
|
+
check = plugin.create_check(
|
|
137
|
+
columns=["customer_id", "order_date"],
|
|
138
|
+
allow_threshold=0.0
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
Create a lenient null check for optional fields::
|
|
142
|
+
|
|
143
|
+
check = plugin.create_check(
|
|
144
|
+
columns=["middle_name", "secondary_email"],
|
|
145
|
+
allow_threshold=0.15 # 15% tolerance
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
Apply to DataFrame directly::
|
|
149
|
+
|
|
150
|
+
result = check.validate(df)
|
|
151
|
+
|
|
152
|
+
"""
|
|
153
|
+
from phlo_pandera.checks import NullCheck
|
|
154
|
+
|
|
155
|
+
return NullCheck(columns=columns, allow_threshold=allow_threshold)
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"""Schema check plugin for validating data structure and types.
|
|
2
|
+
|
|
3
|
+
This module provides the SchemaCheckPlugin, which enables validation of
|
|
4
|
+
data against expected schemas including column presence and data type checks.
|
|
5
|
+
It integrates with Pandera to provide comprehensive schema validation
|
|
6
|
+
capabilities.
|
|
7
|
+
|
|
8
|
+
Example:
|
|
9
|
+
Using the schema check plugin with a Pandera schema::
|
|
10
|
+
|
|
11
|
+
import pandera as pa
|
|
12
|
+
from phlo_core.quality.schema_check import SchemaCheckPlugin
|
|
13
|
+
|
|
14
|
+
# Define expected schema
|
|
15
|
+
schema = pa.DataFrameSchema({
|
|
16
|
+
"id": pa.Column(pa.Int64, nullable=False),
|
|
17
|
+
"name": pa.Column(pa.String, nullable=False),
|
|
18
|
+
"email": pa.Column(pa.String, nullable=False),
|
|
19
|
+
"created_at": pa.Column(pa.DateTime, nullable=False),
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
# Create the check
|
|
23
|
+
plugin = SchemaCheckPlugin()
|
|
24
|
+
check = plugin.create_check(schema=schema, lazy=True)
|
|
25
|
+
|
|
26
|
+
# Apply to data
|
|
27
|
+
validated_df = check.validate(dataframe)
|
|
28
|
+
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
from typing import Any
|
|
32
|
+
|
|
33
|
+
from phlo.plugins import PluginMetadata, QualityCheckPlugin
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class SchemaCheckPlugin(QualityCheckPlugin[Any]):
|
|
37
|
+
"""Plugin for performing schema validation on data.
|
|
38
|
+
|
|
39
|
+
This plugin creates schema check instances that validate data against
|
|
40
|
+
expected column structures and data types. It supports both strict
|
|
41
|
+
validation (fails immediately) and lazy validation (collects all errors).
|
|
42
|
+
|
|
43
|
+
The schema check is particularly useful for:
|
|
44
|
+
- Validating column presence in incoming data
|
|
45
|
+
- Ensuring correct data types before processing
|
|
46
|
+
- Detecting schema drift in data pipelines
|
|
47
|
+
- Enforcing contracts between data producers and consumers
|
|
48
|
+
|
|
49
|
+
Attributes:
|
|
50
|
+
metadata: PluginMetadata containing name, version, description,
|
|
51
|
+
author, and tags for this plugin.
|
|
52
|
+
|
|
53
|
+
Example:
|
|
54
|
+
Create and use a schema check::
|
|
55
|
+
|
|
56
|
+
from phlo_core.quality.schema_check import SchemaCheckPlugin
|
|
57
|
+
import pandera as pa
|
|
58
|
+
|
|
59
|
+
plugin = SchemaCheckPlugin()
|
|
60
|
+
schema = pa.DataFrameSchema({
|
|
61
|
+
"user_id": pa.Column(pa.Int64),
|
|
62
|
+
"username": pa.Column(pa.String)
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
check = plugin.create_check(schema=schema, lazy=True)
|
|
66
|
+
result = check.validate(df)
|
|
67
|
+
|
|
68
|
+
"""
|
|
69
|
+
|
|
70
|
+
@property
|
|
71
|
+
def metadata(self) -> PluginMetadata:
|
|
72
|
+
"""Return plugin metadata for the schema-check plugin.
|
|
73
|
+
|
|
74
|
+
Returns:
|
|
75
|
+
PluginMetadata: Metadata including name ("schema_check"),
|
|
76
|
+
version ("0.1.0"), description, author ("Phlo Team"),
|
|
77
|
+
and tags (["quality", "schema"]).
|
|
78
|
+
|
|
79
|
+
"""
|
|
80
|
+
return PluginMetadata(
|
|
81
|
+
name="schema_check",
|
|
82
|
+
version="0.1.0",
|
|
83
|
+
description="Schema validation for expected columns and types",
|
|
84
|
+
author="Phlo Team",
|
|
85
|
+
tags=["quality", "schema"],
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
def create_check(self, schema: Any, lazy: bool = True) -> Any:
|
|
89
|
+
"""Create a schema check instance.
|
|
90
|
+
|
|
91
|
+
Creates and returns a configured SchemaCheck instance from phlo_pandera
|
|
92
|
+
that validates data against the provided schema.
|
|
93
|
+
|
|
94
|
+
Args:
|
|
95
|
+
schema: Expected schema object for validation. This is typically
|
|
96
|
+
a Pandera DataFrameSchema or similar schema definition that
|
|
97
|
+
defines expected columns, types, and constraints.
|
|
98
|
+
lazy: Whether to collect all validation errors before failing.
|
|
99
|
+
When True, all validation errors are collected and reported
|
|
100
|
+
together. When False, validation fails on the first error.
|
|
101
|
+
Defaults to True.
|
|
102
|
+
|
|
103
|
+
Returns:
|
|
104
|
+
Any: Configured SchemaCheck instance ready to validate data.
|
|
105
|
+
The returned object has a ``validate()`` method that accepts
|
|
106
|
+
a DataFrame and returns the validated data or raises a
|
|
107
|
+
SchemaError if validation fails.
|
|
108
|
+
|
|
109
|
+
Example:
|
|
110
|
+
Create a schema check with lazy validation::
|
|
111
|
+
|
|
112
|
+
from phlo_core.quality.schema_check import SchemaCheckPlugin
|
|
113
|
+
import pandera as pa
|
|
114
|
+
|
|
115
|
+
plugin = SchemaCheckPlugin()
|
|
116
|
+
schema = pa.DataFrameSchema({
|
|
117
|
+
"id": pa.Column(pa.Int64, nullable=False),
|
|
118
|
+
"value": pa.Column(pa.Float, nullable=True)
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
check = plugin.create_check(schema=schema, lazy=True)
|
|
122
|
+
|
|
123
|
+
Create a schema check with strict validation::
|
|
124
|
+
|
|
125
|
+
strict_check = plugin.create_check(schema=schema, lazy=False)
|
|
126
|
+
|
|
127
|
+
"""
|
|
128
|
+
from phlo_pandera.checks_extra import SchemaCheck
|
|
129
|
+
|
|
130
|
+
return SchemaCheck(schema=schema, lazy=lazy)
|