pythonfunctionpipelinechain

Python chain several functions into one


I have several string processing functions like:

def func1(s):
    return re.sub(r'\s', "", s)

def func2(s):
    return f"[{s}]"
...

I want to combine them into one pipeline function: my_pipeline(), so that I can use it as an argument, for example:

class Record:
    def __init__(self, s):
        self.name = s
    
    def apply_func(self, func):
        return func(self.name)

rec = Record(" hell o")
output = rec.apply_func(my_pipeline)
# output = "[hello]"

The goal is to use my_pipeline as an argument, otherwise I need to call these functions one by one.

Thank you.


Solution

  • You can write a simple factory function or class to build a pipeline function:

    def pipeline(*functions):
        def _pipeline(arg):
            result = arg
            for func in functions:
                result = func(result)
            return result
        return _pipeline
    

    Test:

    >>> rec = Record(" hell o")
    >>> rec.apply_func(pipeline(func1, func2))
    '[hello]'
    

    This is a more refined version written with reference to this using functools.reduce:

    from functools import reduce
    
    def pipeline(*functions):
        return lambda initial: reduce(lambda arg, func: func(arg), functions, initial)
    

    I didn't test it, but according to my intuition, each loop will call the function one more time at the python level, so the performance may not be as good as the loop implementation.