Package aerocalc :: Module val_input
[frames] | no frames]

Source Code for Module aerocalc.val_input

  1  #!/usr/bin/python 
  2  # -*- coding: utf-8 -*- 
  3   
  4  # ############################################################################# 
  5  # Copyright (c) 2008, Kevin Horton 
  6  # All rights reserved. 
  7  # Redistribution and use in source and binary forms, with or without 
  8  # modification, are permitted provided that the following conditions are met: 
  9  # * 
 10  #     * Redistributions of source code must retain the above copyright 
 11  #       notice, this list of conditions and the following disclaimer. 
 12  #     * Redistributions in binary form must reproduce the above copyright 
 13  #       notice, this list of conditions and the following disclaimer in the 
 14  #       documentation and/or other materials provided with the distribution. 
 15  #     * The name of Kevin Horton may not be used to endorse or promote products 
 16  #       derived from this software without specific prior written permission. 
 17  # * 
 18  # THIS SOFTWARE IS PROVIDED BY KEVIN HORTON ``AS IS'' AND ANY EXPRESS OR 
 19  # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 20  # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 
 21  # EVENT SHALL KEVIN HORTON BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 22  # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 23  # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
 24  # OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
 25  # WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
 26  # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 27  # ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 28  # ############################################################################# 
 29  # 
 30  # version 0.10, 17 May 2007 
 31  # 
 32  # Version History: 
 33  # vers     date      Notes 
 34  # 0.10   17 May 07   Initial version. 
 35  # ############################################################################# 
 36  # 
 37  # To Do:  1. Done. 
 38  # 
 39  # 
 40  # Done    1. Find way to generalize the criteria, to allow coverage of a wider 
 41  #            range of cases. 
 42   
 43  # ############################################################################# 
 44   
 45  # def get_input_with_default(prompt, type, error_string, check_list =[], default = '', **kwds): 
 46  #     """ 
 47  #     Return user input after validating it, offering the user a default value. 
 48  #     """ 
 49  # 
 50  #     try: 
 51  #         prompt = 'CAS = [' + str(data_items['cas']) + '] ' 
 52  #         value = VI.get_input(prompt, 'float+_or_blank', 
 53  #                        'ERROR - CAS must be a positive number.') 
 54  #         if value != '': 
 55  #             data_items['cas'] = value 
 56  #     except KeyError: 
 57  #         prompt = 'CAS = ' 
 58  #         data_items['cas'] = VI.get_input(prompt, 'float+_or_blank', 
 59  #                        'ERROR - CAS must be a positive number.') 
 60  # 
 61  #     return data_items['cas'] 
 62   
 63  """ 
 64  Validate user input against arbitrary criteria.  Used in the interactive interface in other modules. 
 65  """ 
 66   
 67   
