Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - multiple functions - output of one to the next

I know this is super basic and I have been searching everywhere but I am still very confused by everything I'm seeing and am not sure the best way to do this and am having a hard time wrapping my head around it.

I have a script where I have multiple functions. I would like the first function to pass it's output to the second, then the second pass it's output to the third, etc. Each does it's own step in an overall process to the starting dataset.

For example, very simplified with bad names but this is to just get the basic structure:

#!/usr/bin/python
# script called process.py
import sys
infile = sys.argv[1]

def function_one():
    do things
    return function_one_output

def function_two():
    take output from function_one, and do more things
    return function_two_output

def function_three():
    take output from function_two, do more things
    return/print function_three_output

I want this to run as one script and print the output/write to new file or whatever which I know how to do. Just am unclear on how to pass the intermediate outputs of each function to the next etc.

infile -> function_one -> (intermediate1) -> function_two -> (intermediate2) -> function_three -> final result/outfile

I know I need to use return, but I am unsure how to call this at the end to get my final output

Individually?

function_one(infile)
function_two()
function_three()

or within each other?

function_three(function_two(function_one(infile)))

or within the actual function?

def function_one():
    do things
    return function_one_output

def function_two():
    input_for_this_function = function_one()

# etc etc etc

Thank you friends, I am over complicating this and need a very simple way to understand it.

like image 747
bburc Avatar asked Jun 03 '15 14:06

bburc


People also ask

Why does my python function return multiple values?

There may be many times when your function returns multiple values, but you only care about a few. You don’t really care about the other values, but Python will not let you return only a few of them. This happens because our assignment needs to match the number of items returned.

What is next() function in Python?

What is next () function in python? To retrieve the next value from an iterator, we can make use of the next () function. Also, we cannot use next () with a list or a tuple. But we can make a list or tuple or string an iterator and then use next (). If we want to create an iterable an iterator, we can use iter ...

What is the difference between function_two() and function_three() in Python?

In the example above, function_two () returns the square of its argument, and function_three () the cube of its argument. Each can be called independently from elsewhere in your code, without being entangled in some hardcoded call chain such as you would have if called one function from another.

How do you return more than one value from a function?

In the previous section, you learned how to configure a Python function to return more than a single value. The way that this works, is that Python actually turns the values (separated by commas) into a tuple. We can see how this works by assigning the function to a variable and checking its type.


1 Answers

You could define a data streaming helper function

    from functools import reduce
    
    def flow(seed, *funcs):
        return reduce(lambda arg, func: func(arg), funcs, seed)

    flow(infile, function_one, function_two, function_three)

    #for example
    flow('HELLO', str.lower, str.capitalize, str.swapcase)
    #returns 'hELLO'

edit

I would now suggest that a more "pythonic" way to implement the flow function above is:

def flow(seed, *funcs):
    for func in funcs:
        seed = func(seed)
    return seed;
like image 80
Doug Coburn Avatar answered Oct 05 '22 02:10

Doug Coburn