简体   繁体   中英

Python: assign a list to a function

Is there a way to assign a list based on a variable env and pass the result to function? I am passing a variable called env, which could be UPE, DEV, PRD for example. Based on the result, I want to assign the list respectively to the functions below. What would be the best approach?

UPE=['SERVER1','SERVER2','SERVER3','SERVER4']
DEV=['ServerA','ServerB']
PRD=['SERVER1','SERVER2','SERVER3','SERVER4']

if os.path.isfile('/myfile/' + configFile):

  config_parser = ConfigParser()
  config_parser.read('/myfile/' + configFile)

if actions == "start":
    startOVD('start',UPE[3]) //I want pass the result of env setup variable
    #ans = raw_input("Would you like to start OVD, MSAS,OAG : y | n : ")
    if env == 'UPE':
        startMSAS('start',UPE[0])
        startOAG('start',UPE[1])
    startOHS('start',UPE[2])
    for section_name in sorted(config_parser.sections(), reverse=True):
        adminURL = config_parser.get(section_name, 'ADMIN_URL')
        adminUsername = config_parser.get(section_name, 'ADMIN_USER')
        adminPassword = config_parser.get(section_name, 'ADMIN_PASS')
        adminHost = config_parser.get(section_name, 'NM_HOST')
        domainName = config_parser.get(section_name, 'DOMAIN_NAME')
        domainDir = config_parser.get(section_name, 'DOMAIN_DIR')
        admPort = adminURL[-4:]

        printHeader('Initiating Starting Sequence')
        startAdmin('start', adminHost, domainDir, domainName, admPort)
        showServerStatus('start', adminUsername, adminPassword, adminURL)


if actions == "stop":
    for section_name in (config_parser.sections()):
        adminURL = config_parser.get(section_name, 'ADMIN_URL')
        adminUsername = config_parser.get(section_name, 'ADMIN_USER')
        adminPassword = config_parser.get(section_name, 'ADMIN_PASS')
        adminHost = config_parser.get(section_name, 'NM_HOST')
        domainName = config_parser.get(section_name, 'DOMAIN_NAME')
        domainDir = config_parser.get(section_name, 'DOMAIN_DIR')
        admPort = adminURL[-4:]

        printHeader('Initiating Stopping Sequence')
        showServerStatus('stop', adminUsername, adminPassword, adminURL)
        stopAdmin(adminHost, domainDir, domainName, admPort)

    if env == 'UPE':
        stopMSAS('stop',UPE[0])
        stopOAG('stop',UPE[1])
    stopOHS('stop',UPE[2])
    stopOVD('stop',UPE[3])

I would organize this by setting up a list of callbacks.

from functools import partial

start_funcs = [partial(startOVD 'start',UPE[3])
if env == 'UPE':
    start_funcs.extend([partial(startMSAS, 'start', UPE[0]),
                       partial(startOAG, 'start', UPE[1])])
start_funcs.append(partial(startOHS, 'start', UPE[2]))

Add similar logic for the cases when env has a different value. In the end, you'll just iterate over start_funcs and call each function in order.

if actions == "start":
    for f in start_funcs:
        f()

    # ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM