-
Notifications
You must be signed in to change notification settings - Fork 717
/
grading.py
73 lines (67 loc) · 2.83 KB
/
grading.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import requests
import json
class Grader(object):
def __init__(self, assignment_key, all_parts=()):
"""
Assignment key is the way to tell Coursera which problem is being submitted.
"""
self.submission_page = \
'https://www.coursera.org/api/onDemandProgrammingScriptSubmissions.v1'
self.assignment_key = assignment_key
self.answers = {part: None for part in all_parts}
def submit(self, email, token):
submission = {
"assignmentKey": self.assignment_key,
"submitterEmail": email,
"secret": token,
"parts": {}
}
for part, output in self.answers.items():
if output is not None:
submission["parts"][part] = {"output": output}
else:
submission["parts"][part] = dict()
request = requests.post(self.submission_page, data=json.dumps(submission))
response = request.json()
if request.status_code == 201:
print('Submitted to Coursera platform. See results on assignment page!')
elif u'details' in response and u'learnerMessage' in response[u'details']:
print(response[u'details'][u'learnerMessage'])
else:
print("Unknown response from Coursera: {}".format(request.status_code))
print(response)
def set_answer(self, part, answer):
"""Adds an answer for submission. Answer is expected either as string, number, or
an iterable of numbers.
Args:
part - str, assignment part id
answer - answer to submit. If non iterable, appends repr(answer). If string,
is appended as provided. If an iterable and not string, converted to
space-delimited repr() of members.
"""
if isinstance(answer, str):
self.answers[part] = answer
else:
try:
self.answers[part] = " ".join(map(repr, answer))
except TypeError:
self.answers[part] = repr(answer)
def array_to_grader(array, epsilon=1e-4):
"""Utility function to help preparing Coursera grading conditions descriptions.
Args:
array: iterable of numbers, the correct answers
epslion: the generated expression will accept the answers with this absolute difference with
provided values
Returns:
String. A Coursera grader expression that checks whether the user submission is in
(array - epsilon, array + epsilon)"""
res = []
for element in array:
if isinstance(element, int):
res.append("[{0}, {0}]".format(element))
else:
res.append("({0}, {1})".format(element - epsilon, element + epsilon))
return " ".join(res)