travelsandbooks
travelsandbooks

Reputation: 333

Clean way to run multiple sequential functions on the same dataset

Using Python, I am running a series of preprocessing functions on a list, like this:

my_list0 = [1, 2, 3, ...]

my_list1 = function_foo(my_list0)
my_list2 = function_bar(my_list1)
my_list3 = function_fizz(my_list2)
my_list4 = function_buzz(my_list3)

Is there a more sophisticated way of laying this out? It gets the job done but it feels like it could be more elegant?

Thank you!

Upvotes: 0

Views: 435

Answers (4)

Daniel Trugman
Daniel Trugman

Reputation: 8511

So this reminds a pipeline design pattern.

You can use a simple wrapper methods to help with that:

def pipeline(data, *filters):
    for filter in filters:
        data = filter(data)
    return data 

And then you can use:

pipeline([1,2,3], foo, bar, fizz, buzz)

Upvotes: 3

Michael Butscher
Michael Butscher

Reputation: 10959

If all functions take the same parameter(s) you can write it:

my_list0 = [1, 2, 3, ...]

actions = [function_foo, function_bar, function_fizz, function_buzz]

my_list = my_list0

for a in actions:
    my_list = a(my_list)

Upvotes: 1

matszwecja
matszwecja

Reputation: 8076

def preprocess_list(my_list):
    return function_buzz(function_fizz(function_bar(function_foo(my_list))))

my_list0 = [1, 2, 3, ...]
preprocessed_list = preprocess_list(my_list0)

Upvotes: 1

quamrana
quamrana

Reputation: 39404

Yes, you can put the function names into a list and iterate over them:

functions = [function_foo, function_bar, function_fizz, function_buzz]

result = my_list0
for f in functions:
    result = f(result)

print(result)

This way, if you only need to maintain the list of functions (add, remove, reorder) without changing the code which iterates through them.

Upvotes: 1

Related Questions