4 Copyright (C) 2008 Alberto Gomez-Casado (University of Twente).
6 This program is released under the GNU General Public License version 2.
12 def safeinput (message, valid=[]):
14 friendlier frontend for alphainput and numinput
15 valid should be a list of 0...n values
18 #if possible values are not listed we just ask for any non-null input
20 return alphainput(message, '',1,[])
24 #if valid values are string we use alphainput, if it is only one we take as default
25 if type(valid[0]) is StringType:
27 return alphainput(message, valid[0], 0,[])
29 return alphainput(message,'', 1,valid)
31 #if valid values are numbers we use numinput
32 if type(valid[0]) is IntType:
34 return numinput(message,valid[0],1,[])
36 return numinput(message,'',1,valid)
39 def alphainput (message, default, repeat, valid):
41 message: prompt for the user
42 default: return value if user input was not correct (and repeat=0)
43 repeat: keeps asking user till it gets a valid input
44 valid: list of allowed answers, empty list for "anything"
46 if default and not repeat:
47 print 'Press [enter] for default: ('+str(default)+')'
48 reply=raw_input(message)
54 while reply not in valid:
55 reply=raw_input('You should enter any of these: '+ str(valid) +'\n'+ message)
68 reply=raw_input(message)
72 def checkalphainput (test, default, valid):
73 #useful when input was taken form command args
80 #TODO: raise exception?
87 def numinput(message, default, repeat, limits):
89 message: prompt for the user
90 default: return value if user input was not correct (and repeat=0)
91 repeat: keeps asking user till it gets a valid input
92 limits: pair of values, input is checked to be between them, empty list for "any number"
94 if default and not repeat:
95 print 'Press [enter] for default: '+str(default)
97 reply=raw_input(message)
105 high=int(limits.pop())
106 low=int(limits.pop())
107 if intreply>=low and intreply <= high:
111 while intreply<low or intreply>high :
112 reply=raw_input('You should enter values between: '+ str(low)+' and '+str(high) +'\n'+ message)
127 while intreply==None:
129 reply=raw_input(message)
137 def checknuminput(test,default,limits):
138 #useful when input was taken from command args
140 high=int(limits.pop())
141 low=int(limits.pop())
142 if test>=low and test <= high: