diff --git a/Exercise1/utils.py b/Exercise1/utils.py index d0c909d..b92e3cf 100755 --- a/Exercise1/utils.py +++ b/Exercise1/utils.py @@ -19,7 +19,9 @@ class Grader(SubmissionBase): 'Computing Cost (for multiple variables)', 'Gradient Descent (for multiple variables)', 'Normal Equations'] - super().__init__('linear-regression', part_names) + part_names_key = ['DCRbJ', 'BGa4S', 'b65eO', 'BbS8u', 'FBlE2', 'RZAZC', '7m5Eu'] + assignment_key = 'UkTlA-FyRRKV5ooohuwU6A' + super().__init__('linear-regression', assignment_key, part_names, part_names_key) def __iter__(self): for part_id in range(1, 8): diff --git a/Exercise2/utils.py b/Exercise2/utils.py index 7c52dbe..8e5e6a9 100755 --- a/Exercise2/utils.py +++ b/Exercise2/utils.py @@ -119,7 +119,9 @@ class Grader(SubmissionBase): 'Predict', 'Regularized Logistic Regression Cost', 'Regularized Logistic Regression Gradient'] - super().__init__('logistic-regression', part_names) + part_names_key = ['sFxIn', 'yvXBE', 'HerlY', '9fxV6', 'OddeL', 'aUo3H'] + assignment_key = 'JvOPouj-S-ys8KjYcPYqrg' + super().__init__('logistic-regression', assignment_key, part_names, part_names_key) def __iter__(self): for part_id in range(1, 7): diff --git a/Exercise3/utils.py b/Exercise3/utils.py index 633a563..4bf9971 100755 --- a/Exercise3/utils.py +++ b/Exercise3/utils.py @@ -79,8 +79,9 @@ class Grader(SubmissionBase): 'One-vs-All Classifier Training', 'One-vs-All Classifier Prediction', 'Neural Network Prediction Function'] - - super().__init__('multi-class-classification-and-neural-networks', part_names) + part_names_key = ['jzAIf', 'LjDnh', '3yxcY', 'yNspP'] + assignment_key = '2KZRbGlpQnyzVI8Ki4uXjw' + super().__init__('multi-class-classification-and-neural-networks', assignment_key, part_names, part_names_key) def __iter__(self): for part_id in range(1, 5): diff --git a/Exercise4/utils.py b/Exercise4/utils.py index 6b7c3bd..6d18b86 100755 --- a/Exercise4/utils.py +++ b/Exercise4/utils.py @@ -193,7 +193,9 @@ class Grader(SubmissionBase): 'Sigmoid Gradient', 'Neural Network Gradient (Backpropagation)', 'Regularized Gradient'] - super().__init__('neural-network-learning', part_names) + part_names_key = ['aAiP2', '8ajiz', 'rXsEO', 'TvZch', 'pfIYT'] + assignment_key = 'xolSVXukR72JH37bfzo0pg' + super().__init__('neural-network-learning', assignment_key, part_names, part_names_key) def __iter__(self): for part_id in range(1, 6): diff --git a/Exercise5/utils.py b/Exercise5/utils.py index b2340ad..da8b9be 100755 --- a/Exercise5/utils.py +++ b/Exercise5/utils.py @@ -138,7 +138,9 @@ class Grader(SubmissionBase): 'Learning Curve', 'Polynomial Feature Mapping', 'Validation Curve'] - super().__init__('regularized-linear-regression-and-bias-variance', part_names) + part_names_key = ['a6bvf', 'x4FhA', 'n3zWY', 'lLaa4', 'gyJbG'] + assignment_key = '-wEfetVmQgG3j-mtasztYg' + super().__init__('regularized-linear-regression-and-bias-variance', assignment_key, part_names, part_names_key) def __iter__(self): for part_id in range(1, 6): diff --git a/Exercise6/utils.py b/Exercise6/utils.py index 6d99cec..4ff2c2a 100755 --- a/Exercise6/utils.py +++ b/Exercise6/utils.py @@ -695,7 +695,9 @@ class Grader(SubmissionBase): 'Parameters (C, sigma) for Dataset 3', 'Email Processing', 'Email Feature Extraction'] - super().__init__('support-vector-machines', part_names) + part_names_key = ['drOLk', 'JYt9Q', 'UHwLk', 'RIiFh'] + assignment_key = 'xHfBJWXxTdKXrUG7dHTQ3g' + super().__init__('support-vector-machines', assignment_key, part_names, part_names_key) def __iter__(self): for part_id in range(1, 5): diff --git a/Exercise7/utils.py b/Exercise7/utils.py index 8110917..668802b 100755 --- a/Exercise7/utils.py +++ b/Exercise7/utils.py @@ -211,7 +211,9 @@ class Grader(SubmissionBase): 'PCA', 'Project Data (PCA)', 'Recover Data (PCA)'] - super().__init__('k-means-clustering-and-pca', part_names) + part_names_key = ['7yN0U', 'G1WGM', 'ixOMV', 'AFoJK', 'vf9EL'] + assignment_key = 'rGGTuM9gQoaikOnlhLII1A' + super().__init__('k-means-clustering-and-pca', assignment_key, part_names, part_names_key) def __iter__(self): for part_id in range(1, 6): diff --git a/Exercise8/utils.py b/Exercise8/utils.py index 938a7bb..762b752 100755 --- a/Exercise8/utils.py +++ b/Exercise8/utils.py @@ -235,7 +235,9 @@ class Grader(SubmissionBase): 'Collaborative Filtering Gradient', 'Regularized Cost', 'Regularized Gradient'] - super().__init__('anomaly-detection-and-recommender-systems', part_names) + part_names_key = ['WGzrg', '80Tcg', 'KDzSh', 'wZud3', 'BP3th', 'YF0u1'] + assignment_key = 'JvOPouj-S-ys8KjYcPYqrg' + super().__init__('anomaly-detection-and-recommender-systems', assignment_key, part_names, part_names_key) def __iter__(self): for part_id in range(1, 7): diff --git a/submission.py b/submission.py index 10113e4..135b19f 100755 --- a/submission.py +++ b/submission.py @@ -1,21 +1,21 @@ -from urllib.parse import urlencode -from urllib.request import urlopen -import pickle import json -from collections import OrderedDict -import numpy as np import os +import pickle +from collections import OrderedDict + +import numpy as np +import requests class SubmissionBase: - - submit_url = 'https://www-origin.coursera.org/api/' \ - 'onDemandProgrammingImmediateFormSubmissions.v1' + submit_url = 'https://www.coursera.org/api/onDemandProgrammingScriptSubmissions.v1?includes=evaluation' save_file = 'token.pkl' - def __init__(self, assignment_slug, part_names): + def __init__(self, assignment_slug, assignment_key, part_names, part_names_key): self.assignment_slug = assignment_slug + self.assignment_key = assignment_key self.part_names = part_names + self.part_names_key = part_names_key self.login = None self.token = None self.functions = OrderedDict() @@ -28,24 +28,25 @@ class SubmissionBase: # Evaluate the different parts of exercise parts = OrderedDict() for part_id, result in self: - parts[str(part_id)] = {'output': sprintf('%0.5f ', result)} - result, response = self.request(parts) + parts[self.part_names_key[part_id - 1]] = {'output': sprintf('%0.5f ', result)} + response = self.request(parts) response = json.loads(response.decode("utf-8")) # if an error was returned, print it and stop - if 'errorMessage' in response: - print(response['errorMessage']) + if 'errorCode' in response: + print(response['message'], response['details']['learnerMessage']) return # Print the grading table print('%43s | %9s | %-s' % ('Part Name', 'Score', 'Feedback')) print('%43s | %9s | %-s' % ('---------', '-----', '--------')) - for part in parts: - part_feedback = response['partFeedbacks'][part] - part_evaluation = response['partEvaluations'][part] + for index, part in enumerate(parts): + part_feedback = response['linked']['onDemandProgrammingScriptEvaluations.v1'][0]['parts'][str(part)][ + 'feedback'] + part_evaluation = response['linked']['onDemandProgrammingScriptEvaluations.v1'][0]['parts'][str(part)] score = '%d / %3d' % (part_evaluation['score'], part_evaluation['maxScore']) - print('%43s | %9s | %-s' % (self.part_names[int(part) - 1], score, part_feedback)) - evaluation = response['evaluation'] + print('%43s | %9s | %-s' % (self.part_names[int(index) - 1], score, part_feedback)) + evaluation = response['linked']['onDemandProgrammingScriptEvaluations.v1'][0] total_score = '%d / %d' % (evaluation['score'], evaluation['maxScore']) print(' --------------------------------') print('%43s | %9s | %-s\n' % (' ', total_score, ' ')) @@ -71,18 +72,15 @@ class SubmissionBase: pickle.dump((self.login, self.token), f) def request(self, parts): - params = { - 'assignmentSlug': self.assignment_slug, + payload = { + 'assignmentKey': self.assignment_key, + 'submitterEmail': self.login, 'secret': self.token, - 'parts': parts, - 'submitterEmail': self.login} + 'parts': dict(eval(str(parts)))} + headers = {} - params = urlencode({'jsonBody': json.dumps(params)}).encode("utf-8") - f = urlopen(self.submit_url, params) - try: - return 0, f.read() - finally: - f.close() + r = requests.post(self.submit_url, data=json.dumps(payload), headers=headers) + return r.content def __iter__(self): for part_id in self.functions: