def test_add_one_item(): gr = Grouping() gr['key'] = 'value' assert len(gr) == 1 assert gr['key'] == ['value']
def test_survey_survey_score_grouping() -> None: """A test for score_grouping() in class Survey.""" q1 = YesNoQuestion(1, 'BBC') q2 = MultipleChoiceQuestion(2, 'ABC', ['A', 'B', 'C']) a1 = Answer(True) a2 = Answer('A') a3 = Answer(True) a4 = Answer('C') a5 = Answer(True) a6 = Answer('B') a7 = Answer(False) a8 = Answer('C') stu1 = Student(100, 'Jack') stu2 = Student(200, 'Mike') stu3 = Student(300, 'Diana') stu4 = Student(400, 'Tom') stu1.set_answer(q1, a1) stu1.set_answer(q2, a2) stu2.set_answer(q1, a3) stu2.set_answer(q2, a4) stu3.set_answer(q1, a5) stu3.set_answer(q2, a6) stu4.set_answer(q1, a7) stu4.set_answer(q2, a8) s = Survey([q1, q2]) c = HomogeneousCriterion() s.set_weight(2.0, q1) s.set_criterion(c, q1) s.set_criterion(c, q2) g1 = Group([stu1, stu2]) g2 = Group([stu3, stu4]) grouping = Grouping() grouping.add_group(g1) grouping.add_group(g2) assert s.score_grouping(grouping) == 0.5
def test_simple_sequence_example(): """ This was a example / use case in Michael Selik's PEP """ gr = Grouping(((c.casefold(), c) for c in 'AbBa')) assert gr == {'a': ['A', 'a'], 'b': ['b', 'B']}
def test_add_key_value(): gr = Grouping(key_fun=len, value_fun=str.capitalize) gr.add("fred") gr.add("bob") gr.add("mary") assert gr == {4: ['Fred', 'Mary'], 3: ['Bob']}
def test_most_common(): gr = Grouping(((c.casefold(), c) for c in 'AbBaAAbCccDe')) common = gr.most_common() assert len(common) == len(gr) common = gr.most_common(2) print(common) assert len(common) == 2 assert common == [('a', ['A', 'a', 'A', 'A']), ('b', ['b', 'B', 'b'])]
def test_grouper_grouping_add_group_duplicate() -> None: """A test for add_group() in class Grouping.""" s1 = Student(1, 'A') s2 = Student(2, 'B') s4 = Student(4, 'D') g1 = Group([s1, s2]) g2 = Group([s1, s4]) grouping = Grouping() assert grouping.add_group(g1) assert grouping.add_group(g2) is False
def test_example_loop(): gr = Grouping() for student, school in student_school_list: gr[school] = student assert len(gr) == 3 assert gr['SchoolA'] == ['Fred', 'Mary'] assert gr['SchoolB'] == ['Bob', 'Jane'] assert gr['SchoolC'] == ['Nancy']
def test_grouper_grouping_add_group_valid() -> None: """A test for add_group() in class Grouping.""" s1 = Student(1, 'A') s2 = Student(2, 'B') s3 = Student(3, 'C') s4 = Student(4, 'D') g1 = Group([s1, s2]) g2 = Group([s3, s4]) grouping = Grouping() assert grouping.add_group(g1) assert grouping.add_group(g2)
def test_grouper_grouping_len() -> None: """A test for __len__() in class Grouping.""" s1 = Student(1, 'A') s2 = Student(2, 'B') s3 = Student(3, 'C') s4 = Student(4, 'D') g1 = Group([s1, s2]) g2 = Group([s3, s4]) grouping = Grouping() grouping.add_group(g1) grouping.add_group(g2) assert len(grouping) == 2
def test_constructor_dict(): """ Trying to be as similar to the dict constructor as possible: You can contruct with a Mapping that already has groups """ gr = Grouping(student_school_dict) assert len(gr) == 3 assert gr['SchoolA'] == ['Fred', 'Mary'] assert gr['SchoolB'] == ['Bob', 'Jane'] assert gr['SchoolC'] == ['Nancy']
def test_constructor_list(): """ Trying to be as similar to the dict constructor as possible: We can use pass an iterable of (key, value) tuples, the keys will be what is grouped by, and the values will be in the groups. """ gr = Grouping(((item[1], item[0]) for item in student_school_list)) assert len(gr) == 3 assert gr['SchoolA'] == ['Fred', 'Mary'] assert gr['SchoolB'] == ['Bob', 'Jane'] assert gr['SchoolC'] == ['Nancy']
def test_grouper_grouping_get_groups() -> None: """A test for get_groups() in class Grouping.""" s1 = Student(1, 'A') s2 = Student(2, 'B') s3 = Student(3, 'C') s4 = Student(4, 'D') g1 = Group([s1, s2]) g2 = Group([s3, s4]) grouping = Grouping() grouping.add_group(g1) grouping.add_group(g2) result = grouping.get_groups() assert g1 in result assert g2 in result
def test_only_specify_value(): """ last names by first initial """ data = [ ("Seattle", "Cleveland", "Barker"), ("Cleveland", "Oakland", "Jones"), ("Cleveland", "San Jose", "Miller"), ("Seattle", "Boston", "Cooper"), ("San Francisco", "Atlanta", "Barker"), ] gr = Grouping(data, value_fun=itemgetter(2)) print(gr) assert gr == { 'Seattle': ['Barker', 'Cooper'], 'Cleveland': ['Jones', 'Miller'], 'San Francisco': ['Barker'] }
def test_grouper() -> None: assert windows([3, 4, 6, 2, 3], 2) == [[3, 4], [4, 6], [6, 2], [2, 3]] assert windows(['a', 1, 6.0, False], 3) == [['a', 1, 6.0], [1, 6.0, False]] assert slice_list([3, 4, 6, 2, 3], 2) == [[3, 4], [6, 2], [3]] assert slice_list(['a', 1, 6.0, False], 3) == [['a', 1, 6.0], [False]] g = Group([Student(1, "roney"), Student(2, "tim"), Student(3, "allen")]) g_1 = Group([Student(1, "roney"), Student(2, "tim"), Student(3, "allen")]) g_2 = Group([Student(5, "roney"), Student(6, "tim"), Student(7, "allen")]) assert len(g) == 3 assert Student(1, "roney") in g assert "roney" in str(g) gr = Grouping() assert gr.add_group(g) assert not gr.add_group(g_1) assert gr.add_group(g_2) assert len(gr) == 2 course_0 = Course("Snake") course_0.enroll_students( [Student(1, "a"), Student(2, "b"), Student(3, "c")]) s = Survey([YesNoQuestion(1, "Is earth round")]) ag = AlphaGrouper(2) gr = ag.make_grouping(course_0, s) assert len(gr) == 2 course_0 = Course("Snake") course_0.enroll_students( [Student(1, "a"), Student(2, "b"), Student(3, "c")]) s = Survey([YesNoQuestion(1, "Is earth round")]) ag = AlphaGrouper(3) gr = ag.make_grouping(course_0, s) assert len(gr) == 1 course_0 = Course("Snake") course_0.enroll_students( [Student(1, "a"), Student(2, "b"), Student(3, "c")]) s = Survey([YesNoQuestion(1, "Is earth round")]) ag = RandomGrouper(2) gr = ag.make_grouping(course_0, s) assert len(gr) == 2 course_0 = Course("Snake") course_0.enroll_students( [Student(1, "a"), Student(2, "b"), Student(3, "c")]) s = Survey([YesNoQuestion(1, "Is earth round")]) ag = RandomGrouper(3) gr = ag.make_grouping(course_0, s) assert len(gr) == 1 course_0 = Course("Snake") s1 = Student(1, "a") s2 = Student(2, "b") s3 = Student(3, "c") q = YesNoQuestion(1, "Is earth round") s1.set_answer(q, Answer(True)) s2.set_answer(q, Answer(False)) s3.set_answer(q, Answer(True)) course_0.enroll_students([s1, s2, s3]) s = Survey([q]) ag = GreedyGrouper(2) gr = ag.make_grouping(course_0, s) assert len(gr) == 2 course_0 = Course("Snake") s1 = Student(1, "a") s2 = Student(2, "b") s3 = Student(3, "c") q = YesNoQuestion(1, "Is earth round") s1.set_answer(q, Answer(True)) s2.set_answer(q, Answer(False)) s3.set_answer(q, Answer(True)) course_0.enroll_students([s1, s2, s3]) s = Survey([q]) ag = GreedyGrouper(3) gr = ag.make_grouping(course_0, s) assert len(gr) == 1 groups = gr.get_groups() assert groups[0]._member_id == [1, 3, 2] course_0 = Course("Snake") s1 = Student(1, "a") s2 = Student(2, "b") s3 = Student(3, "c") q = YesNoQuestion(1, "Is earth round") s1.set_answer(q, Answer(True)) s2.set_answer(q, Answer(False)) s3.set_answer(q, Answer(True)) course_0.enroll_students([s1, s2, s3]) s = Survey([q]) s.set_criterion(LonelyMemberCriterion(), q) ag = GreedyGrouper(2) gr = ag.make_grouping(course_0, s) assert len(gr) == 2 groups = gr.get_groups() assert groups[0]._member_id == [1, 3] assert groups[1]._member_id == [2] course_0 = Course("Snake") s1 = Student(1, "a") s2 = Student(2, "b") s3 = Student(3, "c") s4 = Student(4, "d") q = YesNoQuestion(1, "Is earth round") s1.set_answer(q, Answer(True)) s2.set_answer(q, Answer(True)) s3.set_answer(q, Answer(True)) s4.set_answer(q, Answer(True)) course_0.enroll_students([s1, s2, s3, s4]) s = Survey([q]) s.set_criterion(LonelyMemberCriterion(), q) ag = WindowGrouper(2) gr = ag.make_grouping(course_0, s) assert len(gr) == 2 groups = gr.get_groups() assert groups[0]._member_id == [1, 2] assert groups[1]._member_id == [3, 4] course_0 = Course("Snake") s1 = Student(1, "a") s2 = Student(2, "b") s3 = Student(3, "c") s4 = Student(4, "d") q = YesNoQuestion(1, "Is earth round") s1.set_answer(q, Answer(True)) s2.set_answer(q, Answer(False)) s3.set_answer(q, Answer(True)) s4.set_answer(q, Answer(False)) course_0.enroll_students([s1, s2, s3, s4]) s = Survey([q]) s.set_criterion(HeterogeneousCriterion(), q) ag = WindowGrouper(2) gr = ag.make_grouping(course_0, s) assert len(gr) == 2 groups = gr.get_groups() assert groups[0]._member_id == [1, 2] assert groups[1]._member_id == [3, 4] course_0 = Course("Snake") s1 = Student(1, "a") s2 = Student(2, "b") s3 = Student(3, "c") s4 = Student(4, "d") q = YesNoQuestion(1, "Is earth round") s1.set_answer(q, Answer(True)) s2.set_answer(q, Answer(False)) s3.set_answer(q, Answer(True)) s4.set_answer(q, Answer(False)) course_0.enroll_students([s1, s2, s3, s4]) s = Survey([q]) s.set_criterion(HomogeneousCriterion(), q) ag = WindowGrouper(2) gr = ag.make_grouping(course_0, s) assert len(gr) == 2 groups = gr.get_groups() assert groups[0]._member_id == [1, 2] assert groups[1]._member_id == [3, 4] course_0 = Course("Snake") s1 = Student(1, "a") s2 = Student(2, "b") s3 = Student(3, "c") s4 = Student(4, "d") q = YesNoQuestion(1, "Is earth round") s1.set_answer(q, Answer(True)) s2.set_answer(q, Answer(False)) s3.set_answer(q, Answer(False)) s4.set_answer(q, Answer(False)) course_0.enroll_students([s1, s2, s3, s4]) s = Survey([q]) s.set_criterion(HomogeneousCriterion(), q) ag = WindowGrouper(2) gr = ag.make_grouping(course_0, s) assert len(gr) == 2 groups = gr.get_groups() assert groups[0]._member_id == [2, 3] assert groups[1]._member_id == [1, 4]
def test_grouper_grouping_to_string() -> None: """A test for __str__() in class Grouping.""" grouping = Grouping() assert type(str(grouping)) is str
def test_init_empty(): gr = Grouping() assert len(gr) == 0
def test_names_by_first_initial(): names = ["Fred", "Bob", "Frank", "Mary", "Billy"] gr = Grouping(names, key_fun=itemgetter(0)) assert gr == {'F': ['Fred', 'Frank'], 'B': ['Bob', 'Billy'], 'M': ['Mary']}
words = "I wish I may I wish I might".split() # using setdeafult with a regular dict: trigrams = {} for i in range(len(words) - 2): pair = tuple(words[i:i + 2]) follower = words[i + 2] trigrams.setdefault(pair, []).append(follower) print(trigrams) # using a Grouping with a regular loop: trigrams = Grouping() for i in range(len(words) - 2): pair = tuple(words[i:i + 2]) follower = words[i + 2] trigrams[pair] = follower print(trigrams) # using a Grouping with zip trigrams = Grouping() for w1, w2, w3 in zip(words[:], words[1:], words[2:]): trigrams[(w1, w2)] = w3 print(trigrams)
def test_grouper_grouping_add_group_empty() -> None: """A test for add_group() in class Grouping.""" g1 = Group([]) grouping = Grouping() assert grouping.add_group(g1) is False