Skip to content

Instantly share code, notes, and snippets.

@gregjurman
Forked from rossdylan/pipeline.py
Created January 16, 2013 01:55
Show Gist options
  • Save gregjurman/4543948 to your computer and use it in GitHub Desktop.
Save gregjurman/4543948 to your computer and use it in GitHub Desktop.
"""
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment