forked from yandexdataschool/Practical_RL
-
Notifications
You must be signed in to change notification settings - Fork 1
/
grading.py
75 lines (68 loc) · 2.91 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
74
75
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import requests
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://hub.coursera-apps.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()
response = requests.post(self.submission_page, data=json.dumps(submission))
if response.status_code == 201:
print('Submitted to Coursera platform. See results on assignment page!')
else:
d = response.json()
if d is not None and u'details' in d and u'learnerMessage' in d[u'details']:
print(d[u'details'][u'learnerMessage'])
print("Hint: try generating new token and make sure you spelled it correctly")
else:
print("Unknown response from Coursera: {}".format(response.status_code))
print(d)
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)