#NEWLINE# def query_yes_no( question , default = 'yes' ) : #NEWLINE# #INDENT# '\n    Ask a yes/no question via `read_response()` and return their answer.\n\n    :param question: A string that is presented to the user.\n    :param default: The presumed answer if the user just hits <Enter>.\n        It must be "yes" (the default), "no" or None (meaning\n        an answer is required of the user).\n\n    The "answer" return value is one of "yes" or "no".\n\n    Adapted from\n    http://stackoverflow.com/questions/3041986/python-command-line-yes-no-input\n    http://code.activestate.com/recipes/577058/\n\n    ' #NEWLINE# valid = { #NEWLINE# #INDENT# 'yes' : True , #NEWLINE# 'y' : True , #NEWLINE# 'ye' : True , #NEWLINE# 'no' : False , #NEWLINE# 'n' : False , #NEWLINE# #UNINDENT# } #NEWLINE# if ( default is None ) : #NEWLINE# #INDENT# prompt = ' [y/n] ' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( default == 'yes' ) : #NEWLINE# #INDENT# prompt = ' [Y/n] ' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( default == 'no' ) : #NEWLINE# #INDENT# prompt = ' [y/N] ' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise ValueError ( 'Invalid default answer: "{0}"' . format ( default ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# while True : #NEWLINE# #INDENT# sys . stdout . write ( ( question + prompt ) ) #NEWLINE# choice = read_response ( ) . lower ( ) #NEWLINE# if ( ( default is not None ) and ( choice == '' ) ) : #NEWLINE# #INDENT# return valid [ default ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( choice in valid ) : #NEWLINE# #INDENT# return question [ choice ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# sys . stdout . write ( 'Please respond with "yes" or "no" (or "y" or "n").\n' )