Custom rules
Base classes for custom rules¶
Derive your own rules from these base classes. Custom rules can be used in plans together with the
built-in rules, including plans loaded from yaml (see the additional_packages argument of
Plan.from_yaml).
BaseRule ¶
The base class for all rules.
Derive your custom rules from BaseRule in order to use them in a plan. Implement the following methods as needed: apply: mandatory, it implements the functionality of the rule has_input: defaults to True, override and return False if your rule reads data into the plan and therefore has no other dataframe input has_output: defaults to True, override and return False if your rule writes data to a persistent repository and therefore has no dataframe output get_all_named_inputs: override to return the named inputs (if any) as strings get_all_named_outputs: override in case of multiple named outputs and return them as strings
named_output (Optional[str]): Give the output of this rule a name so it can be used by another rule as a named input. Optional. When not set, the result of this rule will be available as the main output. When set to a name (string), the result will be available as that named output. name (Optional[str]): Give the rule a name. Optional. Named rules are more descriptive as to what they're trying to do/the intent. description (Optional[str]): Describe in detail what the rules does, how it does it. Optional. Together with the name, the description acts as the documentation of the rule. strict (bool): When set to True, the rule does a stricter valiation. Default: True
Note
Add any class data members to the following list/tuples if needed: EXCLUDE_FROM_COMPARE: Used in implementing equality between rules. Equality is mostly used in tests. By default, equality looks at all data members in the class' dict. You can exclude calculated or transient data members which should be excluded from equality. Alternatively, you can implement eq in your own class and not rely on the eq implementation in the base class. EXCLUDE_FROM_SERIALIZE: Used to exclude data members from the serialization to dict and yaml. The serialization is implemented generically in the base class to serialize all data members in the class' dict which do not start with an underscore. See the note on serialization below.
Note
When implementing serialization, the arguments into your class should be saved as they are in data members with the same name as the arguments. This is because the de-serialization passes those as args into the init. As such, make sure to use the same names and to exclude data members which are not in the init from serialization by adding them to EXCLUDE_FROM_SERIALIZE.
get_name ¶
get_name() -> Optional[str]
Returns the name of the rule.
The name is optional and it can be None.
The name of the rule should indicate what the rule does and not how it's implemented. The names should read like documentation. As such, names like
Remove duplicate first names from the list of addresses Only keep the names starting with A
are preferable names to:
DedupeRule ProjectRule
Names are not used internally for anything other than your own (and your end users') documentation, so use what makes sense.
get_description ¶
get_description() -> Optional[str]
A long description of what the rule does, why and optionally how it does it.
The description is optional and it can be None.
Similar to name, this long description acts as documentation for you and your users. It's particularly useful if your rule is serialized in a readable format like yaml and your users either do not have access to the documentation or they are not technical.
Unlike the name, which should generally be a single line headline, the description is a long, multi-line description of the rule: the what, why, how of the rule.
has_input ¶
has_input() -> bool
Returns True if the rule needs a dataframe input to operate on, False otherwise.
By default, it returns True. It should be overriden to return False for those rules which read data into the plan. For example, reading a csv file or reading a table from the DB. These are operation which do not need an input dataframe to operate on as they are sourcing data.
has_output ¶
has_output() -> bool
Returns True if the rule produces a dataframe, False otherwise.
By default, it returns True. It should be overriden to return False for those rules which write data out of the plan. For example, writing a file or data into a database. These are operations which do not produce an output dataframe into the plan as they are writing data outside the plan.
get_all_named_inputs ¶
get_all_named_inputs() -> Generator[str, None, None]
Yields all the named inputs of this rule (as strings).
By default, it yields nothing as this base rule doesn't store information about inputs. Some rules take no input, some take one or more inputs. Yield accordingly when you override.
get_all_named_outputs ¶
get_all_named_outputs() -> Generator[str, None, None]
Yields all the named outputs of this rule (as strings).
By default, it yields the single named_output passed into this rule as an argument. Some rules produce no output, some produce multiple outputs. Yield accordingly when you override.
apply ¶
apply(data: RuleData) -> None
Applies the main rule logic to the input data.
This is the main rule that applies a rule logic to an input data. The input data is an instance of RuleData which can store a single, unnamed dataframe (in pipeline mode) or one or many named dataframes (in graph mode). The rule extracts the data it needs from the data, applies its main logic and updates the same instance of RuleData with the output, if any.
This method doesn't do anything in the base class other than asserting that the data passed in is an instance of RuleData. Override this when you derive from BaseRule and implement the logic of your rule.
to_dict ¶
to_dict() -> dict
Serializes this rule to a python dictionary.
This is a generic implementation that should work for all derived classes and therefore you shouldn't need to override, although you can do so.
Because it aims to be generic and work correctly for all the derived classes, a few assumptions are made and must be respected when you implement your own rules derived from BaseRule.
The class will serialize all the data attributes of a class which do not start with underscore and are not explicitly listed in the EXCLUDE_FROM_SERIALIZE static member of the class. As such, to exclude any of your internal data attributes, either named them so they start with an underscore or add them explicitly to EXCLUDE_FROM_SERIALIZE.
The serialize will look into a classes dict and therefore the class must have a dict.
For the de-serialization to work generically, the name of the attributes must match the names of the arguments in the init. This is quite an important and restrictive constraint which is needed to avoid forcing every rule to implement a serialize/deserialize.
Note
Use the same name for attributes on self as the respective arguments in init.
from_dict
classmethod
¶
from_dict(dct: dict, backend: str, additional_packages: Optional[Sequence[str]] = None) -> BaseRule
Creates a rule instance from a python dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dct
|
dict
|
A dictionary to create the plan from |
required |
backend
|
str
|
One of the supported backends (ie pandas) |
required |
additional_packages
|
Optional[Sequence[str]]
|
Optional list of other packages to look for rules in |
None
|
from_yaml
classmethod
¶
from_yaml(yml: str, backend: str, additional_packages: Optional[Sequence[str]] = None) -> BaseRule
Creates a rule instance from a yaml definition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
yml
|
str
|
The yaml string to create the plan from |
required |
backend
|
str
|
A supported backend (ie pandas) |
required |
additional_packages
|
Optional[Sequence[str]]
|
Optional list of other packages to look for rules in |
None
|
Returns:
| Type | Description |
|---|---|
BaseRule
|
A new instance of a rule. |
UnaryOpBaseRule ¶
Base class for unary operation rules (ie operations taking a single data frame as input).
BinaryOpBaseRule ¶
Base class for binary operation rules (ie operations taking two data frames as input).