python - Mapping a returned string to a specific function -
i have router deciding function call based upon user input (uses configparser) , tries decide function call.
def somethingelse(): print 'hello' def uploaddirectory(): print 'hi' def router(config): if config.has_option('job', 'some_task'): taskname = config.get('job', 'some_task') # taskname 'uploaddirectory' ###execute uploaddirectory function ###execute else if else, etc so way write in python? if prebuilt map of functions strings, can execute them way?
how write this?
yep, building map of strings function names valid:
task_map = { 'upload': uploaddirectory, 'something': somethingelse, } and execute as:
task_map[task_name]() aside: try follow pep-8, python style guide; helps make code more readable other python programmers. in case, prefer underscore_separated function names instead of leadingcaps.
Comments
Post a Comment