def get_answer(question, match_prompt, conv_fn=None, test=None, review=None): r''' >>> from StringIO import StringIO >>> sys.stdin = StringIO('4\n') >>> get_answer(u'enter number?', '[0-10]', qa_helpers.to_int, ... slice(3,5)) ______________________________________________________________________________ enter number? [0-10] 4 >>> sys.stdin = StringIO('2\n4\n') >>> get_answer(u'enter number?', '\n[0-10]', qa_helpers.to_int, ... slice(3,5)) ______________________________________________________________________________ enter number? [0-10] answer should be between 3 and 5, got 2 <BLANKLINE> Try Again: ______________________________________________________________________________ enter number? [0-10] 4 >>> sys.stdin = StringIO('4\n') >>> get_answer(u'enter number?\n', '[0-10]', qa_helpers.to_int, slice(3,5), ... ((3, u'not enough'), (4, u'hurray!'), (5, u'too much'))) ______________________________________________________________________________ enter number? [0-10] hurray! 4 ''' if not question[-1].isspace() and \ (not match_prompt or not match_prompt[0].isspace()): question += ' ' question += match_prompt if match_prompt and not match_prompt[-1].isspace(): question += ' ' if encoding: question = question.encode(encoding) while True: print "_" * 78 ans = raw_input(question) try: if encoding and sys.version_info[0] < 3: ans = ans.decode(encoding) if conv_fn: ans = conv_fn(ans) if test: ans = qa_helpers.match(ans, test) break except ValueError, e: print "answer should be %s, got %s" % (str(e), repr(ans)) print print "Try Again:"
def get_answer(question, title, conv_fn=None, test=None, review=None): dlg = wx.TextEntryDialog(None, question, title, u'', wx.CENTRE | wx.RESIZE_BORDER | wx.OK) while True: status = dlg.ShowModal() if status != wx.ID_OK: raise AssertionError("dlg.ShowModal failed with %d" % status) ans = dlg.GetValue() try: if conv_fn: ans = conv_fn(ans) if test: ans = qa_helpers.match(ans, test) break except ValueError, e: err = wx.MessageDialog(dlg, u"Answer should be %s\nGot %s" % (e.message, repr(ans)), u"Error Notification", wx.OK | wx.ICON_ERROR) err.ShowModal() err.Destroy() dlg.SetValue(u"")
def matches2(ans, test): try: qa_helpers.match(ans, test) return True except ValueError: return False
def record_answer(self, postdata): q = self.session["last_question"] params = q["format_params"] question = self.engine.get_ke(q["base"], q["name"]) q_type = question.user_question.__class__.__name__ FormType = self.getFormType() self.form = FormType(postdata) if not self.form.is_valid(): return False answer = self.form.cleaned_data["question"] alternatives = None if q_type == "yn": pass elif q_type == "integer": pass elif q_type == "float": pass elif q_type == "number": if answer.to_integral() == answer: answer = int(answer) else: answer = float(answer) elif q_type == "string": pass elif q_type == "select_1": alternatives = question.prepare_arg2(params) for alt in alternatives: (tag, desc) = alt try: int_ans = int(answer) except ValueError: int_ans = None if tag == int_ans: answer = tag break if tag == answer: break elif q_type == "select_n": alternatives = question.prepare_arg2(params) mapped_answers = [] for alt in alternatives: for ans in answer: (tag, desc) = alt try: int_ans = int(ans) except ValueError: int_ans = None if tag == int_ans: mapped_answers.append(tag) break if tag == ans: mapped_answers.append(tag) answer = mapped_answers else: raise Exception("Unrecognised type: %s" % q_type) if alternatives: match = None else: match = question.prepare_arg2(params) if match: try: qa_helpers.match(answer, match) except ValueError, e: self.form._errors["question"] = self.form.error_class([ "Answer should be %s. Got %s" % ( e.message, repr(answer))]) return False
def record_answer(self, postdata): q = self.session["last_question"] params = q["format_params"] question = self.engine.get_ke(q["base"], q["name"]) q_type = question.user_question.__class__.__name__ FormType = self.getFormType() self.form = FormType(postdata) if not self.form.is_valid(): return False answer = self.form.cleaned_data["question"] alternatives = None if q_type == "yn": pass elif q_type == "integer": pass elif q_type == "float": pass elif q_type == "number": if answer.to_integral() == answer: answer = int(answer) else: answer = float(answer) elif q_type == "string": pass elif q_type == "select_1": alternatives = question.prepare_arg2(params) for alt in alternatives: (tag, desc) = alt try: int_ans = int(answer) except ValueError: int_ans = None if tag == int_ans: answer = tag break if tag == answer: break elif q_type == "select_n": alternatives = question.prepare_arg2(params) mapped_answers = [] for alt in alternatives: for ans in answer: (tag, desc) = alt try: int_ans = int(ans) except ValueError: int_ans = None if tag == int_ans: mapped_answers.append(tag) break if tag == ans: mapped_answers.append(tag) answer = mapped_answers else: raise Exception("Unrecognised type: %s" % q_type) if alternatives: match = None else: match = question.prepare_arg2(params) if match: try: qa_helpers.match(answer, match) except ValueError, e: self.form._errors["question"] = self.form.error_class([ "Answer should be %s. Got %s" % (e.message, repr(answer)) ]) return False
def get_answer(question, match_prompt, conv_fn=None, test=None, review=None): r''' >>> from io import StringIO >>> sys.stdin = StringIO('4\n') >>> get_answer('enter number?', '[0-10]', qa_helpers.to_int, ... slice(3,5)) ______________________________________________________________________________ enter number? [0-10] 4 >>> sys.stdin = StringIO('2\n4\n') >>> get_answer('enter number?', '\n[0-10]', qa_helpers.to_int, ... slice(3,5)) ______________________________________________________________________________ enter number? [0-10] answer should be between 3 and 5, got 2 <BLANKLINE> Try Again: ______________________________________________________________________________ enter number? [0-10] 4 >>> sys.stdin = StringIO('4\n') >>> get_answer('enter number?\n', '[0-10]', qa_helpers.to_int, slice(3,5), ... ((3, 'not enough'), (4, 'hurray!'), (5, 'too much'))) ______________________________________________________________________________ enter number? [0-10] hurray! 4 ''' if not question[-1].isspace() and \ (not match_prompt or not match_prompt[0].isspace()): question += ' ' question += match_prompt if match_prompt and not match_prompt[-1].isspace(): question += ' ' if encoding: question = question.encode(encoding) while True: print("_" * 78) ans = input(question) try: if encoding and sys.version_info[0] < 3: ans = ans.decode(encoding) if conv_fn: ans = conv_fn(ans) if test: ans = qa_helpers.match(ans, test) break except ValueError as e: print("answer should be %s, got %s" % (str(e), repr(ans))) print() print("Try Again:") if review: def matches2(ans, test): try: qa_helpers.match(ans, test) return True except ValueError: return False def matches(ans, test): if isinstance(ans, (tuple, list)): return any(map(lambda elem: matches2(elem, test), ans)) return matches2(ans, test) for review_test, review_str in review: if matches(ans, review_test): print(review_str) return ans