Plans, rule engine and data
Plans, rule engine and data¶
The main classes to create and run plans. They are available from the top level etlrules package,
e.g. from etlrules import Plan, RuleEngine, RuleData.
Plan ¶
A plan to manipulate one or multiple dataframes with a set of rules.
A plan is a blueprint on how to extract one or more dataframes from various sources (e.g. files or other data sources), how to transform those dataframes by adding calculated columns, joining different dataframe, aggregating, sorting, etc. and ultimately how to load that into a data store (files or other data stores).
A plan can operate in two modes: pipeline or graph. A pipeline graph is a simple type of plan where each rule take its input from the previous rule's output. A graph plan is more complex as it allows rules to produce named outputs which can then be used by other rules. This ultimately builds a dag (directed acyclic graph) of rule dependencies. A graph allows branching and joining back allowing complex logic. Rules are executed in the order of dependency and not in the order they are added to the plan. By comparison, pipelines implement a single input/single output mode where rules are executed in the order they are added to the plan.
Pipeline example:
1 2 3 4 | |
Graph example:
1 2 3 4 | |
Note
Rules that are used in graph mode should take a named_input and produce a named_output. Rules that use the pipeline mode must not used named inputs/outputs. The two type of rules cannot be used in the same plan as that leads to ambiguity.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
Optional[Literal['pipeline', 'graph']]
|
One of pipeline or graph, the type of the graph. Optional. In pipeline mode, rules don't use named inputs/outputs and they are run in the same order they are added to the plan, with each rule taking the input from the previous rule. In graph mode, rules use named inputs/outputs which create a directed acyclical graph of dependency. The rules are run in the order of dependency. When not specified, it is inferred from the first rule in the plan. |
None
|
name
|
Optional[str]
|
A name for the plan. Optional. |
None
|
description
|
Optional[str]
|
An optional documentation for the plan. This can include what the plan does, its purpose and detailed information about how it works. |
None
|
context
|
Optional[Mapping[str, Union[str, int, float, bool]]]
|
An optional key-value mapping which can be used in rules via string substitutions. It can be used as arguments into the plan to tweak the running of the plan by providing different values for certain arguments with each run. The types of the values can be: strings, int, float, boolean (True or False). |
None
|
strict
|
Optional[bool]
|
A hint about how the plan should be executed. When None, then the plan has no hint to provide and its the caller deciding whether to run it in a strict mode or not. |
None
|
Raises:
| Type | Description |
|---|---|
InvalidPlanError
|
if pipeline mode rules are mixed with graph mode rules |
get_mode ¶
get_mode() -> Optional[Literal['pipeline', 'graph']]
Return the mode (pipeline or graph) of the plan.
add_rule ¶
add_rule(rule: BaseRule) -> None
Add a new rule to the plan.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rule
|
BaseRule
|
A rule instance to add to the plan |
required |
Raises:
| Type | Description |
|---|---|
InvalidPlanError
|
if the rules are mixed (pipeline vs. graph - ie. mixing use of named inputs/outputs and not using them) |
get_rule ¶
get_rule(idx: int) -> BaseRule
Return the rule at a certain index as per order of addition to the plan.
is_empty ¶
is_empty() -> bool
Return True if the plan has no rules, False otherwise.
Returns:
| Type | Description |
|---|---|
bool
|
A boolean to indicate if the plan is empty. |
to_dict ¶
to_dict() -> dict
Serialize the plan to a dict.
Returns:
| Type | Description |
|---|---|
dict
|
A dictionary with the plan representation. |
from_dict
classmethod
¶
from_dict(dct: dict, backend: str, additional_packages: Optional[Sequence[str]] = None) -> Plan
Creates a plan 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) -> Plan
Creates a plan 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 |
|---|---|
Plan
|
A new instance of a Plan. |
PlanMode ¶
RuleEngine ¶
Run a set of extract/transform/load rules over a dataframe.
Takes in a plan with the definition of the extract/transform/load rules and it runs it over a RuleData instance. The RuleData instance can be optionally pre-populated with a input dataframe (in pipeline mode) or a sequence of named inputs (named dataframes).
The plan can have rules to extract data (ie add more dataframes to the RuleData). It can have transform rules which will transform the existing dataframes (either in-place or produce new named dataframes). It can also have rules to load data into external systems, e.g. files, databases, API connections, etc.
At the end of a plan run, the RuleData instance passed in will contain the results of the run (ie new dataframes/transformed dataframes) which can be inspected/operated on outside of the rule engine.
RuleData ¶
load_plan ¶
load_plan(plan_file: str, backend: str) -> Plan
Load a plan from a yaml file.
Basic usage:
1 2 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plan_file
|
str
|
A path to a yaml file with the plan definition |
required |
backend
|
str
|
One of the supported backends: pandas, polars, dask or pyarrow |
required |
Returns:
| Type | Description |
|---|---|
Plan
|
A Plan instance deserialized from the yaml file. |
run_plan ¶
run_plan(plan_file: str, backend: str, context: Optional[Mapping[str, Union[str, int, float, bool]]] = None) -> RuleData
Runs a plan from a yaml file with a given backend.
The backend referers to the underlying dataframe library used to run the plan.
Basic usage:
1 2 3 4 5 | |
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
plan_file
|
str
|
A path to a yaml file with the plan definition |
required |
backend
|
str
|
One of the supported backends |
required |
context
|
Optional[Mapping[str, Union[str, int, float, bool]]]
|
An optional mapping of values which override the values in the plan's context. |
None
|
Note
The supported backends: pandas, polars, dask, pyarrow
Note
Unlike the command line runner, run_plan doesn't read the command line arguments (sys.argv).
Returns:
| Type | Description |
|---|---|
RuleData
|
A RuleData instance which contains the result dataframe(s). |