6 Copyright (C) 2008 Alberto Gomez-Casado (University of Twente).
8 This program is released under the GNU General Public License version 2.
15 def safeinput (message, valid=[]):
17 friendlier frontend for alphainput and numinput
18 valid should be a list of 0...n values
21 #if possible values are not listed we just ask for any non-null input
23 return alphainput(message, '',1,[])
27 #if valid values are string we use alphainput, if it is only one we take as default
28 if type(valid[0]) is StringType:
30 return alphainput(message, valid[0], 0,[])
32 return alphainput(message,'', 1,valid)
34 #if valid values are numbers we use numinput
35 if type(valid[0]) is IntType:
37 return numinput(message,valid[0],1,[])
39 return numinput(message,'',1,valid)
43 def alphainput (message, default, repeat, valid):
45 message: prompt for the user
46 default: return value if user input was not correct (and repeat=0)
47 repeat: keeps asking user till it gets a valid input
48 valid: list of allowed answers, empty list for "anything"
50 if default and not repeat:
51 print 'Press [enter] for default: ('+str(default)+')'
52 reply=raw_input(message)
58 while reply not in valid:
59 reply=raw_input('You should enter any of these: '+ str(valid) +'\n'+ message)
72 reply=raw_input(message)
77 def checkalphainput (test, default, valid):
78 #useful when input was taken form command args
85 #TODO: raise exception?
92 def numinput(message, default, repeat, limits):
94 message: prompt for the user
95 default: return value if user input was not correct (and repeat=0)
96 repeat: keeps asking user till it gets a valid input
97 limits: pair of values, input is checked to be between them, empty list for "any number"
99 if default and not repeat:
100 print 'Press [enter] for default: '+str(default)
102 reply=raw_input(message)
110 high=int(limits.pop())
111 low=int(limits.pop())
112 if intreply>=low and intreply <= high:
116 while intreply<low or intreply>high :
117 reply=raw_input('You should enter values between: '+ str(low)+' and '+str(high) +'\n'+ message)
132 while intreply==None:
134 reply=raw_input(message)
141 def checknuminput(test,default,limits):
142 #useful when input was taken from command args
144 high=int(limits.pop())
145 low=int(limits.pop())
146 if test>=low and test <= high: