#------------------------------------------------------------------------------- # Name: ParameterEdit # 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. # Firstly, determine the default list of parameters defaultParameters = {}; defaultParamFile = open('/u/hsu004/IntroToDuchamp/duchampDefaultParameters'); for line in defaultParamFile: if line[0] != '#' and len(line) > 1: lineblocks = line.split() defaultParameters[lineblocks[0]] = lineblocks[1] # Display the default set of parameters. print('Original set of Parameters: \n'); for par in defaultParameters: print(par, defaultParameters[par]); print('\n'); # Initialise an empty Dictionary. parameters = {}; # Open the given parameter file. paramfile = open('/u/hsu004/IntroToDuchamp/duchampHIPASS.in'); # 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); # Display the original set of parameters. print('Original set of Parameters: \n'); for par in parameters: print(par, parameters[par]); print('\n'); # Define a simple function to determine the user's intent. # Note that this may be very inefficient. def toEdit(keepEditting): if (keepEditting == 'y') | (keepEditting == 'Y') | (keepEditting == 'yes'): return(1) elif (keepEditting == 'n') | (keepEditting == 'N') | (keepEditting == 'no'): return(0) else: return(-1) # 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) == 0: print('\nNo parameters will be editted.') if toEdit(keepEditting) == -1: print('\nInvalid Input. Did you mistype something? Did you want to edit any of the parameters? (Type "y" for yes): ') if toEdit(keepEditting) == -1: print('\nInvalid Input. No parameters will be editted.') # This handles the case when the parameters are to be editted. while toEdit(keepEditting) == 1: # 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 "' + paramfile.name + '"') # 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) == -1: 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) == 0: print('\nFinished Editting.') elif toEdit(keepEditting) == -1: print('\nInvalid Input. No parameters will be editted.') # Display the editted set of parameters. print('\nEditted set of Parameters: \n'); for par in parameters: print(par, parameters[par]); print('\n'); # Create a new file. newfile = open('/u/hsu004/Kelvin/duchampHIPASSedited.in', 'w') # Write the editted set of parameters into the new file. for par in parameters: newfile.write('%s %s\n'%(par, parameters[par])); newfile.close();