Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extract grade from list #28

Merged
merged 5 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions gradefetcher/gradefetcher.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os
import urllib.parse
from operator import truediv

import pkg_resources
import requests
Expand All @@ -20,6 +21,16 @@
loader = ResourceLoader(__name__)


def grade_from_list(grades):
"""take a list of integers and calculate grade from them"""
if len(grades) > 1:
total_grade = sum(grades)
grade = int(truediv(total_grade * 100, len(grades)))
else:
grade = grades[0] * 100
return grade


@XBlock.needs("i18n", "user")
@XBlock.wants("settings")
class GradeFetcherXBlock(XBlock, StudioEditableXBlockMixin):
Expand Down Expand Up @@ -399,15 +410,7 @@ def grade_user(self, data, suffix=""):
for result in grader_response.json()["results"]:
if "grade" in result:
grades.append(result["grade"])
if len(grades) > 1:
from operator import truediv

total_grade = 0
for g in grades:
total_grade += g
grade = int(truediv(total_grade * 100, len(grades)))
else:
grade = grades[0] * 100
grade = grade_from_list(grades)
reasons = []
for result in grader_response.json()["results"]:
if "grade" in result:
Expand Down
13 changes: 13 additions & 0 deletions gradefetcher/tests/test_gradefetcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import unittest

from gradefetcher.gradefetcher import grade_from_list


class GradeFetcherHelperTests(unittest.TestCase):
def test_grade_from_list(self):
assert grade_from_list([0]) == 0
assert grade_from_list([1]) == 100
assert grade_from_list([2]) == 200
assert grade_from_list([0, 0]) == 0
assert grade_from_list([1, 1]) == 100
assert grade_from_list([1, 0]) == 50