Commit 00f966a9 by Alexander Kryklia

populate -> __init__

parent a95e8098
...@@ -101,11 +101,6 @@ class PositionsCompare(list): ...@@ -101,11 +101,6 @@ class PositionsCompare(list):
class DragAndDrop(object): class DragAndDrop(object):
""" Grader class for drag and drop inputtype. """ Grader class for drag and drop inputtype.
""" """
def __init__(self):
self.correct_groups = dict() # correct groups from xml
self.correct_positions = dict() # correct positions for comparing
self.user_groups = dict() # will be populated from user answer
self.user_positions = dict() # will be populated from user answer
def grade(self): def grade(self):
''' Grader user answer. ''' Grader user answer.
...@@ -243,7 +238,7 @@ class DragAndDrop(object): ...@@ -243,7 +238,7 @@ class DragAndDrop(object):
return True return True
def populate(self, correct_answer, user_answer): def __init__(self, correct_answer, user_answer):
""" Populates DragAndDrop variables from user_answer and correct_answer. """ Populates DragAndDrop variables from user_answer and correct_answer.
If correct_answer is dict, converts it to list. If correct_answer is dict, converts it to list.
Correct answer in dict form is simpe structure for fast and simple Correct answer in dict form is simpe structure for fast and simple
...@@ -289,10 +284,13 @@ class DragAndDrop(object): ...@@ -289,10 +284,13 @@ class DragAndDrop(object):
Args: Args:
user_answer: json user_answer: json
correct_answer: dict or list correct_answer: dict or list
"""
Returns: None self.correct_groups = dict() # correct groups from xml
self.correct_positions = dict() # correct positions for comparing
self.user_groups = dict() # will be populated from user answer
self.user_positions = dict() # will be populated from user answer
"""
# convert from dict answer format to list format # convert from dict answer format to list format
if isinstance(correct_answer, dict): if isinstance(correct_answer, dict):
tmp = [] tmp = []
...@@ -330,8 +328,8 @@ class DragAndDrop(object): ...@@ -330,8 +328,8 @@ class DragAndDrop(object):
def grade(user_input, correct_answer): def grade(user_input, correct_answer):
""" Populates DragAndDrop instance from user_input and correct_answer and """ Creates DragAndDrop instance from user_input and correct_answer and
calls DragAndDrop.drade for grading. calls DragAndDrop.grade for grading.
Supports two interfaces for correct_answer: dict and list. Supports two interfaces for correct_answer: dict and list.
...@@ -373,6 +371,5 @@ def grade(user_input, correct_answer): ...@@ -373,6 +371,5 @@ def grade(user_input, correct_answer):
Returns: bool Returns: bool
""" """
dnd = DragAndDrop() return DragAndDrop(correct_answer=correct_answer,
dnd.populate(correct_answer=correct_answer, user_answer=user_input) user_answer=user_input).grade()
return dnd.grade()
...@@ -520,11 +520,10 @@ class Test_DragAndDrop_Grade(unittest.TestCase): ...@@ -520,11 +520,10 @@ class Test_DragAndDrop_Grade(unittest.TestCase):
class Test_DragAndDrop_Populate(unittest.TestCase): class Test_DragAndDrop_Populate(unittest.TestCase):
def test_1(self): def test_1(self):
dnd = draganddrop.DragAndDrop()
correct_answer = {'1': [[40, 10], 29], 'name_with_icon': [20, 20]} correct_answer = {'1': [[40, 10], 29], 'name_with_icon': [20, 20]}
user_input = '{"draggables": \ user_input = '{"draggables": \
[{"1": [10, 10]}, {"name_with_icon": [20, 20]}]}' [{"1": [10, 10]}, {"name_with_icon": [20, 20]}]}'
dnd.populate(correct_answer, user_input) dnd = draganddrop.DragAndDrop(correct_answer, user_input)
correct_groups = {'1': ['name_with_icon'], '0': ['1']} correct_groups = {'1': ['name_with_icon'], '0': ['1']}
correct_positions = {'1': {'exact': [[20, 20]]}, '0': {'exact': [[[40, 10], 29]]}} correct_positions = {'1': {'exact': [[20, 20]]}, '0': {'exact': [[[40, 10], 29]]}}
...@@ -540,49 +539,49 @@ class Test_DragAndDrop_Populate(unittest.TestCase): ...@@ -540,49 +539,49 @@ class Test_DragAndDrop_Populate(unittest.TestCase):
class Test_DraAndDrop_Compare_Positions(unittest.TestCase): class Test_DraAndDrop_Compare_Positions(unittest.TestCase):
def test_1(self): def test_1(self):
dnd = draganddrop.DragAndDrop() dnd = draganddrop.DragAndDrop({'1': 't1'}, '{"draggables": [{"1": "t1"}]}')
self.assertTrue(dnd.compare_positions(correct=[[1, 1], [2, 3]], self.assertTrue(dnd.compare_positions(correct=[[1, 1], [2, 3]],
user=[[2, 3], [1, 1]], user=[[2, 3], [1, 1]],
flag='anyof')) flag='anyof'))
def test_2a(self): def test_2a(self):
dnd = draganddrop.DragAndDrop() dnd = draganddrop.DragAndDrop({'1': 't1'}, '{"draggables": [{"1": "t1"}]}')
self.assertTrue(dnd.compare_positions(correct=[[1, 1], [2, 3]], self.assertTrue(dnd.compare_positions(correct=[[1, 1], [2, 3]],
user=[[2, 3], [1, 1]], user=[[2, 3], [1, 1]],
flag='exact')) flag='exact'))
def test_2b(self): def test_2b(self):
dnd = draganddrop.DragAndDrop() dnd = draganddrop.DragAndDrop({'1': 't1'}, '{"draggables": [{"1": "t1"}]}')
self.assertFalse(dnd.compare_positions(correct=[[1, 1], [2, 3]], self.assertFalse(dnd.compare_positions(correct=[[1, 1], [2, 3]],
user=[[2, 13], [1, 1]], user=[[2, 13], [1, 1]],
flag='exact')) flag='exact'))
def test_3(self): def test_3(self):
dnd = draganddrop.DragAndDrop() dnd = draganddrop.DragAndDrop({'1': 't1'}, '{"draggables": [{"1": "t1"}]}')
self.assertFalse(dnd.compare_positions(correct=["a", "b"], self.assertFalse(dnd.compare_positions(correct=["a", "b"],
user=["a", "b", "c"], user=["a", "b", "c"],
flag='anyof')) flag='anyof'))
def test_4(self): def test_4(self):
dnd = draganddrop.DragAndDrop() dnd = draganddrop.DragAndDrop({'1': 't1'}, '{"draggables": [{"1": "t1"}]}')
self.assertTrue(dnd.compare_positions(correct=["a", "b", "c"], self.assertTrue(dnd.compare_positions(correct=["a", "b", "c"],
user=["a", "b"], user=["a", "b"],
flag='anyof')) flag='anyof'))
def test_5(self): def test_5(self):
dnd = draganddrop.DragAndDrop() dnd = draganddrop.DragAndDrop({'1': 't1'}, '{"draggables": [{"1": "t1"}]}')
self.assertFalse(dnd.compare_positions(correct=["a", "b", "c"], self.assertFalse(dnd.compare_positions(correct=["a", "b", "c"],
user=["a", "c", "b"], user=["a", "c", "b"],
flag='exact')) flag='exact'))
def test_6(self): def test_6(self):
dnd = draganddrop.DragAndDrop() dnd = draganddrop.DragAndDrop({'1': 't1'}, '{"draggables": [{"1": "t1"}]}')
self.assertTrue(dnd.compare_positions(correct=["a", "b", "c"], self.assertTrue(dnd.compare_positions(correct=["a", "b", "c"],
user=["a", "c", "b"], user=["a", "c", "b"],
flag='anyof')) flag='anyof'))
def test_7(self): def test_7(self):
dnd = draganddrop.DragAndDrop() dnd = draganddrop.DragAndDrop({'1': 't1'}, '{"draggables": [{"1": "t1"}]}')
self.assertFalse(dnd.compare_positions(correct=["a", "b", "b"], self.assertFalse(dnd.compare_positions(correct=["a", "b", "b"],
user=["a", "c", "b"], user=["a", "c", "b"],
flag='anyof')) flag='anyof'))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment