#!/usr/bin/env python3 # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, # MA 02110-1301, USA. """commonly funtions used by code_m """ __all__ = ['exit_with_status', 'get_int', 'get_ans'] __version__ = "0.1.4.2 beta" __author__ = "Cody A. Taylor, better known as code_m ( codemister99@yahoo.com )" import sys def exit_with_status(options="", exit_status=1, arg=False): """Interactive help for use with error handling or help arguments (sys.argv[1]) Default is to print "Usage: sys.argv[0] options" and exit with status 1 If arg is set to True then it will check for help arguments (useful for start of main) """ usage = "Usage: {0} {1}".format(sys.argv[0], str(options)) if arg: try: if sys.argv[1] in {"-h", "--help"}: print(usage) sys.exit(exit_status) else: #Explict pass #pass, not needed except IndexError: pass else: print(usage) sys.exit(exit_status) def get_int(msg, end_characters=": ", name="default", min=None, max=None, default=None, default_style=" [default {default}]", allow_zero=True): """Simple get integer from user Likely the most flexible get_int I have ever written: msg, end_characters, and default_style are added in a programatic way so you can have lots of "automatic defaults" or control how the msg appear exactly the way you want. "end_characters" and "default_style" are added for flexability, allowing you control all parts of the output. "name" is used for (Range and NonZero) error handling, the default is the 'first_word_of_msg' "min" and "max" can be used together or seperately, useful if you want only positives or only negetives Examples: print 'Value [7]? ' and expect an interger to be entered between -20 and 20: >>> get_int("Value", "? ", min=-20, max=20, default=7, default_style=" [{default}]", allow_zero=False) print 'Value [default 20]: ' and an integer to be entered: >>> get_int("Value", default=20) print 'How many pages have you read? ' and expect a positive integer to be entered: >>> get_int("How many pages have you read", "? ", name="pages read", min=0) """ class RangeError(Exception): pass if name == "default": name = msg.split()[0] name = name.capitalize() msg += end_characters if default == None else (default_style.format(default=default) + end_characters) while True: line = input(msg) if not line and default is not None: line = default try: i = int(line) if min and max: if not (min <= i <= max): raise RangeError("{0} must fall between (or equal) {1} and {2}".format(name, min, max)) elif min != None: if not min <= i: raise RangeError("{0} can not be less than {1}".format(name, min)) elif max != None: if not max >= i: raise RangeError("{0} can not be greater than {1}".format(name, max)) if allow_zero == False and i == 0: raise ValueError("{0} cannot be 0".format(name)) except (ValueError, RangeError) as err: print("Error:", err) continue return i def get_ans(msg, end_characters=": ", name="default", default=None, default_style=" [default {default}]"): """Very simple boolean question function. Likely the most flexible get_ans I have ever written: msg, end_characters, and default_style are added in a programatic way so you can have lots of "automatic defaults" or control how the msg appears exactly the way you want. Allows the user to put in any capital varient of 'n', 'no', 'y', 'yes' "end_characters" and "default_style" are given for flexability. "name" is used for error handling, default is 'first_word_of_msg' default can be entered as any 'yes' or 'no' varient or a boolean Examples: print 'Are you happy? ' and use "Happy" for error handling: >>> get_ans("Are you happy", "? ", name='happy') print 'Gone fishin [Yes]: ' and use "Gone" for error handling: >>> get_ans("Gone fishin", default="Yes", default_style=" [{default}]") """ if name == "default": name = msg.split()[0] msg += end_characters if default == None else (default_style.format(default=default) + end_characters) while True: line = input(msg).lower() if not line and default is not None: if default in {True, False}: return default else: line = default.lower() if line in {"y", "yes"}: return True elif line in {"n", "no"}: return False else: print("Error: {0} must be 'Yes' or 'No'".format(name.capitalize())) continue