Exemple #1
0
    def score_grouping(self, grouping: Grouping) -> float:
        """ Return a score for <grouping> calculated based on the answers of
        each student in each group in <grouping> to the questions in <self>.

        If there are no groups in <grouping> this score is 0.0. Otherwise, this
        score is determined using the following algorithm:

        1. For each group in <grouping>, get the score for the members of this
           group calculated based on their answers to the questions in this
           survey.
        2. Return the average of all the scores calculated in step 1.

        === Precondition ===
        All students in the groups in <grouping> have an answer to all questions
            in this survey
        """
        if grouping.__len__() == 0:
            return 0.0
        list_of_score = []
        for group in grouping.get_groups():
            s = group.get_members()
            list_of_score.append(self.score_students(s))
        sum = 0
        for score in list_of_score:
            sum += score
        return sum / len(list_of_score)
Exemple #2
0
    def score_grouping(self, grouping: Grouping) -> float:
        """ Return a score for <grouping> calculated based on the answers of
        each student in each group in <grouping> to the questions in <self>.

        If there are no groups in <grouping> this score is 0.0. Otherwise, this
        score is determined using the following algorithm:

        1. For each group in <grouping>, get the score for the members of this
           group calculated based on their answers to the questions in this
           survey.
        2. Return the average of all the scores calculated in step 1.

        === Precondition ===
        All students in the groups in <grouping> have an answer to all questions
            in this survey
        """
        # TODO: complete the body of this method
        num1 = 0
        num2 = 0
        if len(grouping.get_groups()) == 0:
            return 0.0

        for g in grouping.get_groups():
            score = self.score_students(g.get_members())
            num1 = num1 + score
            num2 = num2 + 1
        return num1 / num2
Exemple #3
0
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
Exemple #4
0
    def score_grouping(self, grouping: Grouping) -> float:
        """ Return a score for <grouping> calculated based on the answers of
        each student in each group in <grouping> to the questions in <self>.

        If there are no groups in <grouping> this score is 0.0. Otherwise, this
        score is determined using the following algorithm:

        1. For each group in <grouping>, get the score for the members of this
           group calculated based on their answers to the questions in this
           survey.
        2. Return the average of all the scores calculated in step 1.

        === Precondition ===
        All students in the groups in <grouping> have an answer to all questions
            in this survey
        """
        acc1 = 0
        acc2 = 0
        if grouping.__len__() == 0:
            return 0.0
        else:
            for group in grouping.get_groups():
                acc1 += self.score_students(group.get_members())
                acc2 += 1
        return acc1 / acc2
Exemple #5
0
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'])]
Exemple #6
0
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
Exemple #7
0
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)
Exemple #8
0
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
Exemple #9
0
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
Exemple #10
0
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']}
Exemple #11
0
    def score_grouping(self, grouping: Grouping) -> float:
        """ Return a score for <grouping> calculated based on the answers of
        each student in each group in <grouping> to the questions in <self>.

        If there are no groups in <grouping> this score is 0.0. Otherwise, this
        score is determined using the following algorithm:

        1. For each group in <grouping>, get the score for the members of this
           group calculated based on their answers to the questions in this
           survey.
        2. Return the average of all the scores calculated in step 1.

        === Precondition ===
        All students in the groups in <grouping> have an answer to all questions
            in this survey
        """
        # Get groups from grouping
        groups_to_score = grouping.get_groups()
        num_groups = len(groups_to_score)
        total_score = 0

        # If grouping comes back empty
        if len(groups_to_score) == 0:
            return 0.0

        # for each group
        for group_to_score in groups_to_score:
            students_to_score = group_to_score.get_members()
            total_score += self.score_students(students_to_score)

        average_score = total_score / num_groups
        return average_score
Exemple #12
0
def test_add_one_item():
    gr = Grouping()

    gr['key'] = 'value'

    assert len(gr) == 1
    assert gr['key'] == ['value']
Exemple #13
0
    def score_grouping(self, grouping: Grouping) -> float:
        """ Return a score for <grouping> calculated based on the answers of
        each student in each group in <grouping> to the questions in <self>.

        If there are no groups in <grouping> this score is 0.0. Otherwise, this
        score is determined using the following algorithm:

        1. For each group in <grouping>, get the score for the members of this
           group calculated based on their answers to the questions in this
           survey.
        2. Return the average of all the scores calculated in step 1.

        === Precondition ===
        All students in the groups in <grouping> have an answer to all questions
            in this survey
        """
        if len(grouping) == 0:
            return 0.0
        lst_of_group_scores = []
        for group in grouping.get_groups():
            lst_of_studs = group.get_members()
            # the students in each group
            score_of_group = self.score_students(lst_of_studs)
            # the score for the group
            lst_of_group_scores.append(score_of_group)
        try:
            return sum(lst_of_group_scores) / len(lst_of_group_scores)
        except ZeroDivisionError:
            return 0.0
