-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoptionsMenu.py
More file actions
71 lines (55 loc) · 3.01 KB
/
optionsMenu.py
File metadata and controls
71 lines (55 loc) · 3.01 KB
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
import StringIO
#Class to encapsulate the options menu that allows users to perform operations, add datasets, print results, etc.
class optionsMenu:
def __init__(self):
print "Options menu initiated"
def launchMenu(self, dist, dat, printFlag, saveFlag, saveFile):
#User must enter the number of the option listed below to activate the option
optionsList = ["1. Load dataset", "2. Normalize", "3. Use distance metric","4. Add new instance"]
print '\n'
for i in range (0, len(optionsList)):
print optionsList[i]
#Asks user to enter number corresponding to each feature option, which allows the program to perform the feature
us = raw_input("\nType number of desired option or q to quit\n")
while (us != 'q'):
#Program will ask for dataset file name and try to copy the data into the program's dataset object
if us == '1':
dataList = []
dataFile = raw_input("Enter name of file (including extension) containing dataset: ")
try:
f = open(dataFile, 'r')
#error handling for line format
for line in f:
arr = line.split()
dataList.append(arr)
dat.datSet = dataList
print "New dataset loaded \n"
except IOError:
print("Cannot open %r \n" % dataFile)
#Dataset will be normalized using the function defined in the class file dataSet.py
elif us == '2':
if (dat.datSet):
dat.normalize()
else:
print "\n No dataset loaded \n"
#User can choose to use distance metrics or add new one
elif us == '3':
dist.listMetrics()
x = dist.metricChoice(dat.datSet)
if (printFlag):
print "Metric result: {}".format(x)
print "Distance metric finished"
if (saveFlag):
with open(saveFile, "a") as f:
f.write(str(x))
#User can add new instance to the program's new instance class variable contained by dist
elif us == '4':
filename = raw_input("\n Enter file name of new instance (including extension) \n")
dist.addInstanceFromFile(filename)
else:
assert False, "unhandled option \n"
# The options list will continue to be printed until the user enters 'q'
for i in range (0, len(optionsList)):
print optionsList[i]
us = raw_input("New command?\n")
print "\n Program closed"