You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As a user, it would be easier to create my own custom constraints if the only thing I had to supply was the functions for transforming and validating.
Expected behavior
Add a factory class method called create_custom_constraint to the constraints module.
This method should take in the following parameters:
is_valid_fn: required. A function with the following signature is_valid_fn(column_names, data, **kwargs). Must return a series with the rows that are valid for the constraint set to True.
transform_fn: optional. A function with the following signature - transform_fn(column_names, data, **kwargs)
reverse_transform_fn: optional. A function with the following signature - reverse_transform_fn(column_names, data, **kwargs)
The method should return a new constraint class object (not instance) with the is_valid, transform and reverse_transform methods set to what the user provides. The is_valid must be provided, and if either of the two tranform methods are provided, then they both must be.
Delete the current CustomConstraint class
Additional context
Use the following prototype as inspiration:
defcreate_custom_constraint(transform_fn=None, reverse_transform_fn=None,
is_valid_fn=None):
# User must provide a validity functionifis_valid_fnisNone:
raiseException("Missing required parameter 'is_valid'.")
# Transform & reverse are optional but should be provided together or not at alliftransform_fnisNoneandreverse_transform_fnisnotNone:
raiseException('No transform is given for the reverse transform.')
iftransform_fnisnotNoneandreverse_transform_fnisNone:
raiseException('No reverse transform is given for the transform.')
# Create a Class that binds to the provided functionsclassCustomConstraint:
# can keep track of class-level attributesTYPE='CUSTOM_CONSTRAINT'# this lets us know how to handle itdef__init__(self, column_names, **kwargs):
self.column_names=column_namesself.kwargs=kwargsdeftransform(self, data):
iftransform_fnisNone:
raiseException('Transform is not defined for this custom constraint.')
result=transform_fn(self.column_names, data, **self.kwargs)
ifdata.shape[0] !=result.shape[0]:
raiseException('Transformation did not produce the same number of rows as the original')
returnresultdefreverse_transform(self, data):
ifreverse_transform_fnisNone:
raiseException('Reverse transform is not defined for this custom constraint.')
result=reverse_transform_fn(self.column_names, data, **self.kwargs)
ifdata.shape[0] !=result.shape[0]:
raiseException('Reverse transformation did not produce the same number of rows as the original')
returnresultdefis_valid(self, data):
result=is_valid_fn(self.column_names, data, **self.kwargs)
iflen(result) !=data.shape[0]:
raiseException('The validity check should produce exactly 1 True/False value for every row in the data')
returnresult# return the class to the user returnCustomConstraint
The text was updated successfully, but these errors were encountered:
Problem Description
As a user, it would be easier to create my own custom constraints if the only thing I had to supply was the functions for transforming and validating.
Expected behavior
create_custom_constraint
to theconstraints
module.is_valid_fn
: required. A function with the following signatureis_valid_fn(column_names, data, **kwargs)
. Must return a series with the rows that are valid for the constraint set toTrue
.transform_fn
: optional. A function with the following signature -transform_fn(column_names, data, **kwargs)
reverse_transform_fn
: optional. A function with the following signature -reverse_transform_fn(column_names, data, **kwargs)
is_valid
,transform
andreverse_transform
methods set to what the user provides. Theis_valid
must be provided, and if either of the two tranform methods are provided, then they both must be.CustomConstraint
classAdditional context
The text was updated successfully, but these errors were encountered: