-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBTCInput3.py
executable file
·351 lines (323 loc) · 12.6 KB
/
BTCInput3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
DEBUG_MODE = True
from dateutil.parser import parse
import cmd2
#create read_date function
def read_date_adv(prompt, cmdobj, bg, fg, bold=True):
'''
Displays a prompt and reads in a date. Keyboard interrupts (CTRL+C) are
ignored. Invalid dates are rejected. Returns a datetime.date object
containing the value input by the user
'''
while True:
try:
date_text = read_text_adv(prompt=prompt, cmdobj=cmdobj, bg=bg,
fg=fg, bold=bold)
#if date_text == '':
# print('please enter text')
result = parse(date_text)
result = result.date()
break
#pass
except ValueError:
print('Please enter a valid date')
#pass
return result
def read_text_adv(prompt, cmdobj, bg=cmd2.bg.blue, fg=cmd2.fg.white, bold=True):
'''
Displays a prompt and reads in a string of text.
Keyboard interrupts (CTRL+C) are ignored
Takes a cmd style object for the prompt, and a cmd2.cmd instance as the cmdobj
returns a string containing the string input by the user
'''
#background_choices = {'blue': cmd2.bg.blue}
#foreground_choices = {'white': cmd2.fg.white}
#print(cmdobj)
# print(prompt)
while True: # repeat forever
try:
prompt_text=cmd2.style(text=prompt, bg=bg,#cmd2.bg.blue,#foreground_choices.get(foreground),
fg=fg, bold=bold)#foreground_choices.get(background))
cmdobj.poutput(prompt_text)
#result2 = str(cmdobj.prompt)
input_style=cmd2.style(text=cmdobj.prompt, fg=fg, bg=bg, bold=True)
result=input(input_style) # read the input
# if we get here no exception was raised
if result=='':
#don't accept empty lines
print('Please enter text')
else:
# break out of the loop
break
except KeyboardInterrupt:
# if we get here the user pressed CTRL+C
print('Please enter text')
if DEBUG_MODE:
raise Exception('Keyboard interrupt')
# return the result
return result
#pass
def read_number_adv(prompt,function, cmdobj, bg=cmd2.bg.blue, fg=cmd2.fg.white,
bold=True):
'''
Displays a prompt and reads in a floating point number.
Keyboard interrupts (CTRL+C) are ignored
Invalid numbers are rejected
returns a float containing the value input by the user
'''
while True: # repeat forever
try:
number_text=read_text_adv(prompt=prompt, cmdobj=cmdobj, bg=bg,
fg=fg, bold=bold)
result=function(number_text) # read the input
# if we get here no exception was raised
# break out of the loop
break
except ValueError:
# if we get here the user entered an invalid number
print('Please enter a number')
# return the result
return result
def read_number_ranged_adv(prompt,function, cmdobj, min_value, max_value,
bg=cmd2.bg.blue, fg=cmd2.fg.white,
bold=True):
'''
Displays a prompt and reads in a number.
min_value gives the inclusive minimum value
max_value gives the inclusive maximum value
Raises an exception if max and min are the wrong way round
Keyboard interrupts (CTRL+C) are ignored
Invalid numbers are rejected
returns a number containing the value input by the user
'''
#print(min_value)
# print(max_value)
if min_value>max_value:
# If we get here the min and the max
# are wrong way round
raise Exception('Min value is greater than max value')
while True: # repeat forever
result=read_number_adv(prompt=prompt,function=function, cmdobj=cmdobj, bg=bg, fg=fg)
if result<min_value:
# Value entered is too low
print('That number is too low')
print('Minimum value is:',min_value)
# Repeat the number reading loop
continue
if result>max_value:
# Value entered is too high
print('That number is too high')
print('Maximum value is:',max_value)
# Repeat the number reading loop
continue
# If we get here the number is valid
# break out of the loop
break
# return the result
return result
def read_float_adv(prompt, cmdobj, bg=cmd2.bg.blue, fg=cmd2.fg.white, bold=True):
'''
Displays a prompt and reads in a floating point number.
Keyboard interrupts (CTRL+C) are ignored
Invalid numbers are rejected
returns a float containing the value input by the user
'''
return read_number_adv(prompt=prompt, function=float, cmdobj=cmdobj,
bg=bg, fg=fg, bold=bold)
def read_int_adv(prompt, cmdobj, bg=cmd2.bg.blue, fg=cmd2.fg.white, bold=True):
'''
Displays a prompt and reads in an integer number.
Keyboard interrupts (CTRL+C) are ignored
Invalid numbers are rejected
returns an int containing the value input by the user
'''
return read_number_adv(prompt=prompt,function=int, cmdobj=cmdobj,
bg=bg, fg=fg, bold=bold)
def read_float_ranged_adv(prompt, min_value, max_value, cmdobj,
bg=cmd2.bg.blue, fg=cmd2.fg.white,
bold=True):
'''
Displays a prompt and reads in a floating point number.
min_value gives the inclusive minimum value
max_value gives the inclusive maximum value
Raises an exception if max and min are the wrong way round
Keyboard interrupts (CTRL+C) are ignored
Invalid numbers are rejected
returns a number containing the value input by the user
'''
return read_number_ranged(prompt=prompt,function=float,min_value=min_value,
max_value=max_value,cmdobj=cmdobj,
fg=fg, bg=bg, bold=bold)
def read_int_ranged_adv(prompt, min_value, max_value, cmdobj, bg=cmd2.bg.blue,
fg=cmd2.fg.white, bold=True):
'''
Displays a prompt and reads in an integer point number.
min_value gives the inclusive minimum value
max_value gives the inclusive maximum value
Raises an exception if max and min are the wrong way round
Keyboard interrupts (CTRL+C) are ignored
Invalid numbers are rejected
returns a number containing the value input by the user
'''
return read_number_ranged_adv(prompt=prompt,function=int,min_value=min_value,
max_value=max_value,cmdobj=cmdobj,
fg=fg, bg=bg, bold=bold)
#old functions
def read_text(prompt):
'''
Displays a prompt and reads in a string of text.
Keyboard interrupts (CTRL+C) are ignored
returns a string containing the string input by the user
'''
while True: # repeat forever
try:
result=input(prompt) # read the input
# if we get here no exception was raised
if result=='':
#don't accept empty lines
print('Please enter text')
else:
# break out of the loop
break
except KeyboardInterrupt:
# if we get here the user pressed CTRL+C
print('Please enter text')
if DEBUG_MODE:
raise Exception('Keyboard interrupt')
# return the result
return result
#def read_bool(prompt='y or n', yes='y', no='n', yes_option='confirm', no_option='cancel'):
# choice_string = ' '+ '{0} to {1}, {2} to {3} '.format(
# yes, yes_option, no, no_option)
# #The choice_string formats the choices that the user has to make when
# #making a yes or no decision
# while True:
# choice = read_text(prompt=prompt+choice_string+' ')
# if choice == yes:
# return True
# elif choice == no:
# return False
# else:
# print('Please enter {0} to {1}, {1} to {2}')
def read_bool(decision='(y or n)', yes='y', no='n', yes_option='confirm', no_option='cancel'):
choice_string = '{0} {1} to {2}, {3} to {4}: '.format(decision,
yes, yes_option, no, no_option)
#The choice_string formats the choices that the user has to make when
#making a yes or no decision
while True:
choice = read_text(prompt=choice_string)
if choice == yes:
return True
elif choice == no:
return False
else:
print('Invalid Entry')
# print('Please enter {0} to {1}, {2} to {3}'.format(yes_option, yes, no_option, no))
def read_date(prompt):
'''
Displays a prompt and reads in a date. Keyboard interrupts (CTRL+C) are
ignored. Invalid dates are rejected. Returns a datetime.date object
containing the value input by the user
'''
while True:
try:
date_text = read_text(prompt)
#if date_text == '':
# print('please enter text')
result = parse(date_text)
result = result.date()
break
#pass
except ValueError:
print('Please enter a valid date')
#pass
return result
def read_number(prompt,function):
'''
Displays a prompt and reads in a floating point number.
Keyboard interrupts (CTRL+C) are ignored
Invalid numbers are rejected
returns a float containing the value input by the user
'''
while True: # repeat forever
try:
number_text=read_text(prompt)
result=function(number_text) # read the input
# if we get here no exception was raised
# break out of the loop
break
except ValueError:
# if we get here the user entered an invalid number
print('Please enter a number')
# return the result
return result
def read_number_ranged(prompt, function, min_value, max_value):
'''
Displays a prompt and reads in a number.
min_value gives the inclusive minimum value
max_value gives the inclusive maximum value
Raises an exception if max and min are the wrong way round
Keyboard interrupts (CTRL+C) are ignored
Invalid numbers are rejected
returns a number containing the value input by the user
'''
if min_value>max_value:
# If we get here the min and the max
# are wrong way round
raise Exception('Min value is greater than max value')
while True: # repeat forever
result=read_number(prompt,function)
if result<min_value:
# Value entered is too low
print('That number is too low')
print('Minimum value is:',min_value)
# Repeat the number reading loop
continue
if result>max_value:
# Value entered is too high
print('That number is too high')
print('Maximum value is:',max_value)
# Repeat the number reading loop
continue
# If we get here the number is valid
# break out of the loop
break
# return the result
return result
def read_float(prompt):
'''
Displays a prompt and reads in a floating point number.
Keyboard interrupts (CTRL+C) are ignored
Invalid numbers are rejected
returns a float containing the value input by the user
'''
return read_number(prompt,float)
def read_int(prompt):
'''
Displays a prompt and reads in an integer number.
Keyboard interrupts (CTRL+C) are ignored
Invalid numbers are rejected
returns an int containing the value input by the user
'''
return read_number(prompt,int)
def read_float_ranged(prompt, min_value, max_value):
'''
Displays a prompt and reads in a floating point number.
min_value gives the inclusive minimum value
max_value gives the inclusive maximum value
Raises an exception if max and min are the wrong way round
Keyboard interrupts (CTRL+C) are ignored
Invalid numbers are rejected
returns a number containing the value input by the user
'''
return read_number_ranged(prompt,float,min_value,max_value)
def read_int_ranged(prompt, min_value, max_value):
'''
Displays a prompt and reads in an integer point number.
min_value gives the inclusive minimum value
max_value gives the inclusive maximum value
Raises an exception if max and min are the wrong way round
Keyboard interrupts (CTRL+C) are ignored
Invalid numbers are rejected
returns a number containing the value input by the user
'''
return read_number_ranged(prompt,int,min_value,max_value)