diff --git a/common/lib/xmodule/capa_module.py b/common/lib/xmodule/capa_module.py index 59e022cd16..a4140c7489 100644 --- a/common/lib/xmodule/capa_module.py +++ b/common/lib/xmodule/capa_module.py @@ -18,7 +18,12 @@ log = logging.getLogger("mitx.courseware") TIMEDELTA_REGEX = re.compile(r'^((?P\d+?) day(?:s?))?(\s)?((?P\d+?) hour(?:s?))?(\s)?((?P\d+?) minute(?:s)?)?(\s)?((?P\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: return default elif len(l) == 1: @@ -163,18 +168,18 @@ class Module(XModule): dom2 = etree.fromstring(xml) - self.explanation = "problems/" + 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.explanation = "problems/" + only_one(dom2.xpath('/problem/@explain'), default="closed") + # TODO: Should be converted to: self.explanation=only_one(dom2.xpath('/problem/@explain'), default="closed") + 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: self.display_due_date = dateutil.parser.parse(display_due_date_string) #log.debug("Parsed " + display_due_date_string + " to " + str(self.display_due_date)) else: 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: self.grace_period = parse_timedelta(grace_period_string) self.close_date = self.display_due_date + self.grace_period @@ -183,18 +188,18 @@ class Module(XModule): self.grace_period = None 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: self.max_attempts =int(self.max_attempts) else: 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 =="": 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": self.rerandomize="always" elif self.rerandomize=="false" or self.rerandomize=="per_student": @@ -209,10 +214,10 @@ class Module(XModule): if state!=None and 'attempts' in state: self.attempts=state['attempts'] - # TODO: Should be: self.filename=item(dom2.xpath('/problem/@filename')) - self.filename= "problems/"+item(dom2.xpath('/problem/@filename'))+".xml" - self.name=item(dom2.xpath('/problem/@name')) - self.weight=item(dom2.xpath('/problem/@weight')) + # TODO: Should be: self.filename=only_one(dom2.xpath('/problem/@filename')) + self.filename= "problems/"+only_one(dom2.xpath('/problem/@filename'))+".xml" + self.name=only_one(dom2.xpath('/problem/@name')) + self.weight=only_one(dom2.xpath('/problem/@weight')) if self.rerandomize == 'never': seed = 1 else: