Home > Enterprise >  call functions based on dynamic list from the user
call functions based on dynamic list from the user

Time:09-13

I have a report generating API in my django app which takes name of reports as input from the user, user can multiselect different reports from the dropdown

    reports = request.GET['reports'].split(',')

example:

    print("selected reports", reports)

    >>seleceted reports ['AS', 'DD', 'IS', 'LM']

now this reports can vary according to the user and I have a dictionary of all the functions to create these reports

master = {
            'AS': audit_summary(),
            'AH': audit_health(),
            'MS': missing_scan(),
            'IS': individual(),
            'LM': loc_mis(),
            'MAS': missing_assets(),
            'DD' : dump(),
              }

How can I call the functions from the dictionary based on the dynamic list I get from user?

CodePudding user response:

I think I get what you're trying to do. You could change the master list to be a dictionary of functions instead of function results like this. I'm not 100% sure of the syntax at the end but I know it's possible.

master = {
            'AS': audit_summary,
            'AH': audit_health,
            'MS': missing_scan,
            'IS': individual,
            'LM': loc_mis,
            'MAS': missing_assets,
            'DD' : dump,
              }

def callAllTheFuncts(data):
    for item in data:
        master[item]()

CodePudding user response:

for i in selected_reports:
    masters[i]   # this will call the function

assuming the variable selected_reports contains the list

  • Related