def generate_stance(self, stance_importance, relation_importance): """Generates stances for testing with sort keys""" stance = Stance() stance.importance = stance_importance relation = Relation() relation.importance = relation_importance stance.relation = relation stance.sort_key = stance_sort_key.LOYALTY return stance
def _compare_stances(fors, agns): """This functions compares the stances to see which side has the most compelling reasons. It does so by checking which side has the largest number and most important stances supporting it. Arguments: fors: the list of stances supporting the bill to decide on agns: the list of stances against the bill to decide on Returns: A list containing the strongest arguments for or against the bill. Returns an empty list if both sides are equally compelling. Notes: Both lists are first sorted by importance. Then, it goes through and compares each stance. If one list runs out before the other, the longest list is considered to have the most compelling stances. If one list is found to have a stance of stronger importance than the other on a give iteration of the for loop, that list is considered to be the most important and is chosen. """ fors.sort(key=lambda stance: stance.sort_key, reverse=True) agns.sort(key=lambda stance: stance.sort_key, reverse=True) # base_stance is used in the enum. Once an array runs out of stances, # this will be mapped to be compared to the stances in the other list. Since # Z is not an importance actually assigned in the DB, this is guaranteed to # fail so that the longer list will be detected as the winner. # # The sort key is also needed to properly set the default. It will take the # source key from whichever array will last longer. This will ensure that # the stance is less than the next key it will be compared to. temp_stance = None if fors and len(fors) > len(agns): temp_stance = fors[0] elif agns and len(agns) > len(fors): temp_stance = agns[0] base_stance = Stance() base_stance.importance = importance.Z base_stance.sort_key = temp_stance._sort_key if temp_stance else None enum = enumerate(itertools.izip_longest(fors, agns, fillvalue=base_stance)) for index, (a_for, an_agn) in enum: if a_for.sort_key > an_agn.sort_key: result = ResultData() result.outcome = outcomes.FOR result.data = util.remove_less_important_stances(fors[index:]) logger.LOGGER.info("MI stance is FOR the bill.") return result if an_agn.sort_key > a_for.sort_key: result = ResultData() result.outcome = outcomes.AGN result.data = util.remove_less_important_stances(agns[index:]) logger.LOGGER.info("MI stance is AGN the bill.") return result logger.LOGGER.info("MI stance is neutral on the bill.") return None