Skip to content

Instantly share code, notes, and snippets.

@rossdylan
Created January 16, 2013 01:40

Revisions

  1. rossdylan created this gist Jan 16, 2013.
    46 changes: 46 additions & 0 deletions pipeline.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    """
    Misc tools/functions written by Ross Delinger
    """


    def assemblePipeLine(*args, **kwargs):
    """
    Given an arbitrary number of functions we create a pipeline where the output
    is piped between functions. you can also specify a tuple of arguments that
    should be passed to functions in the pipeline. The first arg is always the
    output of the previous function.
    """
    def wrapper(*data):
    if len(args) == 1:
    if args[-1].__name__ in kwargs:
    otherArgs = data + kwargs[args[-1].__name__]
    return args[-1](*otherArgs)
    else:
    return args[-1](*data)
    else:
    if args[-1].__name__ in kwargs:
    otherArgs = kwargs[args[-1].__name__]
    del kwargs[args[-1].__name__]
    return args[-1](assemblePipeLine(*args[:-1], **kwargs)(*data), *otherArgs)
    else:
    return args[-1](assemblePipeLine(*args[:-1], **kwargs)(*data))
    return wrapper


    def testAssemblePipelineWithArgs():
    def add1(input_):
    return input_ + 1

    def subX(input_, x):
    return input_ - x

    def stringify(input_):
    return str(input_)

    pipeline = assemblePipeLine(
    add1,
    subX,
    stringify,
    subX=(2,),
    )
    print pipeline(10)