68 -def get_input( 69 prompt, 70 type, 71 error_string, 72 check_list=[], 73 default='', 74 **kwds 75 ):
76 """ 77 Return user input after validating it. 78 """ 79 80 input_validated = False 81 82 if type == 'float': 83 while not input_validated: 84 input_data = raw_input(prompt) 85 try: 86 input_data = float(input_data) 87 input_validated = True 88 except ValueError: 89 print error_string 90 91 if type == 'float+': 92 93 # data is a positive float 94 95 input_data = raw_input(prompt) 96 try: 97 input_data = float(input_data) 98 if input_data < 0: 99 print error_string 100 else: 101 input_validated = True 102 except ValueError: 103 print error_string 104 elif type == 'float_or_blank': 105 106 # data is a float or blank 107 108 while not input_validated: 109 input_data = raw_input(prompt) 110 if input_data == '': 111 input_data = default 112 input_validated = True 113 else: 114 try: 115 input_data = float(input_data) 116 input_validated = True 117 except ValueError: 118 print error_string 119 elif type == 'float_str_or_blank': 120 121 # data is a float, a string in kwds['str_list'] or blank 122 123 while not input_validated: 124 input_data = raw_input(prompt) 125 if input_data == '': 126 input_data = default 127 input_validated = True 128 elif input_data in kwds['str_list']: 129 input_validated = True 130 else: 131 try: 132 input_data = float(input_data) 133 input_validated = True 134 except ValueError: 135 print error_string 136 elif type == 'float+_or_blank': 137 138 # data is a float, >= 0, or blank 139 140 while not input_validated: 141 input_data = raw_input(prompt) 142 if input_data == '': 143 input_data = default 144 input_validated = True 145 return input_data 146 try: 147 input_data = float(input_data) 148 if input_data < 0: 149 print error_string 150 else: 151 input_validated = True 152 except ValueError: 153 print error_string 154 155 if type == 'int': 156 while 1: 157 input_data = raw_input(prompt) 158 try: 159 input_data = int(input_data) 160 break 161 except ValueError: 162 print error_string 163 164 if type == 'int_or_str': 165 166 # data is an int or a string in kwds['str_list'] 167 168 while 1: 169 input_data = raw_input(prompt) 170 if input_data in kwds['str_list']: 171 break 172 try: 173 input_data = int(input_data) 174 try: 175 if input_data < kwds['min']: 176 print 'You must enter an integer no less than', \ 177 kwds['min'] 178 except KeyError: 179 pass 180 try: 181 if input_data > kwds['max']: 182 print 'You must enter an integer less than', \ 183 kwds['max'] 184 except KeyError: 185 break 186 except ValueError: 187 print 'You must enter an integer' 188 elif type == 'list': 189 190 while not input_validated: 191 input_data = raw_input(prompt) 192 if input_data == '': 193 input_data = default 194 input_validated = True 195 return default 196 if input_data in check_list: 197 input_validated = True 198 else: 199 print error_string 200 201 return input_data
202 203
204 -def get_input2( 205 prompt, 206 conditions_any=[], 207 conditions_all=[], 208 debug=False, 209 ):
210 """Return user input, after validating it against arbitrary criteria. 211 212 The user input must satisfy all conditions in conditions_all, and one or 213 more of the conditions in conditions_any. Conditions_any is a list, with 214 the first item the error string to display if none of the conditions are 215 met. Conditions_all is a list of tuples, with each tuple having one 216 condition + the associated error string to display to the user if the 217 condition is not met. 218 219 The conditions will be inserted in 'if' statements, and must be written 220 so that the 'if' statement will be true if the data is valid. E.g. 221 222 if a 'Q' is needed: 223 'X == \"Q\"' 224 225 Note 1: all data input by the user is seen as a string, even if numbers are 226 entered. If the test is looking for an integer, write it as: 227 'int(X)' 228 229 Note 2: All tests are run inside 'try' statements, so exceptions will not 230 cause problems. Any test that raises an exception is treated as a failed 231 test. 232 233 Note 3: One idiosyncrasy of 'if' statements is that any statement that 234 returns a \"0\" is treated as a False. Tests must be crafted so a pass 235 does not return a '0'. E.g., if the test is intended to check that the 236 data is a float, and a zero would be valid data, do not write the test as: 237 'float(X)', as 'if float(\"0\"):' will not pass. 238 239 """ 240 241 input_validated = False 242 243 while not input_validated: 244 X = raw_input(prompt) 245 validated_any = False 246 validated_all = True 247 248 # does input meet one of conditions in conditions_any? 249 250 if len(conditions_any) > 0: 251 error_string = conditions_any[0] 252 for condition in conditions_any[1:]: 253 if debug: 254 print 'Testing condition', condition 255 try: 256 if eval(condition): 257 validated_any = True 258 if debug: 259 print 'Test of ', condition, 'passed' 260 else: 261 if debug: 262 print 'Test of ', condition, 'failed' 263 except: 264 if debug: 265 print 'Exception during test' 266 pass 267 268 # does input meet all conditions in conditions_all? 269 270 if len(conditions_all) > 0: 271 for condition in conditions_all: 272 if debug: 273 print 'Testing condition', condition[0] 274 try: 275 if eval(condition[0]): 276 if debug: 277 print 'Test of ', condition[0], 'passed' 278 pass 279 else: 280 if debug: 281 print 'Test of ', condition[0], 'failed' 282 validated_all = False 283 print condition[1] 284 except: 285 if debug: 286 print 'Exception during test' 287 validated_all = False 288 print condition[1] 289 290 if not validated_any: 291 print error_string, '\n' 292 elif validated_all: 293 input_validated = True 294 return X
295