Friday, February 12, 2010

python: call function stored in string variable

In the following, op1 and op2 are strings containing function names (e.g. 'stop', 'enable', 'disable') to be invoked:

def start():
    print 'INFO|start()|starting identity %s.%s' %(name1, domain)
def stop():
    print 'INFO|start()|starting identity %s.%s' %(name1, domain)
def enable():
    print 'INFO|enable()|enabling identity %s.%s' %(name1, domain)
def disable():
    print 'INFO|disable()|disabling identity %s.%s' %(name1, domain)

function_mapper = { 'stop':stop,
                    'start':start,
                    'enable':enable,
                    'disable':disable
                  }


def main():
    newlist = ['start', 'disable', 'enable']
    print '\n\n', newlist

    for op2 in newlist:
    for op1 in newlist:
    function_mapper[op1]()
    function_mapper[op2]()
    print "\n"

    
if __name__ == '__main__':
sys.exit( main() )

No comments: