Jimmy
Jimmy

Reputation: 161

How can I automatically run all the functions in the module?

I need to run several functions in a module as follws:

mylist = open('filing2.txt').read()
noTables = remove_tables(mylist)
newPassage = clean_text_passage(noTables)
replacement = replace(newPassage)
ncount = count_words(replacement)
riskcount = risk_count(ncount)

Is there any way that I can run all the functions at once? Should I make all the functions into a big function and run that big function?

Thanks.

Upvotes: 3

Views: 269

Answers (4)

martineau
martineau

Reputation: 123501

Here's another approach.

You could write a general function somewhat like that shown in the First-class composition section of the Wikipedia article on Function composition. Note that unlike in the article the functions are applied in the the order they are listed in the call to compose().

try:
    from functools import reduce  # Python 3 compatibility
except:
    pass

def compose(*funcs, **kwargs):
    """Compose a series of functions (...(f3(f2(f1(*args, **kwargs))))) into
    a single composite function which passes the result of each
    function as the argument to the next, from the first to last
    given.
    """
    return reduce(lambda f, g:
                    lambda *args, **kwargs: f(g(*args, **kwargs)),
                        reversed(funcs))

Here's a trivial example illustrating what it does:

f = lambda x: 'f({!r})'.format(x)
g = lambda x: 'g({})'.format(x)
h = lambda x: 'h({})'.format(x)

my_composition = compose(f, g, h)
print my_composition('X')

Output:

h(g(f('X')))

Here's how it could be applied to the series of functions in your module:

my_composition = compose(remove_tables, clean_text_passage, replace,
                         count_words, risk_count)
with open('filing2.txt') as input:
    riskcount = my_composition(input.read())

Upvotes: 0

martineau
martineau

Reputation: 123501

You should make a new function in the module which executes the common sequence being used. This will require you to figure out what input arguments are required and what results to return. So given the code you posted, the new function might look something like this -- I just guessed as to what final results you might be interested in. Also note that I opened the file within a with statement to ensure that it gets closed after reading it.

def do_combination(file_name):
    with open(file_name) as input:
        mylist = input.read()
    noTables = remove_tables(mylist)
    newPassage = clean_text_passage(noTables)
    replacement = replace(newPassage)
    ncount = count_words(replacement)
    riskcount = risk_count(ncount)

    return replacement, riskcount

Example of usage:

replacement, riskcount = do_combination('filing2.txt')

Upvotes: 2

Oleksii Kachaiev
Oleksii Kachaiev

Reputation: 6232

As far as I understood, use need function composition. There is no special function for this in Python stdlib, but you can do this with reduce function:

funcs = [remove_tables, clean_text_passage, replace, count_words, risk_count]
do_all = lambda args: reduce(lambda prev, f: f(prev), funcs, args)

Using as

with open('filing2.txt') as f:
    riskcount = do_all(f.read()) 

Upvotes: 1

Wolph
Wolph

Reputation: 80081

If you simply store these lines in a Python (.py) file you can simply execute them.

Or am I missing something here?

Creating a function is also easy to call them though:

def main():
    mylist = open('filing2.txt').read()
    noTables = remove_tables(mylist)
    newPassage = clean_text_passage(noTables)
    replacement = replace(newPassage)
    ncount = count_words(replacement)
    riskcount = risk_count(ncount)

main()

Upvotes: 1

Related Questions