def automatic_questioner3(function_name, db, choosen={}): """Function which carry out the automatic questioning task. Parameters ---------- function_name: str the function for which we are interested in their params in order to call it. db: dict the dictionary of all the information about the system with all its functions and dependencies between them in order to ask for their variables authomatically. choosen: dict previous choosen parameters. The function will avoid to ask for the pre-set parameters. Returns ------- choosen_values: dict the selected values which are disposed to input in the function we want to call. """ ## Initialize variables needed m1 = "Not value for a variables in order to create aggregate variables." choosen_values = choosen if function_name in db.keys(): data_f = db[function_name] else: # Better raise error? return choosen_values # Put the variables for var in data_f['variables'].keys(): # Put the variables if there are still not selected if var not in choosen_values.keys(): question = data_f['variables'][var]['question_info'] choosen_values[var] = general_questioner(**question) # Put aggregated variables (descendants) for i in range(len(data_f['descendants'])): # Possible variables and aggregated parameter name vars_values = data_f['descendants'][i]['variable_values'] agg_param = data_f['descendants'][i]['parameters'] variables = vars_values.keys() # prepare possible input for existant aggregated value in choosen ifaggvar = agg_param in choosen_values aggvarval = choosen_values[agg_param] if ifaggvar else {} for var in variables: # boolean variables value = choosen_values[var] iflist = type(value) == list ifvars = var in choosen_values.keys() # if we have to return a list if ifvars and iflist: # Initialization values n = len(value) aggvarval = aggvarval if ifaggvar else [{} for i in range(n)] results = [] i = 0 for val in value: # Obtain function_name f_name = vars_values[var][value] # Recurrent call aux = authomatic_questioner(f_name, db, aggvarval[i]) # Insert in the correspondent list results.append(aux) i += 1 # if we have to return a dict elif ifvars and not iflist: # Obtain function_name f_name = vars_values[var][value] # Recurrent call choosen_values[agg_param] = authomatic_questioner(f_name, db, aggvarval) return choosen_values
def automatic_questioner(function_name, db, choosen={}): """Function which carry out the automatic questioning task. Parameters ---------- function_name: str the function for which we are interested in their params in order to call it. db: dict the dictionary of all the information about the system with all its functions and dependencies between them in order to ask for their variables authomatically. choosen: dict previous choosen parameters. The function will avoid to ask for the pre-set parameters. Returns ------- choosen_values: dict the selected values which are disposed to input in the function we want to call. """ ## Initialize variables needed m1 = "Not value for a variables in order to create aggregate variables." choosen_values = choosen if function_name in db.keys(): data_f = db[function_name] else: # Better raise error? return choosen_values # Put the variables for var in data_f['variables'].keys(): # Put the variables if there are still not selected if var not in choosen_values.keys(): question = data_f['variables'][var]['question_info'] choosen_values[var] = general_questioner(**question) # Put aggregated variables (descendants) for var_desc in data_f['descendants']: # Possible variables and aggregated parameter name agg_description = var_desc['agg_description'] agg_param = var_desc['agg_name'] # prepare possible input for existant aggregated value in choosen ifaggvar = agg_param in choosen_values aggvarval = choosen_values[agg_param] if ifaggvar else {} ## Without dependant variable if type(agg_description) == str: # Obtain function name fn = choosen_values[agg_param] # Recurrent call aux = automatic_questioner(fn, db, aggvarval) # Aggregate to our values choosen_values[agg_param] = aux ## With dependant variable elif type(agg_description) == dict: for var in var_desc['agg_description']: if not var in choosen_values: raise Exception(m1) ## Give a list and return a dict in the aggparam variable elif type(choosen_values[var]) == str: # Obtain function name fn = var_desc['agg_description'][var][choosen_values[var]] # Recurrent call aux = automatic_questioner(fn, db, aggvarval) # Aggregate to our values choosen_values[agg_param] = aux ## Give a list and return a list in the aggparam variable elif type(choosen_values[var]) == list: choosen_values[agg_param] = [] aggvarval = [] if type(aggvarval) != list else aggvarval for i in range(len(choosen_values[var])): val = choosen_values[var][i] fn = var_desc['agg_description'][var][val] aux = automatic_questioner(fn, db, aggvarval[i]) choosen_values.append(aux) return choosen_values