Commit 7deb043e by Calen Pennington

Doing some stylistic cleanup on capa_module.py

parent a916f0ff
...@@ -17,14 +17,16 @@ log = logging.getLogger("mitx.courseware") ...@@ -17,14 +17,16 @@ log = logging.getLogger("mitx.courseware")
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
TIMEDELTA_REGEX = re.compile(r'^((?P<days>\d+?) day(?:s?))?(\s)?((?P<hours>\d+?) hour(?:s?))?(\s)?((?P<minutes>\d+?) minute(?:s)?)?(\s)?((?P<seconds>\d+?) second(?:s)?)?$') TIMEDELTA_REGEX = re.compile(r'^((?P<days>\d+?) day(?:s?))?(\s)?((?P<hours>\d+?) hour(?:s?))?(\s)?((?P<minutes>\d+?) minute(?:s)?)?(\s)?((?P<seconds>\d+?) second(?:s)?)?$')
def item(l, default="", process=lambda x:x):
if len(l)==0: def item(l, default="", process=lambda x: x):
if len(l) == 0:
return default return default
elif len(l)==1: elif len(l) == 1:
return process(l[0]) return process(l[0])
else: else:
raise Exception('Malformed XML') raise Exception('Malformed XML')
def parse_timedelta(time_str): def parse_timedelta(time_str):
parts = TIMEDELTA_REGEX.match(time_str) parts = TIMEDELTA_REGEX.match(time_str)
if not parts: if not parts:
...@@ -36,15 +38,18 @@ def parse_timedelta(time_str): ...@@ -36,15 +38,18 @@ def parse_timedelta(time_str):
time_params[name] = int(param) time_params[name] = int(param)
return timedelta(**time_params) return timedelta(**time_params)
class ComplexEncoder(json.JSONEncoder): class ComplexEncoder(json.JSONEncoder):
def default(self, obj): def default(self, obj):
if isinstance(obj, complex): if isinstance(obj, complex):
return "{real:.7g}{imag:+.7g}*j".format(real = obj.real,imag = obj.imag) return "{real:.7g}{imag:+.7g}*j".format(real=obj.real, imag=obj.imag)
return json.JSONEncoder.default(self, obj) return json.JSONEncoder.default(self, obj)
class ModuleDescriptor(XModuleDescriptor): class ModuleDescriptor(XModuleDescriptor):
pass pass
class Module(XModule): class Module(XModule):
''' Interface between capa_problem and x_module. Originally a hack ''' Interface between capa_problem and x_module. Originally a hack
meant to be refactored out, but it seems to be serving a useful meant to be refactored out, but it seems to be serving a useful
...@@ -77,8 +82,8 @@ class Module(XModule): ...@@ -77,8 +82,8 @@ class Module(XModule):
def get_problem_html(self, encapsulate=True): def get_problem_html(self, encapsulate=True):
html = self.lcp.get_html() html = self.lcp.get_html()
content={'name':self.name, content = {'name': self.name,
'html':html, 'html': html,
'weight': self.weight, 'weight': self.weight,
} }
...@@ -95,7 +100,6 @@ class Module(XModule): ...@@ -95,7 +100,6 @@ class Module(XModule):
reset_button = False reset_button = False
save_button = False save_button = False
# User submitted a problem, and hasn't reset. We don't want # User submitted a problem, and hasn't reset. We don't want
# more submissions. # more submissions.
if self.lcp.done and self.rerandomize == "always": if self.lcp.done and self.rerandomize == "always":
...@@ -115,22 +119,22 @@ class Module(XModule): ...@@ -115,22 +119,22 @@ class Module(XModule):
save_button = False save_button = False
# Check if explanation is available, and if so, give a link # Check if explanation is available, and if so, give a link
explain="" explain = ""
if self.lcp.done and self.explain_available=='attempted': if self.lcp.done and self.explain_available == 'attempted':
explain=self.explanation explain = self.explanation
if self.closed() and self.explain_available=='closed': if self.closed() and self.explain_available == 'closed':
explain=self.explanation explain = self.explanation
if len(explain) == 0: if len(explain) == 0:
explain = False explain = False
context = {'problem' : content, context = {'problem': content,
'id' : self.item_id, 'id': self.item_id,
'check_button' : check_button, 'check_button': check_button,
'reset_button' : reset_button, 'reset_button': reset_button,
'save_button' : save_button, 'save_button': save_button,
'answer_available' : self.answer_available(), 'answer_available': self.answer_available(),
'ajax_url' : self.ajax_url, 'ajax_url': self.ajax_url,
'attempts_used': self.attempts, 'attempts_used': self.attempts,
'attempts_allowed': self.max_attempts, 'attempts_allowed': self.max_attempts,
'explain': explain, 'explain': explain,
...@@ -138,7 +142,7 @@ class Module(XModule): ...@@ -138,7 +142,7 @@ class Module(XModule):
html = self.system.render_template('problem.html', context) html = self.system.render_template('problem.html', context)
if encapsulate: if encapsulate:
html = '<div id="problem_{id}" class="problem" data-url="{ajax_url}">'.format(id=self.item_id,ajax_url=self.ajax_url)+html+"</div>" html = '<div id="problem_{id}" class="problem" data-url="{ajax_url}">'.format(id=self.item_id, ajax_url=self.ajax_url) + html + "</div>"
return html return html
...@@ -150,20 +154,19 @@ class Module(XModule): ...@@ -150,20 +154,19 @@ class Module(XModule):
dom2 = etree.fromstring(xml) dom2 = etree.fromstring(xml)
self.explanation="problems/"+item(dom2.xpath('/problem/@explain'), default="closed") self.explanation = "problems/" + item(dom2.xpath('/problem/@explain'), default="closed")
# TODO: Should be converted to: self.explanation=item(dom2.xpath('/problem/@explain'), default="closed") # TODO: Should be converted to: self.explanation=item(dom2.xpath('/problem/@explain'), default="closed")
self.explain_available=item(dom2.xpath('/problem/@explain_available')) self.explain_available = item(dom2.xpath('/problem/@explain_available'))
display_due_date_string=item(dom2.xpath('/problem/@due')) display_due_date_string = item(dom2.xpath('/problem/@due'))
if len(display_due_date_string)>0: if len(display_due_date_string) > 0:
self.display_due_date=dateutil.parser.parse(display_due_date_string) self.display_due_date = dateutil.parser.parse(display_due_date_string)
#log.debug("Parsed " + display_due_date_string + " to " + str(self.display_due_date)) #log.debug("Parsed " + display_due_date_string + " to " + str(self.display_due_date))
else: else:
self.display_due_date=None self.display_due_date = None
grace_period_string = item(dom2.xpath('/problem/@graceperiod')) grace_period_string = item(dom2.xpath('/problem/@graceperiod'))
if len(grace_period_string)>0 and self.display_due_date: if len(grace_period_string) >0 and self.display_due_date:
self.grace_period = parse_timedelta(grace_period_string) self.grace_period = parse_timedelta(grace_period_string)
self.close_date = self.display_due_date + self.grace_period self.close_date = self.display_due_date + self.grace_period
#log.debug("Then parsed " + grace_period_string + " to closing date" + str(self.close_date)) #log.debug("Then parsed " + grace_period_string + " to closing date" + str(self.close_date))
...@@ -171,19 +174,19 @@ class Module(XModule): ...@@ -171,19 +174,19 @@ class Module(XModule):
self.grace_period = None self.grace_period = None
self.close_date = self.display_due_date self.close_date = self.display_due_date
self.max_attempts=item(dom2.xpath('/problem/@attempts')) self.max_attempts =item(dom2.xpath('/problem/@attempts'))
if len(self.max_attempts)>0: if len(self.max_attempts)>0:
self.max_attempts=int(self.max_attempts) self.max_attempts =int(self.max_attempts)
else: else:
self.max_attempts=None self.max_attempts =None
self.show_answer=item(dom2.xpath('/problem/@showanswer')) self.show_answer =item(dom2.xpath('/problem/@showanswer'))
if self.show_answer=="": if self.show_answer =="":
self.show_answer="closed" self.show_answer ="closed"
self.rerandomize=item(dom2.xpath('/problem/@rerandomize')) self.rerandomize =item(dom2.xpath('/problem/@rerandomize'))
if self.rerandomize=="" or self.rerandomize=="always" or self.rerandomize=="true": if self.rerandomize =="" or self.rerandomize=="always" or self.rerandomize=="true":
self.rerandomize="always" self.rerandomize="always"
elif self.rerandomize=="false" or self.rerandomize=="per_student": elif self.rerandomize=="false" or self.rerandomize=="per_student":
self.rerandomize="per_student" self.rerandomize="per_student"
......
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