Last active
April 19, 2020 14:36
-
-
Save berkakkaya/ad1997eacddd66c1f7a874c57c154326 to your computer and use it in GitHub Desktop.
Input Controller - Script that takes user inputs and checks whether the answer is of the desired type (or checks if given path exists, you can customize it :) )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from os.path import exists | |
| class InputController(object): | |
| """Controller class for controlling user inputs | |
| Methods | |
| ------- | |
| get_bool(prompt : str) -> bool | |
| Checks if the answer is yes(y) or no(n). | |
| get_path(prompt : str) -> str | |
| Checks if the path that is specified in answer is exists. | |
| get_str(prompt : str) -> str | |
| Checks if the answer is blank. | |
| Can be used if answer is required. | |
| get_option(prompt : str, choices:list) -> int | |
| Checks if answer is a selectable option. | |
| keyboard_interrupt() | |
| Custom function that will be called if KeyboardInterrupt raised. | |
| Don't use this function outside of the InputController class. | |
| cancel_operation() | |
| Custom function that is called when user want to cancel operation (if user types 'q') | |
| Don't use this function outside of the InputController class. | |
| """ | |
| def __init__(self): | |
| super().__init__() | |
| def get_path(self, prompt: str) -> str: | |
| """ | |
| Checks if the path that is specified in answer is exists. | |
| Parameters | |
| ---------- | |
| prompt : str | |
| The prompt that will be asked to the user | |
| """ | |
| while True: | |
| try: user_input = input(prompt) | |
| except KeyboardInterrupt: self.keyboard_interrupt() | |
| if not exists(user_input): | |
| print("Your specified folder not exists. Please check your folder path and try again.") | |
| else: | |
| return user_input | |
| def get_bool(self, prompt: str) -> bool: | |
| """ | |
| Checks if the answer is yes(y) or no(n). | |
| Parameters | |
| ---------- | |
| prompt : str | |
| The prompt that will be asked to the user | |
| """ | |
| acceptable_inputs = ["y", "n", "yes", "no"] | |
| while True: | |
| try: user_input = input(prompt) | |
| except KeyboardInterrupt: self.keyboard_interrupt() | |
| if user_input not in acceptable_inputs: | |
| print("You can answer questions with yes(y) or no(n)") | |
| elif user_input in ["yes", "y"]: | |
| return True | |
| else: | |
| return False | |
| def get_str(self, prompt: str) -> str: | |
| """ | |
| Checks if the answer is blank. | |
| Can be used if answer is required. | |
| Parameters | |
| ---------- | |
| prompt : str | |
| The prompt that will be asked to the user | |
| """ | |
| while True: | |
| try: user_input = input(prompt) | |
| except KeyboardInterrupt: self.keyboard_interrupt() | |
| if len(user_input) == 0: | |
| print("This question cannot be left blank.") | |
| else: | |
| return user_input | |
| def get_option(self, prompt: str, choices: list) -> int: | |
| """ | |
| Checks if answer is a selectable option. | |
| Parameters | |
| ---------- | |
| prompt : str | |
| The prompt that will be asked to the user | |
| choices : list | |
| The choices that user must select between them | |
| """ | |
| while True: | |
| try: user_input = input(prompt) | |
| except KeyboardInterrupt: self.keyboard_interrupt() | |
| if "q" in user_input: self.cancel_operation() | |
| try: user_input = int(user_input) | |
| except: | |
| print("Your answer must be an integer.") | |
| continue | |
| if user_input not in choices: | |
| print("Your selection is not in options. Options are: ", end="") | |
| print(*choices, sep=", ", end=".\n") | |
| continue | |
| return user_input | |
| def keyboard_interrupt(self): | |
| """ | |
| This function is called if KeyboardInterrupt raised. | |
| You can do anything you want here. | |
| Don't use it outside of the InputController class. | |
| """ | |
| print("Operation cancelled.") | |
| exit(1) | |
| def cancel_operation(self): | |
| """ | |
| This function is called if user want to cancel operaion (when user types 'q') | |
| You can do everything you want here. | |
| Don't use it outside of the InputController class. | |
| """ | |
| print("Operation cancelled.") | |
| exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment