<reponame>tdiprima/code<filename>recipes/Python/580681_Drive_which_Pythfunctions_are_executed_vitext/recipe-580681.py
# Driving Python function execution from a text file.
# Author: <NAME>: 
# https://vasudevram.github.io, 
# http://jugad2.blogspot.com
# Copyright 2016 <NAME>

def square(n): return n * n
def cube(n): return n * square(n)
def fourth(n):  return square(square(n))

# 1) Define the fns dict literally ...
#fns = {'square': square, 'cube': cube, 'fourth': fourth}
# 2a) ... or programmatically with a dict comprehension ...
fns = { fn.func_name : fn for fn in (square, cube, fourth) }
# OR:
# 2b) 
# fns = { fn.__name__ : fn for fn in (square, cube, fourth) }
# The latter approach (2a or 2b) scales better with more functions, 
# and reduces the chance of typos in the function names.

with open('functions.txt') as fil:
    for line in fil:
        print
        line = line[:-1] 
        if line.lower() not in fns:
            print "Skipping invalid function name:", line
            continue
        for item in range(1, 5):
            print 'item: ' + str(item) + ' : ' + line + \
            '(' + str(item) + ') : ' + str(fns[line](item)).rjust(3)

# Driving Python function execution from a text file.
# Author: <NAME>: 
# https://vasudevram.github.io, 
# http://jugad2.blogspot.com
# Copyright 2016 <NAME>

def square(n): return n * n
def cube(n): return n * square(n)
def fourth(n):  return square(square(n))

# 1) Define the fns dict literally ...
#fns = {'square': square, 'cube': cube, 'fourth': fourth}
# 2a) ... or programmatically with a dict comprehension ...
fns = { fn.func_name : fn for fn in (square, cube, fourth) }
# OR:
# 2b) 
# fns = { fn.__name__ : fn for fn in (square, cube, fourth) }
# The latter approach (2a or 2b) scales better with more functions, 
# and reduces the chance of typos in the function names.

with open('functions.txt') as fil:
    for line in fil:
        print
        line = line[:-1] 
        if line.lower() not in fns:
            print "Skipping invalid function name:", line
            continue
        for item in range(1, 5):
            print 'item: ' + str(item) + ' : ' + line + \
            '(' + str(item) + ') : ' + str(fns[line](item)).rjust(3)
# Driving Python function execution from a text file. # Author: <NAME>: # https://vasudevram.github.io, # http://jugad2.blogspot.com # Copyright 2016 <NAME> # 1) Define the fns dict literally ... #fns = {'square': square, 'cube': cube, 'fourth': fourth} # 2a) ... or programmatically with a dict comprehension ... # OR: # 2b) # fns = { fn.__name__ : fn for fn in (square, cube, fourth) } # The latter approach (2a or 2b) scales better with more functions, # and reduces the chance of typos in the function names.