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
......@@ -14,7 +14,7 @@ from xblockutils.studio_editable import StudioEditableXBlockMixin
from .cells import NumericCell
from .parsers import ParseError, parse_table, parse_number_list
loader = ResourceLoader(__name__)
loader = ResourceLoader(__name__) # pylint: disable=invalid-name
class ActiveTableXBlock(StudioEditableXBlockMixin, XBlock):
......@@ -22,7 +22,7 @@ class ActiveTableXBlock(StudioEditableXBlockMixin, XBlock):
table_definition = String(
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,
multiline_editor=True,
resettable_editor=False,
......@@ -82,6 +82,8 @@ class ActiveTableXBlock(StudioEditableXBlockMixin, XBlock):
self._column_widths = None
self._row_heights = None
self.response_cells = None
self.parse_fields()
self.postprocess_table()
def parse_fields(self):
"""Parse the user-provided fields into more processing-friendly structured data."""
......@@ -98,6 +100,12 @@ class ActiveTableXBlock(StudioEditableXBlockMixin, XBlock):
self._row_heights = parse_number_list(self.row_heights)
else:
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 = {}
for row, height in zip(self.tbody, self._row_heights[1:]):
row['height'] = height
......@@ -122,7 +130,6 @@ class ActiveTableXBlock(StudioEditableXBlockMixin, XBlock):
def student_view(self, context=None):
"""Render the table."""
self.parse_fields()
context = dict(
help_text=self.help_text,
total_width=sum(self._column_widths) if self._column_widths else None,
......@@ -143,12 +150,11 @@ class ActiveTableXBlock(StudioEditableXBlockMixin, XBlock):
return frag
@XBlock.json_handler
def check_answers(self, data, suffix=''):
def check_answers(self, data, unused_suffix=''):
"""Check the answers given by the student.
This handler is called when the "Check" button is clicked.
"""
self.parse_fields()
correct_dict = {
cell_id: self.response_cells[cell_id].check_response(value)
for cell_id, value in data.iteritems()
......@@ -166,6 +172,7 @@ class ActiveTableXBlock(StudioEditableXBlockMixin, XBlock):
properties of this XBlock.
"""
def add_error(msg):
"""Add a validation error."""
validation.add(ValidationMessage(ValidationMessage.ERROR, msg))
try:
parse_table(data.table_definition)
......
......@@ -7,6 +7,8 @@ classes themselves.
"""
from __future__ import absolute_import, division, unicode_literals
import decimal
class StaticCell(object):
"""A static cell with a fixed value in the table body."""
......@@ -23,32 +25,34 @@ class NumericCell(object):
is_static = False
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."""
self.answer = answer
self.abs_tolerance = None
self.set_tolerance(tolerance)
self.min_significant_digits = min_significant_digits
self.max_significant_digits = max_significant_digits
def set_tolerance(self, tolerance):
if tolerance is None:
self.abs_tolerance = None
else:
"""Set the tolerance to the specified value, if it is not None."""
if tolerance is not None:
self.abs_tolerance = abs(self.answer) * tolerance / 100.0
def check_response(self, student_response):
"""Return a Boolean value indicating whether the student response is correct."""
try:
r = float(student_response)
value = float(student_response)
except ValueError:
return False
if self.min_significant_digits or self.max_significant_digits:
d = len(decimal.Decimal(student_response).as_tuple().digits)
if self.min_significant_digits and d < self.min_significant_digits:
digits = len(decimal.Decimal(student_response).as_tuple().digits) # pylint: disable=no-member
if self.min_significant_digits and digits < self.min_significant_digits:
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 abs(r - self.answer) <= self.abs_tolerance
return abs(value - self.answer) <= self.abs_tolerance
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