Search code examples
pythonpython-3.xclasspipelinekedro

How to run functions from a Class in the nodes.py file?


I want to organize the node functions by Classes in the nodes.py file. For example, functions related to cleaning data are in the "CleanData" Class, with a @staticmethod decorator, while other functions will stay in the "Other" Class, without any decorator (the names of these classes are merely representative). In the pipeline file, I tried importing the names of the classes, the names of the nodes and the following way: CleanData.function1 (which gave an error) and none of them worked. How can I call the nodes from the classes, if possible, please?


Solution

  • I'm not entirely certain what the error you're getting is. If you're literally trying to do from .nodes import CleanData.function1 that won't work. Imports don't work like that in Python. If you do something like this:

    nodes.py has:

    class CleanData:
        def clean(arg1):
            pass
    

    and pipeline.py has:

    from kedro.pipeline import Pipeline, node
    from .nodes import CleanData
    
    def create_pipeline(**kwargs):
        return Pipeline(
            [
                node(
                    CleanData.clean,
                    "example_iris_data",
                    None,
                )
            ]
        )
    

    that should work.