source: branches/GUIdev/Original Trial Files (9 December 2013)/ParameterEdit4.py @ 1441

Last change on this file since 1441 was 1306, checked in by KelvinHsu, 11 years ago

Initial commit of early python tests

File size: 7.5 KB
Line 
1#-------------------------------------------------------------------------------
2# Name:        ParameterEdit4
3# Purpose:
4#
5# Author:      Kelvin
6#
7# Created:     4/12/2013
8#-------------------------------------------------------------------------------
9
10# Note to self: Make this program more modular and readable by splitting it into distinct parts and use perhaps use functions to implement them.
11# Note to self: Python can also be made into multi-file programs, so that it looks neater. Use 'import' to achieve that.
12
13
14# Define the file names involved.
15defaultParamFileName = '/u/hsu004/IntroToDuchamp/duchampDefaultParameters'
16oldParamFileName = '/u/hsu004/IntroToDuchamp/duchampHIPASS.in'
17newParamFileName = '/u/hsu004/Kelvin/duchampHIPASSedited.in'
18userSpecifiedParamFileName = newParamFileName
19
20
21# Define a simple function to determine the user's intent.
22# Note that this may be very inefficient.
23YES = 1
24NO = 0
25INVALID = -1
26def toEdit(response):
27    if (response == 'y') | (response == 'Y') | (response == 'yes'):
28        return(YES)
29    elif (response == 'n') | (response == 'N') | (response == 'no'):
30        return(NO)
31    else:
32        return(INVALID)
33
34   
35# Define a function to read parameter files with the specified format.
36def readParamFile(paramFileName):
37
38    # Initialise an empty Dictionary.
39    parameters = {}
40
41    # Open the given parameter file.
42    paramFile = open(paramFileName)
43
44    # Load files for lines of parameter data in the specified format and
45    # store them into the dictionary.
46    for line in paramFile:
47        if line[0] != '#' and len(line) > 1:
48            linebits = line.split()
49            parameters[linebits[0]] = linebits[1]
50
51    # Go back to the start of the file.
52    paramFile.seek(0, 0)
53
54    paramFile.close()
55
56    return parameters
57
58# Define a function to create a parameter file with the specified format.
59def writeParamFile(newParamFileName, parameters):
60
61    # Create a new file.
62    newParamFile = open(newParamFileName, 'w')
63
64    # Write the editted set of parameters into the new file.
65    for par in parameters:
66        newParamFile.write('%s    %s\n'%(par, parameters[par]))
67
68    newParamFile.close();
69
70# Define a function to print a list of parameters listed in a dictionary.
71def listParameters(parameters):
72
73    for par in parameters:
74        print(par, parameters[par])
75       
76    print('\n')
77
78# This is the function that defines the shell interface
79# which prompts the user with options to edit the parameters in a given file.
80def editParameter():
81   
82    # Firstly, determine the default list of parameters
83    defaultParameters = readParamFile(defaultParamFileName)
84
85    # Display the default set of parameters.
86    print('Original set of Parameters: \n')
87    listParameters(defaultParameters)
88
89    parameters = readParamFile(oldParamFileName)
90
91    # Display the original set of parameters.
92    print('Original set of Parameters: \n')
93    listParameters(parameters)
94
95    # Prompt the user about whether they want to edit any parameters.
96    keepEditting = raw_input('\nWould you like to edit any of the parameters? (Type "y" for yes): ')
97
98    # This handles the case when no parameters are to be editted.
99    if toEdit(keepEditting) == NO:
100        print('\nNo parameters will be editted.')
101       
102    if toEdit(keepEditting) == INVALID:
103        keepEditting = raw_input('\nInvalid Input. Did you mistype something? Did you want to edit any of the parameters? (Type "y" for yes): ')
104
105
106    # This handles the case when the parameters are to be editted.
107    while toEdit(keepEditting) == YES:
108
109        # Prompt the user for the parameter name.
110        parameter2edit = raw_input('\nWhich parameter would you like to edit? \n\tParameter: ')
111
112        # If the parameter exists, edit the parameter value.
113        # Note that the value is always stored as a string, even if the value is a 'number'.
114        if parameter2edit in parameters:
115
116            oldValue = parameters[parameter2edit]
117            newValue = raw_input('\nPlease enter a new value or setting for "' + parameter2edit + '": ')
118            parameters[parameter2edit] = newValue
119            print('\n"' + parameter2edit + '" has been updated from ' + str(oldValue) + ' to ' + str(newValue) + '!')
120
121        # Otherwise, if no such parameters exist, tell the user.   
122        else:
123            print('\nThere is no such parameter as "' +  parameter2edit + '" in your file "' + oldParamFileName + '"')
124
125        # Prompt the user again if editting is to be continued.
126        keepEditting = raw_input('\nWould you like to edit any other parameters? (Type "y" for yes): ')
127
128        # Sometimes people mistype...
129        if toEdit(keepEditting) == INVALID:
130            keepEditting = raw_input('\nInvalid Input. Did you mistype something? Would you like to edit any other parameters? (Type "y" for yes): ')
131
132    # Let the user know that the editting process has ended.
133    if toEdit(keepEditting) == NO:
134        print('\nFinished Editting.')
135    elif toEdit(keepEditting) == INVALID:
136        print('\nInvalid Input. No parameters will be editted.')
137       
138
139    # Display the editted set of parameters.
140    print('\nEditted set of Parameters: \n');
141    listParameters(parameters)
142       
143    writeParamFile(newParamFileName, parameters)
144
145# This is the function that checks the parameter value of a given parameter.
146def checkParameter():
147
148    # Firstly, determine the default list of parameters
149    defaultParameters = readParamFile(defaultParamFileName)   
150
151    # Then, determine the user specified list of parameters
152    userSpecifiedParameters = readParamFile(userSpecifiedParamFileName)
153
154    parameter2check = raw_input('\nWhich parameter would you like to check? \n\tParameter: ')
155
156    if parameter2check in userSpecifiedParameters:
157        print(parameter2check + ': ' + userSpecifiedParameters[parameter2check])
158    elif parameter2check in defaultParameters:
159        print(parameter2check + ': ' + defaultParameters[parameter2check])
160    else:
161        print('\n"' + parameter2check + '" does not exist.')
162
163#-------------------------------------------------------------------------------
164# GUI - Tkinter section
165from Tkinter import *
166 
167class mainInterface:
168 
169    def __init__(self, master):
170 
171        self.frame = Frame(master)
172        self.frame.pack()
173 
174        self.buttonEDIT = Button(self.frame, text = "Edit", command = editParameter)
175        self.buttonEDIT.pack()
176
177        self.buttonCHECK = Button(self.frame, text = "Check", command = checkParameter)
178        self.buttonCHECK.pack()
179
180        self.entry1 = Entry(self.frame, width = 25)
181        self.entry1.pack()
182
183        self.entry1.delete(0, END)
184        self.entry1.insert(0, "A Value")
185
186       
187       
188        self.button5 = Button(self.frame, text = "Create Button!", command = self.newButton)
189        self.button5.pack(side = LEFT)
190
191        self.button6 = Button(self.frame, text = "Print what's typed!", command = self.printTyped)
192        self.button6.pack()
193       
194        self.buttonQUIT = Button(self.frame, text = "QUIT", fg = "red", command = self.frame.quit)
195        self.buttonQUIT.pack()
196 
197    def newButton(self):
198        self.buttonNEW = Button(self.frame, text = "New Button", command = self.newButton)
199        self.buttonNEW.pack()
200        print("New Button Created!")
201
202    def printTyped(self):
203        print(self.entry1.get())
204 
205def main():
206    root = Tk()
207 
208    interface = mainInterface(root)
209
210 
211    root.mainloop()
212    root.destroy()
213
214#-------------------------------------------------------------------------------     
215   
216##def main():
217##    editParameter()
218##    checkParameter()
219   
220if __name__ == '__main__':
221    main()
Note: See TracBrowser for help on using the repository browser.