Exemple #14
0
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']}
Exemple #15
0
 def score_grouping(self, grouping: Grouping) -> float:
     """ Return a score for <grouping> calculated based on the answers of
     each student in each group in <grouping> to the questions in <self>.
     If there are no groups in <grouping> this score is 0.0. Otherwise, this
     score is determined using the following algorithm:
     1. For each group in <grouping>, get the score for the members of this
        group calculated based on their answers to the questions in this
        survey.
     2. Return the average of all the scores calculated in step 1.
     === Precondition ===
     All students in the groups in <grouping> have an answer to all questions
         in this survey
     """
     if len(grouping) == 0:
         return 0.0
     groups = grouping.get_groups()
     score = 0.0
     num = 0.0
     for group in groups:
         students = group.get_members()
         score += self.score_students(students)
         num += 1.0
     if num == 0.0:
         return 0.0
     return score / num
Exemple #16
0
def get_member_ids(grouping: grouper.Grouping) -> Set[FrozenSet[int]]:
    member_ids = set()
    for group in grouping.get_groups():
        ids = []
        for member in group.get_members():
            ids.append(member.id)
        member_ids.add(frozenset(ids))
    return member_ids
Exemple #17
0
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']
Exemple #18
0
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']
Exemple #19
0
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']
Exemple #20
0
    def score_grouping(self, grouping: Grouping) -> float:
        """ Return a score for <grouping> calculated based on the answers of
        each student in each group in <grouping> to the questions in <self>.

        If there are no groups in <grouping> this score is 0.0. Otherwise, this
        score is determined using the following algorithm:

        1. For each group in <grouping>, get the score for the members of this
           group calculated based on their answers to the questions in this
           survey.
        2. Return the average of all the scores calculated in step 1.

        === Precondition ===
        All students in the groups in <grouping> have an answer to all questions
            in this survey
        """
        student_list = []
        # total score
        group_score = 0

        # check if grouping is empty
        if len(grouping.get_groups()) == 0:
            return 0

        # get average of all questions for each group
        try:
            for group in grouping.get_groups():
                # make a list of students in each group
                student_list = []
                for student in group.get_members():
                    student_list.append(student)
                group_score += self.score_students(student_list)

            return group_score / len(grouping.get_groups())

        except InvalidAnswerError:
            return 0
Exemple #21
0
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']
    }
Exemple #22
0
    def score_grouping(self, grouping: Grouping) -> float:
        """ Return a score for <grouping> calculated based on the answers of
        each student in each group in <grouping> to the questions in <self>.

        If there are no groups in <grouping> this score is 0.0. Otherwise, this
        score is determined using the following algorithm:

        1. For each group in <grouping>, get the score for the members of this
           group calculated based on their answers to the questions in this
           survey.
        2. Return the average of all the scores calculated in step 1.

        === Precondition ===
        All students in the groups in <grouping> have an answer to all questions
            in this survey
        """
        # TODO: complete the body of this method
        if grouping._groups == []:
            return 0

        scores = []
        for group in grouping.get_groups():
            scores.append(self.score_students(group.get_members()))
        return sum(scores) / len(scores)
Exemple #23
0
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]
Exemple #24
0
def test_grouper_grouping_to_string() -> None:
    """A test for __str__() in class Grouping."""
    grouping = Grouping()
    assert type(str(grouping)) is str
Exemple #25
0
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)
Exemple #26
0
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
Exemple #27
0
def grouping_to_list_of_list_flatten(grouping: grouper.Grouping):
    r = []
    for g in grouping.get_groups():
        r.extend([x.id for x in g.get_members()])
    r.sort()
    return r
Exemple #28
0
def grouping_to_list_of_list(grouping: grouper.Grouping):
    r = []
    for g in grouping.get_groups():
        r.append(sorted([x.id for x in g.get_members()]))
    r.sort()
    return r
Exemple #29
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']}
Exemple #30
0
def test_init_empty():
    gr = Grouping()
    assert len(gr) == 0