def parse_simple_match(self): token, value = self.get_token() if token == 'str' or token == 'id' or token == 'number' or \ token == 'const': next_token, next_value = self.get_token() if next_token == 'prompt' and token == 'str': final_token, final_value = self.get_token('regexp') return qa_helpers.regexp(final_value, value, next_value) if next_token == 'regexp' and token == 'str': return qa_helpers.regexp(next_value, value) if next_token == 'equal': return qa_helpers.qmap(self.parse_simple_match(), value) if next_token == 'hyphen' and token == 'number': final_token, final_value = self.get_token() if final_token == 'number': return slice(value, final_value) self.push_token() return slice(value, None) self.push_token() return value if token == 'prompt': next_token, next_value = self.get_token('regexp') return qa_helpers.regexp(next_value, prompt=value) if token == 'lparen': ans = self.parse_match() self.get_token('rparen') return ans if token == 'regexp': return qa_helpers.regexp(value) if token == 'hyphen': next_token, next_value = self.get_token('number') return slice(None, next_value) self.SyntaxError("expected match, got %s" % token)
def ask_yn(question, review=None): r''' >>> from StringIO import StringIO >>> sys.stdin = StringIO('yes\n') >>> ask_yn(u'got it?') ______________________________________________________________________________ got it? (y/n) True >>> sys.stdin = StringIO('N\n') >>> ask_yn(u'got it?') ______________________________________________________________________________ got it? (y/n) False ''' return get_answer(question, u"(y/n)", conv_fn=lambda str: str.lower(), test=(qa_helpers.qmap(yes_match, True), qa_helpers.qmap(no_match, False)), review=review)