Is there a Python equivalent to HighLine?
Question: [HighLine](http://highline.rubyforge.org) is a Ruby library for easing console
input and output. It provides methods that lets you request input and validate
it. Is there something that provides functionality similar to it in Python?

To show what HighLine does see the following example:

    
    
    require 'highline/import'
    
    input = ask("Yes or no? ") do |q|
      q.responses[:not_valid] = "Answer y or n for yes or no"
      q.default = 'y'
      q.validate = /\A[yn]\Z/i
    end
    

It asks "Yes or no? " and lets the user input something. As long as the user
does not input y or n (case-insensitive) it prints "Answer y or n for yes or
no" and lets the user type an answer again. Also if the user press Enter it
defaults to y. Finally, when it is done the input is stored in `input`. Here
is an example result where the user first input "EH???" and then "y":

    
    
    Yes or no? |y| EH???
    Answer y or n for yes or no
    ?  y
    

Is there similarly simple way to do the same in Python?


Answer: The following _should_ work similarly for you, although it will not be exactly
the same style of asking as it is in Ruby.

    
    
    class ValidInput(object):
        def __init__(self,prompt,default="",regex_validate="",
                 invalid_response="",correct_response=""):
            self.prompt=prompt
            self.default=default
            self.regex_validate=regex_validate
            self.invalid_response=invalid_response
            self.correct_response=correct_response
        def ask(self):
            fin=""
            while True:
                v_in=raw_input(self.prompt)
                if re.match(v_in,self.regex_validate):
                    fin=v_in
                    print self.correct_response
                    break
                else:
                    print self.invalid_response
                    if self.default=="break":
                          break
                    continue
            return fin
    

And you would use it like:

    
    
    my_input=ValidInput("My prompt (Y/N): ",regex_validate="your regex matching string here",
                        invalid_response="The response to be printed when it does not match the regex",
                        correct_response="The response to be printed when it is matched to the regex.")
    
    my_input.ask()
    

