Skip to content

Commit

Permalink
Task 33, adding validatio id
Browse files Browse the repository at this point in the history
  • Loading branch information
Nargis Sultani committed Sep 12, 2023
1 parent ade2867 commit 1d729d8
Show file tree
Hide file tree
Showing 3 changed files with 212 additions and 40 deletions.
26 changes: 13 additions & 13 deletions src/validator/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
name="Just a Warning"
)
error_check_implied = SBLCheck(lambda: Truename="Error Check")
error_check_implied = SBLCheck(lambda: True name="Error Check")
error_check_explicit = SBLCheck(
lambda: True,
Expand All @@ -32,31 +32,37 @@


class SBLCheck(Check):
"""A custom Pandera.Check subclasss that requires a `name` be
"""A custom Pandera.Check subclasss that requires a `name` and an `id` be
specified. Additionally, an attribute named `warning` is added to
the class to enable distinction between warnings and errors. The
default value of warning is `False` which corresponds to an error.
Don't use this class directly. Make use of the SBLErrorCheck and
SBLWarningCheck subclasses below."""

def __init__(self, check_fn: Callable, warning=False, *args, **kwargs):
"""Custom init method that verifies the presence of `name` in
def __init__(
self, check_fn: Callable, id: str = None, warning=False, *args, **kwargs
):
"""Custom init method that verifies the presence of `name` and `id` in
kwargs creates a custom class attribute called `warning`. All
other initializaiton is handled by the parent Check class.
Args:
check_fn (Callable): A function which evaluates the validity
of the column(s) being tested.
id (str, required): Each check mut have an id.
warning (bool, optional): Boolean specifying whether to
treat the check as a warning rather than an error.
Raises:
ValueError: Raised if `name` not supplied in kwargs.
ValueError: Raised if `name` not supplied in kwargs and if id is not
supplied or None.
"""

if "name" not in kwargs:
raise ValueError("Each check must be assigned a `name`.")
self.id = id

if "name" not in kwargs or id is None:
raise ValueError("Each check must be assigned a `name` and an `id`.")

# if warning==False treat check as an error check
self.warning = warning
Expand All @@ -68,9 +74,3 @@ def get_backend(cls, check_obj: Any) -> Type[BaseCheckBackend]:
"""Assume Pandas DataFrame and return PandasCheckBackend"""
return PandasCheckBackend


if __name__ == "__main__":
warning_check = SBLCheck(lambda: True, warning=True, name="Just a Warning")

error_check_implied = SBLCheck(lambda: True, name="Error Check")
error_check_explicit = SBLCheck(lambda: True, warning=False, name="Also an Error")
5 changes: 4 additions & 1 deletion src/validator/create_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,22 @@ def print_schema_errors(errors: SchemaErrors, phase: str):
# Name of the column in the dataframe being checked
schema_error = error["error"]
column_name = schema_error.schema.name
check_id = "n/a"

# built in checks such as unique=True are different than custom
# checks unfortunately so the name needs to be accessed differently
try:
check_name = schema_error.check.name
check_id = schema_error.check.id
# This will either be a boolean series or a single bool
check_output = schema_error.check_output
except AttributeError:
check_name = schema_error.check
# this is just a string that we'd need to parse manually
check_output = schema_error.args[0]

print(f"{phase} Validation `{check_name}` failed for column `{column_name}`")
f"{phase} Validation `{check_name}` with id: `{check_id}` \
failed for column `{column_name}`"
print(check_output)
print("")

Expand Down
Loading

0 comments on commit 1d729d8

Please sign in to comment.