def test_remove_less_important_stances_loyalty(self): """ Verifies remove less importance when using a sort key.""" stance1 = self.generate_stance(importance.C, importance.A) stance2 = self.generate_stance(importance.C, importance.A) stance3 = self.generate_stance(importance.A, importance.B) answer = [stance1, stance2] result = util.remove_less_important_stances([stance1, stance2, stance3]) self.assertEqual(len(result), len(answer)) for stance1, stance2 in zip(result, answer): self.assertEquals(stance1, stance2)
def test_remove_less_important_stances_sorts_stances(self): """ Verifies function works properly when on unsorted arrays.""" stance1 = self.generate_stance(importance.C, importance.A) stance2 = self.generate_stance(importance.C, importance.A) stance3 = self.generate_stance(importance.A, importance.B) answer = [stance2, stance1] data = [stance3, stance2, stance3, stance1] result = util.remove_less_important_stances(data) self.assertEqual(len(result), len(answer)) for stance1, stance2 in zip(result, answer): self.assertEquals(stance1, stance2)
def test_remove_less_important_stances_only_one(self): """ Verifies remove less importance where only one is left.""" stance1 = Stance() stance1.importance = importance.A stance2 = Stance() stance2.importance = importance.B stance3 = Stance() stance3.importance = importance.C answer = [stance1] result = util.remove_less_important_stances([stance1, stance2, stance3]) self.assertEqual(len(result), len(answer)) for stance1, stance2 in zip(result, answer): self.assertEquals(stance1, stance2)
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