Commit d139a10c by Sven Marnach

Configure pylint and make it happy.

parent dbe54692
"""ActiveTable XBlock top-level package.
See activetable.activetable for more information.
"""
from .activetable import ActiveTableXBlock from .activetable import ActiveTableXBlock
...@@ -14,7 +14,7 @@ from xblockutils.studio_editable import StudioEditableXBlockMixin ...@@ -14,7 +14,7 @@ from xblockutils.studio_editable import StudioEditableXBlockMixin
from .cells import NumericCell from .cells import NumericCell
from .parsers import ParseError, parse_table, parse_number_list from .parsers import ParseError, parse_table, parse_number_list
loader = ResourceLoader(__name__) loader = ResourceLoader(__name__) # pylint: disable=invalid-name
class ActiveTableXBlock(StudioEditableXBlockMixin, XBlock): class ActiveTableXBlock(StudioEditableXBlockMixin, XBlock):
...@@ -22,7 +22,7 @@ class ActiveTableXBlock(StudioEditableXBlockMixin, XBlock): ...@@ -22,7 +22,7 @@ class ActiveTableXBlock(StudioEditableXBlockMixin, XBlock):
table_definition = String( table_definition = String(
display_name='Table definition', display_name='Table definition',
help='The definition of the table in Python-like syntax.', # TODO(smarnach): proper help help='The definition of the table in Python-like syntax.',
scope=Scope.content, scope=Scope.content,
multiline_editor=True, multiline_editor=True,
resettable_editor=False, resettable_editor=False,
...@@ -82,6 +82,8 @@ class ActiveTableXBlock(StudioEditableXBlockMixin, XBlock): ...@@ -82,6 +82,8 @@ class ActiveTableXBlock(StudioEditableXBlockMixin, XBlock):
self._column_widths = None self._column_widths = None
self._row_heights = None self._row_heights = None
self.response_cells = None self.response_cells = None
self.parse_fields()
self.postprocess_table()
def parse_fields(self): def parse_fields(self):
"""Parse the user-provided fields into more processing-friendly structured data.""" """Parse the user-provided fields into more processing-friendly structured data."""
...@@ -98,6 +100,12 @@ class ActiveTableXBlock(StudioEditableXBlockMixin, XBlock): ...@@ -98,6 +100,12 @@ class ActiveTableXBlock(StudioEditableXBlockMixin, XBlock):
self._row_heights = parse_number_list(self.row_heights) self._row_heights = parse_number_list(self.row_heights)
else: else:
self._row_heights = [36] * (len(self.tbody) + 1) self._row_heights = [36] * (len(self.tbody) + 1)
def postprocess_table(self):
"""Augment the parsed table definition with further information.
The additional information is taken from other content and student state fields.
"""
self.response_cells = {} self.response_cells = {}
for row, height in zip(self.tbody, self._row_heights[1:]): for row, height in zip(self.tbody, self._row_heights[1:]):
row['height'] = height row['height'] = height
...@@ -122,7 +130,6 @@ class ActiveTableXBlock(StudioEditableXBlockMixin, XBlock): ...@@ -122,7 +130,6 @@ class ActiveTableXBlock(StudioEditableXBlockMixin, XBlock):
def student_view(self, context=None): def student_view(self, context=None):
"""Render the table.""" """Render the table."""
self.parse_fields()
context = dict( context = dict(
help_text=self.help_text, help_text=self.help_text,
total_width=sum(self._column_widths) if self._column_widths else None, total_width=sum(self._column_widths) if self._column_widths else None,
...@@ -143,12 +150,11 @@ class ActiveTableXBlock(StudioEditableXBlockMixin, XBlock): ...@@ -143,12 +150,11 @@ class ActiveTableXBlock(StudioEditableXBlockMixin, XBlock):
return frag return frag
@XBlock.json_handler @XBlock.json_handler
def check_answers(self, data, suffix=''): def check_answers(self, data, unused_suffix=''):
"""Check the answers given by the student. """Check the answers given by the student.
This handler is called when the "Check" button is clicked. This handler is called when the "Check" button is clicked.
""" """
self.parse_fields()
correct_dict = { correct_dict = {
cell_id: self.response_cells[cell_id].check_response(value) cell_id: self.response_cells[cell_id].check_response(value)
for cell_id, value in data.iteritems() for cell_id, value in data.iteritems()
...@@ -166,6 +172,7 @@ class ActiveTableXBlock(StudioEditableXBlockMixin, XBlock): ...@@ -166,6 +172,7 @@ class ActiveTableXBlock(StudioEditableXBlockMixin, XBlock):
properties of this XBlock. properties of this XBlock.
""" """
def add_error(msg): def add_error(msg):
"""Add a validation error."""
validation.add(ValidationMessage(ValidationMessage.ERROR, msg)) validation.add(ValidationMessage(ValidationMessage.ERROR, msg))
try: try:
parse_table(data.table_definition) parse_table(data.table_definition)
......
...@@ -7,6 +7,8 @@ classes themselves. ...@@ -7,6 +7,8 @@ classes themselves.
""" """
from __future__ import absolute_import, division, unicode_literals from __future__ import absolute_import, division, unicode_literals
import decimal
class StaticCell(object): class StaticCell(object):
"""A static cell with a fixed value in the table body.""" """A static cell with a fixed value in the table body."""
...@@ -23,32 +25,34 @@ class NumericCell(object): ...@@ -23,32 +25,34 @@ class NumericCell(object):
is_static = False is_static = False
placeholder = 'numeric response' placeholder = 'numeric response'
def __init__(self, answer, tolerance=None, min_significant_digits=None, max_significant_digits=None): def __init__(
self, answer, tolerance=None, min_significant_digits=None, max_significant_digits=None
):
"""Set the correct answer and the allowed relative tolerance in percent.""" """Set the correct answer and the allowed relative tolerance in percent."""
self.answer = answer self.answer = answer
self.abs_tolerance = None
self.set_tolerance(tolerance) self.set_tolerance(tolerance)
self.min_significant_digits = min_significant_digits self.min_significant_digits = min_significant_digits
self.max_significant_digits = max_significant_digits self.max_significant_digits = max_significant_digits
def set_tolerance(self, tolerance): def set_tolerance(self, tolerance):
if tolerance is None: """Set the tolerance to the specified value, if it is not None."""
self.abs_tolerance = None if tolerance is not None:
else:
self.abs_tolerance = abs(self.answer) * tolerance / 100.0 self.abs_tolerance = abs(self.answer) * tolerance / 100.0
def check_response(self, student_response): def check_response(self, student_response):
"""Return a Boolean value indicating whether the student response is correct.""" """Return a Boolean value indicating whether the student response is correct."""
try: try:
r = float(student_response) value = float(student_response)
except ValueError: except ValueError:
return False return False
if self.min_significant_digits or self.max_significant_digits: if self.min_significant_digits or self.max_significant_digits:
d = len(decimal.Decimal(student_response).as_tuple().digits) digits = len(decimal.Decimal(student_response).as_tuple().digits) # pylint: disable=no-member
if self.min_significant_digits and d < self.min_significant_digits: if self.min_significant_digits and digits < self.min_significant_digits:
return False return False
if self.max_significant_digits and d > self.max_significant_digits: if self.max_significant_digits and digits > self.max_significant_digits:
return False return False
return abs(r - self.answer) <= self.abs_tolerance return abs(value - self.answer) <= self.abs_tolerance
class StringCell(object): class StringCell(object):
......
[REPORTS]
reports=no
[FORMAT]
max-line-length=100
[MESSAGES CONTROL]
disable=
I,
too-few-public-methods,
too-many-ancestors
[VARIABLES]
dummy-variables-rgx=_$|dummy|unused
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