kedro.pipeline.node.Node

class kedro.pipeline.node.Node(func, inputs, outputs, *, name=None, tags=None, decorators=None)[source]

Bases: object

Node is an auxiliary class facilitating the operations required to run user-provided functions as part of Kedro pipelines.

Attributes

Node.inputs Return node inputs as a list, in the order required to bind them properly to the node’s function.
Node.name Node’s name.
Node.outputs Return node outputs as a list preserving the original order
Node.short_name Node’s name.
Node.tags Return the tags assigned to the node.

Methods

Node.__init__(func, inputs, outputs, *[, …]) Create a node in the pipeline by providing a function to be called along with variable names for inputs and/or outputs.
Node.decorate(*decorators) Create a new Node by applying the provided decorators to the underlying function.
Node.run([inputs]) Run this node using the provided inputs and return its results in a dictionary.
Node.tag(tags) Create a new Node which is an exact copy of the current one,
__init__(func, inputs, outputs, *, name=None, tags=None, decorators=None)[source]

Create a node in the pipeline by providing a function to be called along with variable names for inputs and/or outputs.

Parameters:
  • func (Callable) – A function that corresponds to the node logic. The function should have at least one input or output.
  • inputs (Union[None, str, List[str], Dict[str, str]]) – The name or the list of the names of variables used as inputs to the function. The number of names should match the number of arguments in the definition of the provided function. When Dict[str, str] is provided, variable names will be mapped to function argument names.
  • outputs (Union[None, str, List[str], Dict[str, str]]) – The name or the list of the names of variables used as outputs to the function. The number of names should match the number of outputs returned by the provided function. When Dict[str, str] is provided, variable names will be mapped to the named outputs the function returns.
  • name (Optional[str]) – Optional node name to be used when displaying the node in logs or any other visualisations.
  • tags (Union[str, Iterable[str], None]) – Optional set of tags to be applied to the node.
  • decorators (Optional[Iterable[Callable]]) – Optional list of decorators to be applied to the node.
Raises:

ValueError – Raised in the following cases: a) When the provided arguments do not conform to the format suggested by the type hint of the argument. b) When the node produces multiple outputs with the same name. c) An input has the same name as an output.

decorate(*decorators)[source]

Create a new Node by applying the provided decorators to the underlying function. If no decorators are passed, it will return a new Node object, but with no changes to the function.

Parameters:decorators (Callable) – Decorators to be applied on the node function. Decorators will be applied from right to left.
Return type:Node
Returns:A new Node object with the decorators applied to the function.

Example:

from functools import wraps


def apply_f(func: Callable) -> Callable:
    @wraps(func)
    def with_f(*args, **kwargs):
        args = ["f({})".format(a) for a in args]
        return func(*args, **kwargs)
    return with_f


def apply_g(func: Callable) -> Callable:
    @wraps(func)
    def with_g(*args, **kwargs):
        args = ["g({})".format(a) for a in args]
        return func(*args, **kwargs)
    return with_g


def apply_h(func: Callable) -> Callable:
    @wraps(func)
    def with_h(*args, **kwargs):
        args = ["h({})".format(a) for a in args]
        return func(*args, **kwargs)
    return with_h


def apply_fg(func: Callable) -> Callable:
    @wraps(func)
    def with_fg(*args, **kwargs):
        args = ["fg({})".format(a) for a in args]
        return func(*args, **kwargs)
    return with_fg


def identity(value):
    return value


# using it as a regular python decorator
@apply_f
def decorated_identity(value):
    return value


# wrapping the node function
old_node = node(apply_g(decorated_identity), 'input', 'output',
                name='node')
# using the .decorate() method to apply multiple decorators
new_node = old_node.decorate(apply_h, apply_fg)
result = new_node.run(dict(input=1))

assert old_node.name == new_node.name
assert "output" in result
assert result['output'] == "f(g(fg(h(1))))"
inputs

Return node inputs as a list, in the order required to bind them properly to the node’s function. If the node’s function contains kwargs, then kwarg inputs are sorted alphabetically (for python 3.5 deterministic behavior).

Return type:List[str]
Returns:Node input names as a list.
name

Node’s name.

Return type:str
Returns:Node’s name if provided or the name of its function.
outputs
Return node outputs as a list preserving the original order
if possible.
Return type:List[str]
Returns:Node output names as a list.
run(inputs=None)[source]

Run this node using the provided inputs and return its results in a dictionary.

Parameters:

inputs (Optional[Dict[str, Any]]) – Dictionary of inputs as specified at the creation of the node.

Raises:
  • ValueError – In the following cases: a) The node function inputs are incompatible with the node input definition. Example 1: node definition input is a list of 2 DataFrames, whereas only 1 was provided or 2 different ones were provided. b) The node function outputs are incompatible with the node output definition. Example 1: node function definition is a dictionary, whereas function returns a list. Example 2: node definition output is a list of 5 strings, whereas the function returns a list of 4 objects.
  • Exception – Any exception thrown during execution of the node.
Return type:

Dict[str, Any]

Returns:

All produced node outputs are returned in a dictionary, where the keys are defined by the node outputs.

short_name

Node’s name.

Return type:str
Returns:Returns a short user-friendly name that is not guaranteed to be unique.
tag(tags)[source]
Create a new Node which is an exact copy of the current one,
but with more tags added to it.
Parameters:tags (Union[str, Iterable[str]]) – The tags to be added to the new node.
Return type:Node
Returns:A copy of the current Node object with the tags added.
tags

Return the tags assigned to the node.

Return type:Set[str]
Returns:Return the set of all assigned tags to the node.