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

Popular posts from this blog

python - Mongodb How to add addtional information when aggregating? -

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

ruby - Net::HTTP extremely slow responses for HTTPS requests -