4 Copyright (C) 2008 Alberto Gomez-Casado (University of Twente).
6 This program is released under the GNU General Public License version 2.
8 This is just a collection of wrappers for raw_input(), I noticed using
9 hooke that unexpected answers from the user often triggered a nasty
10 crash, so what they do is provide some basic check of inputs to avoid
11 'voids', letters where numbers are expected, etc
13 The basic call there is safeinput(message,[list])
14 Message is a string that is shown to the user ('Enter filename:')
15 [list] can be present or not.
16 - If not present safeinput is just a raw_input shielded against void
17 inputs (it will keep asking until it gets some answer)
18 - If a list of only one element is provided, it is interpreted as a
19 default value (in case a void input that value will be returned)
20 - If a list of two or more 'string' elements is provided, user input
21 must match one of them to be valid
22 - If a list of two integer elements [a, b] is provided, user input
23 is required to be in the interval [a,b]
25 More info about the underlying calls can be found in the code. However
26 I am still not very satisfied with them, That's why I made safeinput()
27 to wrap all so I can improve them without further fiddling with other
34 def safeinput (message, valid=[]):
36 friendlier frontend for alphainput and numinput
37 valid should be a list of 0...n values
40 #if possible values are not listed we just ask for any non-null input
42 return alphainput(message, '',1,[])
46 #if valid values are string we use alphainput, if it is only one we take as default
47 if type(valid[0]) is StringType:
49 return alphainput(message, valid[0], 0,[])
51 return alphainput(message,'', 1,valid)
53 #if valid values are numbers we use numinput
54 if type(valid[0]) is IntType:
56 return numinput(message,valid[0],1,[])
58 return numinput(message,'',1,valid)
61 def alphainput (message, default, repeat, valid):
63 message: prompt for the user
64 default: return value if user input was not correct (and repeat=0)
65 repeat: keeps asking user till it gets a valid input
66 valid: list of allowed answers, empty list for "anything"
68 if default and not repeat:
69 print 'Press [enter] for default: ('+str(default)+')'
70 reply=raw_input(message)
76 while reply not in valid:
77 reply=raw_input('You should enter any of these: '+ str(valid) +'\n'+ message)
90 reply=raw_input(message)
94 def checkalphainput (test, default, valid):
95 #useful when input was taken form command args
102 #TODO: raise exception?
109 def numinput(message, default, repeat, limits):
111 message: prompt for the user
112 default: return value if user input was not correct (and repeat=0)
113 repeat: keeps asking user till it gets a valid input
114 limits: pair of values, input is checked to be between them, empty list for "any number"
116 if default and not repeat:
117 print 'Press [enter] for default: '+str(default)
119 reply=raw_input(message)
127 high=int(limits.pop())
128 low=int(limits.pop())
129 if intreply>=low and intreply <= high:
133 while intreply<low or intreply>high :
134 reply=raw_input('You should enter values between: '+ str(low)+' and '+str(high) +'\n'+ message)
149 while intreply==None:
151 reply=raw_input(message)
159 def checknuminput(test,default,limits):
160 #useful when input was taken from command args
162 high=int(limits.pop())
163 low=int(limits.pop())
164 if test>=low and test <= high: