From 2b9d78dfd5d939687c5310252734b3f6feda94be Mon Sep 17 00:00:00 2001 From: Peter Baratta Date: Mon, 13 May 2013 10:18:19 -0400 Subject: [PATCH] Pep8 fixes and changes to NaN tests --- common/lib/capa/capa/responsetypes.py | 3 ++- common/lib/capa/capa/tests/test_responsetypes.py | 13 +++++++++---- common/lib/capa/capa/util.py | 7 +++++-- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/common/lib/capa/capa/responsetypes.py b/common/lib/capa/capa/responsetypes.py index f4f5d854a9..65e903b576 100644 --- a/common/lib/capa/capa/responsetypes.py +++ b/common/lib/capa/capa/responsetypes.py @@ -2,7 +2,8 @@ # File: courseware/capa/responsetypes.py # ''' -Problem response evaluation. Handles checking of student responses, of a variety of types. +Problem response evaluation. Handles checking of student responses, +of a variety of types. Used by capa_problem.py ''' diff --git a/common/lib/capa/capa/tests/test_responsetypes.py b/common/lib/capa/capa/tests/test_responsetypes.py index f7848ca094..554df1cde6 100644 --- a/common/lib/capa/capa/tests/test_responsetypes.py +++ b/common/lib/capa/capa/tests/test_responsetypes.py @@ -442,7 +442,7 @@ class FormulaResponseTest(ResponseTest): # This resolves a bug where a problem with relative tolerance would # pass with any arbitrarily large student answer. - sample_dict = {'x' : (1,2)} + sample_dict = {'x': (1, 2)} # Test problem problem = self.build_problem(sample_dict=sample_dict, @@ -457,7 +457,7 @@ class FormulaResponseTest(ResponseTest): # attempt to produce a value which causes the student's answer to be # evaluated to nan. See if this is resolved correctly. - sample_dict = {'x' : (1,2)} + sample_dict = {'x': (1, 2)} # Test problem problem = self.build_problem(sample_dict=sample_dict, @@ -465,7 +465,11 @@ class FormulaResponseTest(ResponseTest): tolerance="1%", answer="x") # Expect an incorrect answer (+ nan) to be marked incorrect - input_formula = "10*x + 0*1e999" # right now this evaluates to 'nan' for a given x + # right now this evaluates to 'nan' for a given x (Python implementation-dependent) + input_formula = "10*x + 0*1e999" + self.assert_grade(problem, input_formula, "incorrect") + # Expect an correct answer (+ nan) to be marked incorrect + input_formula = "x + 0*1e999" self.assert_grade(problem, input_formula, "incorrect") @@ -763,7 +767,8 @@ class NumericalResponseTest(ResponseTest): answer=4, tolerance="10%") correct_responses = [] - incorrect_responses = ["0*1e999"] # right now this evaluates to 'nan' for a given x + # right now these evaluate to 'nan' + incorrect_responses = ["0*1e999", "4 + 0*1e999"] self.assert_multiple_grade(problem, correct_responses, incorrect_responses) def test_grade_with_script(self): diff --git a/common/lib/capa/capa/util.py b/common/lib/capa/capa/util.py index c219a7b5f6..4663b388c2 100644 --- a/common/lib/capa/capa/util.py +++ b/common/lib/capa/capa/util.py @@ -23,10 +23,12 @@ def compare_with_tolerance(v1, v2, tol): tolerance = evaluator(dict(), dict(), tol) if isinf(v1) or isinf(v2): - return v1 == v2 # because the other numerical comparison does not work with infinities + # because the other numerical comparison does not work with infinities + return v1 == v2 else: return abs(v1 - v2) <= tolerance + def contextualize_text(text, context): # private ''' Takes a string with variables. E.g. $a+$b. Does a substitution of those variables from the context ''' @@ -55,7 +57,8 @@ def convert_files_to_filenames(answers): new_answers = dict() for answer_id in answers.keys(): answer = answers[answer_id] - if is_list_of_files(answer): # Files are stored as a list, even if one file + # Files are stored as a list, even if one file + if is_list_of_files(answer): new_answers[answer_id] = [f.name for f in answer] else: new_answers[answer_id] = answers[answer_id]