[1306] | 1 | #------------------------------------------------------------------------------- |
---|
| 2 | # Name: ParameterEdit2 |
---|
| 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 | |
---|
| 12 | # Define the file names involved. |
---|
| 13 | defaultParamFileName = '/u/hsu004/IntroToDuchamp/duchampDefaultParameters' |
---|
| 14 | oldParamFileName = '/u/hsu004/IntroToDuchamp/duchampHIPASS.in' |
---|
| 15 | newParamFileName = '/u/hsu004/Kelvin/duchampHIPASSedited.in' |
---|
| 16 | userSpecifiedParamFileName = newParamFileName |
---|
| 17 | |
---|
| 18 | |
---|
| 19 | # Define a simple function to determine the user's intent. |
---|
| 20 | # Note that this may be very inefficient. |
---|
| 21 | YES = 1 |
---|
| 22 | NO = 0 |
---|
| 23 | INVALID = -1 |
---|
| 24 | |
---|
| 25 | def toEdit(response): |
---|
| 26 | if (response == 'y') | (response == 'Y') | (response == 'yes'): |
---|
| 27 | return(YES) |
---|
| 28 | elif (response == 'n') | (response == 'N') | (response == 'no'): |
---|
| 29 | return(NO) |
---|
| 30 | else: |
---|
| 31 | return(INVALID) |
---|
| 32 | |
---|
| 33 | def readParamFile(paramFileName): |
---|
| 34 | |
---|
| 35 | # Initialise an empty Dictionary. |
---|
| 36 | parameters = {} |
---|
| 37 | |
---|
| 38 | # Open the given parameter file. |
---|
| 39 | paramFile = open(paramFileName) |
---|
| 40 | |
---|
| 41 | # Load files for lines of parameter data in the specified format and |
---|
| 42 | # store them into the dictionary. |
---|
| 43 | for line in paramFile: |
---|
| 44 | if line[0] != '#' and len(line) > 1: |
---|
| 45 | linebits = line.split() |
---|
| 46 | parameters[linebits[0]] = linebits[1] |
---|
| 47 | |
---|
| 48 | # Go back to the start of the file. |
---|
| 49 | paramFile.seek(0, 0) |
---|
| 50 | |
---|
| 51 | paramFile.close() |
---|
| 52 | |
---|
| 53 | return parameters |
---|
| 54 | |
---|
| 55 | def writeParamFile(newParamFileName, parameters): |
---|
| 56 | |
---|
| 57 | # Create a new file. |
---|
| 58 | newParamFile = open(newParamFileName, 'w') |
---|
| 59 | |
---|
| 60 | # Write the editted set of parameters into the new file. |
---|
| 61 | for par in parameters: |
---|
| 62 | newParamFile.write('%s %s\n'%(par, parameters[par])) |
---|
| 63 | |
---|
| 64 | newParamFile.close(); |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | def listParameters(parameters): |
---|
| 68 | |
---|
| 69 | for par in parameters: |
---|
| 70 | print(par, parameters[par]) |
---|
| 71 | |
---|
| 72 | print('\n') |
---|
| 73 | |
---|
| 74 | def editParameter(): |
---|
| 75 | |
---|
| 76 | # Firstly, determine the default list of parameters |
---|
| 77 | defaultParameters = readParamFile(defaultParamFileName) |
---|
| 78 | |
---|
| 79 | # Display the default set of parameters. |
---|
| 80 | print('Original set of Parameters: \n') |
---|
| 81 | listParameters(defaultParameters) |
---|
| 82 | |
---|
| 83 | parameters = readParamFile(oldParamFileName) |
---|
| 84 | |
---|
| 85 | # Display the original set of parameters. |
---|
| 86 | print('Original set of Parameters: \n') |
---|
| 87 | listParameters(parameters) |
---|
| 88 | |
---|
| 89 | # Prompt the user about whether they want to edit any parameters. |
---|
| 90 | keepEditting = raw_input('\nWould you like to edit any of the parameters? (Type "y" for yes): ') |
---|
| 91 | |
---|
| 92 | # This handles the case when no parameters are to be editted. |
---|
| 93 | if toEdit(keepEditting) == NO: |
---|
| 94 | print('\nNo parameters will be editted.') |
---|
| 95 | |
---|
| 96 | if toEdit(keepEditting) == INVALID: |
---|
| 97 | keepEditting = raw_input('\nInvalid Input. Did you mistype something? Did you want to edit any of the parameters? (Type "y" for yes): ') |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | # This handles the case when the parameters are to be editted. |
---|
| 101 | while toEdit(keepEditting) == YES: |
---|
| 102 | |
---|
| 103 | # Prompt the user for the parameter name. |
---|
| 104 | parameter2edit = raw_input('\nWhich parameter would you like to edit? \n\tParameter: ') |
---|
| 105 | |
---|
| 106 | # If the parameter exists, edit the parameter value. |
---|
| 107 | # Note that the value is always stored as a string, even if the value is a 'number'. |
---|
| 108 | if parameter2edit in parameters: |
---|
| 109 | |
---|
| 110 | oldValue = parameters[parameter2edit] |
---|
| 111 | newValue = raw_input('\nPlease enter a new value or setting for "' + parameter2edit + '": ') |
---|
| 112 | parameters[parameter2edit] = newValue |
---|
| 113 | print('\n"' + parameter2edit + '" has been updated from ' + str(oldValue) + ' to ' + str(newValue) + '!') |
---|
| 114 | |
---|
| 115 | # Otherwise, if no such parameters exist, tell the user. |
---|
| 116 | else: |
---|
| 117 | print('\nThere is no such parameter as "' + parameter2edit + '" in your file "' + oldParamFileName + '"') |
---|
| 118 | |
---|
| 119 | # Prompt the user again if editting is to be continued. |
---|
| 120 | keepEditting = raw_input('\nWould you like to edit any other parameters? (Type "y" for yes): ') |
---|
| 121 | |
---|
| 122 | # Sometimes people mistype... |
---|
| 123 | if toEdit(keepEditting) == INVALID: |
---|
| 124 | keepEditting = raw_input('\nInvalid Input. Did you mistype something? Would you like to edit any other parameters? (Type "y" for yes): ') |
---|
| 125 | |
---|
| 126 | # Let the user know that the editting process has ended. |
---|
| 127 | if toEdit(keepEditting) == NO: |
---|
| 128 | print('\nFinished Editting.') |
---|
| 129 | elif toEdit(keepEditting) == INVALID: |
---|
| 130 | print('\nInvalid Input. No parameters will be editted.') |
---|
| 131 | |
---|
| 132 | |
---|
| 133 | # Display the editted set of parameters. |
---|
| 134 | print('\nEditted set of Parameters: \n'); |
---|
| 135 | listParameters(parameters) |
---|
| 136 | |
---|
| 137 | writeParamFile(newParamFileName, parameters) |
---|
| 138 | |
---|
| 139 | def checkParameter(): |
---|
| 140 | |
---|
| 141 | # Firstly, determine the default list of parameters |
---|
| 142 | defaultParameters = readParamFile(defaultParamFileName) |
---|
| 143 | |
---|
| 144 | # Then, determine the user specified list of parameters |
---|
| 145 | userSpecifiedParameters = readParamFile(userSpecifiedParamFileName) |
---|
| 146 | |
---|
| 147 | parameter2check = raw_input('\nWhich parameter would you like to check? \n\tParameter: ') |
---|
| 148 | |
---|
| 149 | if parameter2check in userSpecifiedParameters: |
---|
| 150 | print(parameter2check + ': ' + userSpecifiedParameters[parameter2check]) |
---|
| 151 | elif parameter2check in defaultParameters: |
---|
| 152 | print(parameter2check + ': ' + defaultParameters[parameter2check]) |
---|
| 153 | else: |
---|
| 154 | print('\n"' + parameter2check + '" does not exist.') |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | def main(): |
---|
| 158 | editParameter() |
---|
| 159 | checkParameter() |
---|
| 160 | |
---|
| 161 | if __name__ == '__main__': |
---|
| 162 | main() |
---|