-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperiment_io_tools.py
executable file
·155 lines (123 loc) · 3.67 KB
/
experiment_io_tools.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
#!/usr/bin/env sage
""" Module to contain all I/O functions used by
cone conjecture experiment.
"""
import sys
import time
def timed_query_yes_no(question,expire=5,default=True):
timeout = time.time()+ expire # seconds
user_input = None
while True:
time.sleep(.1)
user_input = query_yes_no(question)
if user_input is not None:
return user_input
if time.time() > timeout:
break
return default
def query_yes_no(question, expire=5,default="yes"):
"""Ask a yes/no question via raw_input() and return their answer.
"question" is a string that is presented to the user.
"default" is the presumed answer if the user just hits <Enter>.
It must be "yes" (the default), "no" or None (meaning
an answer is required of the user).
The "answer" return value is True for "yes" or False for "no".
Source: http://code.activestate.com/recipes/577058/)
"""
valid = {"yes": True, "y": True, "ye": True,
"no": False, "n": False}
if default is None:
prompt = " [y/n] "
elif default == "yes":
prompt = " [Y/n] "
elif default == "no":
prompt = " [y/N] "
else:
raise ValueError("invalid default answer: '%s'" % default)
while True:
sys.stdout.write(question + prompt)
choice = raw_input().lower()
if default is not None and choice == '':
return valid[default]
elif choice in valid:
return valid[choice]
else:
sys.stdout.write("Please respond with 'yes' or 'no' "
"(or 'y' or 'n').\n")
def new_screen(header=None):
""" Prints new screen in terminal regardless of OS """
print("\033[H\033[J")
if header is not None:
boxprint(header)
def boxprint(string,symbol='#'):
""" prints a box around a string using symbol """
length = len(string)+4
mainline = []
mainline.append(symbol)
mainline.append(' ')
mainline.append(string)
mainline.append(' ')
mainline.append(symbol)
mainlinestring = "".join(str(e) for e in mainline)
horizontalboarder = [symbol for i in range(length)]
horizontalboarderstring = "".join(str(e) for e in horizontalboarder)
print(horizontalboarderstring)
print(mainlinestring)
print(horizontalboarderstring)
def pause(pausestring="Press Enter to continue..."):
try:
input("\n"+pausestring)
except:
None
def ask_int(string="Please input an integer: "):
""" returns a user inputted integer """
acceptable_input = False
user_input = None
while not acceptable_input:
try:
user_input = input(string)
except:
print("\tNon-integer input detected, try again...")
if isinstance(user_input, (int,long)):
acceptable_input = True
return user_input
def separator():
return "\n---------------------------------------------\n"
def printseparator():
print("\n----------------------------------------------\n")
def printmenu(choices_dict,
menutitle = "Menu"):
new_screen(menutitle)
for choice in choices_dict:
print("{} : {}".format(choice, choices_dict[choice]))
printseparator()
def menu(choices_dict,
menutitle = "Menu",
optional_text=" ", prompt = "Please enter your choice: "):
""" displays a menu and returns the choice listed
Args:
choices_dict (dictionary) : ( int : "choice text")
menutitle (string) : optional text
Returns:
user_choice (int)
"""
printmenu(choices_dict,menutitle)
print(optional_text)
keys = choices_dict.keys()
valid_input = False
while not valid_input:
user_input = ask_int(prompt)
if user_input in keys:
valid_input = True
else:
printmenu(choices_dict,menutitle)
print(optional_text)
new_screen()
return user_input
if __name__ == "__main__":
""" Some testing code here """
choice = menu({1: "New Experiment",
2: "Load Experiment"})
print("You chose {}".format(choice))
usr_input = timed_query_yes_no("Try this (10 seconds)",10)
print usr_input