#------------------------------------------------------------------------------- # Name: ParameterEdit4 # Purpose: # # Author: Kelvin # # Created: 4/12/2013 #------------------------------------------------------------------------------- # Note to self: Make this program more modular and readable by splitting it into distinct parts and use perhaps use functions to implement them. # Note to self: Python can also be made into multi-file programs, so that it looks neater. Use 'import' to achieve that. # Define the file names involved. defaultParamFileName = '/u/hsu004/IntroToDuchamp/duchampDefaultParameters' oldParamFileName = '/u/hsu004/IntroToDuchamp/duchampHIPASS.in' newParamFileName = '/u/hsu004/Kelvin/duchampHIPASSedited.in' userSpecifiedParamFileName = newParamFileName # Define a simple function to determine the user's intent. # Note that this may be very inefficient. YES = 1 NO = 0 INVALID = -1 def toEdit(response): if (response == 'y') | (response == 'Y') | (response == 'yes'): return(YES) elif (response == 'n') | (response == 'N') | (response == 'no'): return(NO) else: return(INVALID) # Define a function to read parameter files with the specified format. def readParamFile(paramFileName): # Initialise an empty Dictionary. parameters = {} # Open the given parameter file. paramFile = open(paramFileName) # Load files for lines of parameter data in the specified format and # store them into the dictionary. for line in paramFile: if line[0] != '#' and len(line) > 1: linebits = line.split() parameters[linebits[0]] = linebits[1] # Go back to the start of the file. paramFile.seek(0, 0) paramFile.close() return parameters # Define a function to create a parameter file with the specified format. def writeParamFile(newParamFileName, parameters): # Create a new file. newParamFile = open(newParamFileName, 'w') # Write the editted set of parameters into the new file. for par in parameters: newParamFile.write('%s %s\n'%(par, parameters[par])) newParamFile.close(); # Define a function to print a list of parameters listed in a dictionary. def listParameters(parameters): for par in parameters: print(par, parameters[par]) print('\n') # This is the function that defines the shell interface # which prompts the user with options to edit the parameters in a given file. def editParameter(): # Firstly, determine the default list of parameters defaultParameters = readParamFile(defaultParamFileName) # Display the default set of parameters. print('Original set of Parameters: \n') listParameters(defaultParameters) parameters = readParamFile(oldParamFileName) # Display the original set of parameters. print('Original set of Parameters: \n') listParameters(parameters) # Prompt the user about whether they want to edit any parameters. keepEditting = raw_input('\nWould you like to edit any of the parameters? (Type "y" for yes): ') # This handles the case when no parameters are to be editted. if toEdit(keepEditting) == NO: print('\nNo parameters will be editted.') if toEdit(keepEditting) == INVALID: keepEditting = raw_input('\nInvalid Input. Did you mistype something? Did you want to edit any of the parameters? (Type "y" for yes): ') # This handles the case when the parameters are to be editted. while toEdit(keepEditting) == YES: # Prompt the user for the parameter name. parameter2edit = raw_input('\nWhich parameter would you like to edit? \n\tParameter: ') # If the parameter exists, edit the parameter value. # Note that the value is always stored as a string, even if the value is a 'number'. if parameter2edit in parameters: oldValue = parameters[parameter2edit] newValue = raw_input('\nPlease enter a new value or setting for "' + parameter2edit + '": ') parameters[parameter2edit] = newValue print('\n"' + parameter2edit + '" has been updated from ' + str(oldValue) + ' to ' + str(newValue) + '!') # Otherwise, if no such parameters exist, tell the user. else: print('\nThere is no such parameter as "' + parameter2edit + '" in your file "' + oldParamFileName + '"') # Prompt the user again if editting is to be continued. keepEditting = raw_input('\nWould you like to edit any other parameters? (Type "y" for yes): ') # Sometimes people mistype... if toEdit(keepEditting) == INVALID: keepEditting = raw_input('\nInvalid Input. Did you mistype something? Would you like to edit any other parameters? (Type "y" for yes): ') # Let the user know that the editting process has ended. if toEdit(keepEditting) == NO: print('\nFinished Editting.') elif toEdit(keepEditting) == INVALID: print('\nInvalid Input. No parameters will be editted.') # Display the editted set of parameters. print('\nEditted set of Parameters: \n'); listParameters(parameters) writeParamFile(newParamFileName, parameters) # This is the function that checks the parameter value of a given parameter. def checkParameter(): # Firstly, determine the default list of parameters defaultParameters = readParamFile(defaultParamFileName) # Then, determine the user specified list of parameters userSpecifiedParameters = readParamFile(userSpecifiedParamFileName) parameter2check = raw_input('\nWhich parameter would you like to check? \n\tParameter: ') if parameter2check in userSpecifiedParameters: print(parameter2check + ': ' + userSpecifiedParameters[parameter2check]) elif parameter2check in defaultParameters: print(parameter2check + ': ' + defaultParameters[parameter2check]) else: print('\n"' + parameter2check + '" does not exist.') #------------------------------------------------------------------------------- # GUI - Tkinter section from Tkinter import * class mainInterface: def __init__(self, master): self.frame = Frame(master) self.frame.pack() self.buttonEDIT = Button(self.frame, text = "Edit", command = editParameter) self.buttonEDIT.pack() self.buttonCHECK = Button(self.frame, text = "Check", command = checkParameter) self.buttonCHECK.pack() self.entry1 = Entry(self.frame, width = 25) self.entry1.pack() self.entry1.delete(0, END) self.entry1.insert(0, "A Value") self.button5 = Button(self.frame, text = "Create Button!", command = self.newButton) self.button5.pack(side = LEFT) self.button6 = Button(self.frame, text = "Print what's typed!", command = self.printTyped) self.button6.pack() self.buttonQUIT = Button(self.frame, text = "QUIT", fg = "red", command = self.frame.quit) self.buttonQUIT.pack() def newButton(self): self.buttonNEW = Button(self.frame, text = "New Button", command = self.newButton) self.buttonNEW.pack() print("New Button Created!") def printTyped(self): print(self.entry1.get()) def main(): root = Tk() interface = mainInterface(root) root.mainloop() root.destroy() #------------------------------------------------------------------------------- ##def main(): ## editParameter() ## checkParameter() if __name__ == '__main__': main()