plugins.h_narhwals.NarwhalsAdapter
plugins.h_narhwals.NarwhalsAdapter
Provides a convenience wrapper for the Narwhals library; use the Narwhals decorator underneath. Must have Narwhals installed to use it:
pip install “apache-hamilton[narwhals]”
class hamilton.plugins.h_narwhals.NarwhalsAdapter[source]
Adapter to make it simpler to use narwhals with Hamilton.
from hamilton import base, driver
from hamilton.plugins import h_narwhals
import example
# pandas
dr = (
driver.Builder()
.with_config({"load": "pandas"})
.with_modules(example)
.with_adapters(
h_narwhals.NarwhalsAdapter(),
h_narwhals.NarwhalsDataFrameResultBuilder(
base.PandasDataFrameResult()
),
)
.build()
)
result = dr.execute(
[example.group_by_mean, example.example1],
inputs={"col_name": "a"}
)do_node_execute(*, run_id: str, node_: Node, kwargs: dict[str, Any], task_id: str | None = None) → Any
Method that is called to implement node execution. This can replace the execution of a node with something all together, augment it, or delegate it.
Parameters:
- run_id – ID of the run, unique in scope of the driver.
- node – Node that is being executed
- kwargs – Keyword arguments that are being passed into the node
- task_id – ID of the task, defaults to None if not in a task setting
run_to_execute_node(*, node_name: str, node_tags: dict[str, Any], node_callable: Any, node_kwargs: dict[str, Any], task_id: str | None, **future_kwargs: Any) → Any[source]
This method is responsible for executing the node and returning the result.
It uses nw_kwargs from the node tags to know if any special flags should be passed to the narwhals decorator function.
Parameters:
- node_name – Name of the node.
- node_tags – Tags of the node.
- node_callable – Callable of the node.
- node_kwargs – Keyword arguments to pass to the node.
- task_id – The ID of the task, none if not in a task-based environment
- future_kwargs – Additional keyword arguments – this is kept for backwards compatibility
Returns:
The result of the node execution – up to you to return this.
plugins.h_narhwals.NarwhalsDataFrameResultBuilder
Result builder to be used with the NarwhalsAdapter. Must have Narwhals installed to use it:
pip install “apache-hamilton[narwhals]”
class hamilton.plugins.h_narwhals.NarwhalsDataFrameResultBuilder(result_builder: ResultBuilder | LegacyResultMixin)[source]
Builds the result. It unwraps the narwhals parts of it and delegates to the passed in result builder.
from hamilton import base, driver
from hamilton.plugins import h_narwhals, h_polars
import example
# polars
dr = (
driver.Builder()
.with_config({"load": "polars"})
.with_modules(example)
.with_adapters(
h_narwhals.NarwhalsAdapter(),
h_narwhals.NarwhalsDataFrameResultBuilder(
h_polars.PolarsDataFrameResult()
),
)
.build()
)
result = dr.execute(
["group_by_mean", "example1"],
inputs={"col_name": "a"}
)__init__(result_builder: ResultBuilder | LegacyResultMixin)[source]
build_result(**outputs: Any) → Any[source]
Given a set of outputs, build the result.
Parameters:
outputs – the outputs from the execution of the graph.
Returns:
the result of the execution of the graph.
do_build_result(outputs: dict[str, Any]) → Any
Implements the do_build_result method from the BaseDoBuildResult class. This is kept from the user as the public-facing API is build_result, allowing us to change the API/implementation of the internal set of hooks
input_types() → list[type[type]]
Gives the applicable types to this result builder. This is optional for backwards compatibility, but is recommended.
Returns:
A list of types that this can apply to.
output_type() → type[source]
Returns the output type of this result builder the type that this creates
评论
登录后参与评论
KnowForge