def perf_func(col_name, entries, col_funcs, grouping=None): ''' Given a dictionary of column functions mapping to column name, perform column function on column entries. Do it by groups if there is grouping :param col_name: string column name :param entries: a list of values :param col_funcs: a dict {col_name: func_name} :param grouping: a list of [indexes], each sublist is a equivalence class :return: a list of entries [val1, val2...] ''' if grouping: # there are grouping if col_funcs.get(col_name): func_name = col_funcs[col_name] # Perform column function on each grouping of the entries perf_entries = [eva.FUNC[func_name](man.get_entries(entries, grp)) for grp in grouping] else: # No column function, return the first element of each group perf_entries = [entries[grp[0]] for grp in grouping] else: # There are no grouping if col_funcs.get(col_name): # There is column function for the column # Get the name of the column function func_name = col_funcs[col_name] # perform col functions on column entries perf_entries = [eva.FUNC[func_name](entries)] else: # if there's no need to perform col func, return entries as it is perf_entries = entries return perf_entries
def filter_grps(line, tb, grouping): ''' Filter out groups that do not meet filter condition before performing column functions on groups in do_col_func_two :param line: a TENIENDO condition without ' TENIENDO ' and '()' :param tb: a dict {col_name: [entries]} :param grouping: a list of [indexes], each sublist is an equivalence class :return: a list of [indexes], each equivalence class meets the condition ''' # split the line into var1, operator, var2 var1, oper, var2 = split_clauses(line) if '(' in var1: col_str = var1 val_str = var2 else: col_str = var2 val_str = var1 # Get the list of function names func_keys = list(eva.FUNC.keys()) col, func = [(strip_func(col_str), func) for func in func_keys if func + '(' in col_str][0] if tb.get(col) is None: raise hpr.InvalidQueryError('Table do not have column: {0}'.format(col)) # Get the list of entry values of each group after performing function values = [eva.FUNC[func](man.get_entries(tb[col], grp)) for grp in grouping] # Filter list of values by the var2 using operator, return a list of group # ids of groups that passed filter condition if col_str == var1: filtered_grps = [grp_id for grp_id, val in enumerate(values) if eva.OPER[oper](var1=val, var2=hpr.parse_val(val_str))] else: filtered_grps = [grp_id for grp_id, val in enumerate(values) if eva.OPER[oper](var1=hpr.parse_val(val_str), var2=val)] return [grouping[i] for i in filtered_grps]