Commit 770bc4c3 by Calen Pennington

Renaming item to only_one, and adding documentation

parent 84df2678
...@@ -18,7 +18,12 @@ log = logging.getLogger("mitx.courseware") ...@@ -18,7 +18,12 @@ 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): def only_one(lst, default="", process=lambda x: x):
"""
If lst is empty, returns default
If lst has a single element, applies process to that element and returns it
Otherwise, raises an exeception
"""
if len(l) == 0: if len(l) == 0:
return default return default
elif len(l) == 1: elif len(l) == 1:
...@@ -163,18 +168,18 @@ class Module(XModule): ...@@ -163,18 +168,18 @@ class Module(XModule):
dom2 = etree.fromstring(xml) dom2 = etree.fromstring(xml)
self.explanation = "problems/" + item(dom2.xpath('/problem/@explain'), default="closed") self.explanation = "problems/" + only_one(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=only_one(dom2.xpath('/problem/@explain'), default="closed")
self.explain_available = item(dom2.xpath('/problem/@explain_available')) self.explain_available = only_one(dom2.xpath('/problem/@explain_available'))
display_due_date_string = item(dom2.xpath('/problem/@due')) display_due_date_string = only_one(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 = only_one(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
...@@ -183,18 +188,18 @@ class Module(XModule): ...@@ -183,18 +188,18 @@ 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 =only_one(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 =only_one(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 =only_one(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":
...@@ -209,10 +214,10 @@ class Module(XModule): ...@@ -209,10 +214,10 @@ class Module(XModule):
if state!=None and 'attempts' in state: if state!=None and 'attempts' in state:
self.attempts=state['attempts'] self.attempts=state['attempts']
# TODO: Should be: self.filename=item(dom2.xpath('/problem/@filename')) # TODO: Should be: self.filename=only_one(dom2.xpath('/problem/@filename'))
self.filename= "problems/"+item(dom2.xpath('/problem/@filename'))+".xml" self.filename= "problems/"+only_one(dom2.xpath('/problem/@filename'))+".xml"
self.name=item(dom2.xpath('/problem/@name')) self.name=only_one(dom2.xpath('/problem/@name'))
self.weight=item(dom2.xpath('/problem/@weight')) self.weight=only_one(dom2.xpath('/problem/@weight'))
if self.rerandomize == 'never': if self.rerandomize == 'never':
seed = 1 seed = 1
else: else:
......
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