Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactoring] making the code more Pythonic #1930

Merged
merged 7 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions developer_tools/createRegressionTestDocumentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,18 @@ def noDescriptionTestsAndInformationOnTheOther(self):
for testInfoFile in __testInfoList:
if 'moose' in testInfoFile.split(os.sep) or not os.path.isfile(testInfoFile):
continue
fileObject = open(testInfoFile,"r+")
fileLines = fileObject.readlines()
dirName = os.path.dirname(testInfoFile)
# I do not want to use getpot!
for line in fileLines:
if line.strip().startswith("input"):
fileName = line.split("=")[-1].replace("'", "").replace('"', '').rstrip().strip()
fileName = os.path.join(dirName,fileName)
if os.path.split(fileName)[-1].lower().endswith('xml'):
__testList.append(os.path.abspath(fileName))
if os.path.split(fileName)[-1].lower().endswith('py'):
__testList.append(os.path.abspath(fileName))
fileObject.close()
with open(testInfoFile,"r+") as fileObject:
fileLines = fileObject.readlines()
dirName = os.path.dirname(testInfoFile)
# I do not want to use getpot!
for line in fileLines:
if line.strip().startswith("input"):
fileName = line.split("=")[-1].replace("'", "").replace('"', '').rstrip().strip()
fileName = os.path.join(dirName,fileName)
if os.path.split(fileName)[-1].lower().endswith('xml'):
__testList.append(os.path.abspath(fileName))
if os.path.split(fileName)[-1].lower().endswith('py'):
__testList.append(os.path.abspath(fileName))
for testFile in __testList:
if testFile.endswith('xml'):
try: root = ET.parse(testFile).getroot()
Expand Down
23 changes: 11 additions & 12 deletions developer_tools/readRequirementsAndCreateLatex.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,17 @@ def writeLatexFromDictionaryOfRequirements(applicationName,requirementGroups,out
"""
sections = ["section","subsection","subsubsection","paragraph"]
indexSec = 0
fileObject = open(outputFileName,"w+")
fileObject.write("\\"+sections[0].strip()+"{System Requirements: " +applicationName.strip()+"}\n")
for group, value in requirementGroups.items():
fileObject.write("\\"+sections[1].strip()+"{" +group.strip()+"}\n")
for reqSet, requirements in value.items():
# construct table
fileObject.write("\\"+sections[2].strip()+"{" +reqSet.strip()+"}\n")
# start write the requirements for this set
for req, content in requirements.items():
fileObject.write("\\"+sections[3].strip()+"{" +req.strip()+"} \n")
fileObject.write(content['description'].strip() + "\n")
fileObject.close()
with open(outputFileName,"w+") as fileObject:
fileObject.write("\\"+sections[0].strip()+"{System Requirements: " +applicationName.strip()+"}\n")
for group, value in requirementGroups.items():
fileObject.write("\\"+sections[1].strip()+"{" +group.strip()+"}\n")
for reqSet, requirements in value.items():
# construct table
fileObject.write("\\"+sections[2].strip()+"{" +reqSet.strip()+"}\n")
# start write the requirements for this set
for req, content in requirements.items():
fileObject.write("\\"+sections[3].strip()+"{" +req.strip()+"} \n")
fileObject.write(content['description'].strip() + "\n")


def readRequirementsXml(fileName):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,12 @@ def writeNewInput(self,outfile=None):
"""
if outfile==None:
outfile = self.inputfile
IOfile = open(outfile,'w')
for e, entry in enumerate(self.fileOrderStorage):
if type(entry) == unicode:
IOfile.writelines(entry)
elif type(entry) == list:
DictBlockName = self.fileOrderStorage[e][0]
DictBlock = self.AllVarDict[DictBlockName]
for key, value in DictBlock.items():
IOfile.writelines(DictBlockName + "['" + key + "'] = " + str(value) + '\n')
IOfile.close()
with open(outfile,'w') as IOfile:
for e, entry in enumerate(self.fileOrderStorage):
if type(entry) == unicode:
IOfile.writelines(entry)
elif type(entry) == list:
DictBlockName = self.fileOrderStorage[e][0]
DictBlock = self.AllVarDict[DictBlockName]
for key, value in DictBlock.items():
IOfile.writelines(DictBlockName + "['" + key + "'] = " + str(value) + '\n')
15 changes: 7 additions & 8 deletions ravenframework/CodeInterfaceClasses/MooseBasedApp/CUBITparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,10 @@ def writeNewInput(self,outFile=None):
"""
if outFile == None:
outFile = self.inputfile
IOfile = open(outFile,'w')
for entry in self.fileOrderStorage:
if type(entry) == unicode:
IOfile.writelines(entry)
elif type(entry) == list:
for key, value in self.keywordDictionary.items():
IOfile.writelines('#{ '+key+' = '+str(value)+'}'+'\n')
IOfile.close()
with open(outFile,'w') as IOfile:
for entry in self.fileOrderStorage:
if type(entry) == unicode:
IOfile.writelines(entry)
elif type(entry) == list:
for key, value in self.keywordDictionary.items():
IOfile.writelines('#{ '+key+' = '+str(value)+'}'+'\n')
23 changes: 10 additions & 13 deletions ravenframework/CodeInterfaceClasses/Neutrino/neutrinoInterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,23 +203,20 @@ def finalizeCodeOutput(self, command, output, workingDir):
return newOutputPath'''

# open original output file (the working directory is provided)
outputFile = open(outputPath,"r+")
with open(outputPath,"r+") as outputFile:
#Open the new output file so the results can be written to it and put in the form for RAVEN to read
with open(newOutputPath + ".csv", 'w') as resultsFile:

#Open the new output file so the results can be written to it and put in the form for RAVEN to read
resultsFile = open(newOutputPath + ".csv", 'w')
lines = outputFile.readlines()

lines = outputFile.readlines()
#Needed for RAVEN to read output
#These need to match RAVEN input file output names
resultsFile.write('time,result\n')

#Needed for RAVEN to read output
#These need to match RAVEN input file output names
resultsFile.write('time,result\n')
#Write Neutrino results to a new file for RAVEN
for line in lines:
resultsFile.write(line)

#Write Neutrino results to a new file for RAVEN
for line in lines:
resultsFile.write(line)

resultsFile.close()

outputFile.close()

return newOutputPath
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,11 @@ def finalizeCodeOutput(self, command, output, workingDir):
print('sourcefilename:',sourceFileName)
destFileName = sourceFileName.replace('rawout~', 'out~') # When fix the CSV, change rawout~ to out~
sourceFileName += '.csv'
inputFile = open(sourceFileName)
for line in inputFile:
# Line ends with a comma followed by a newline
#XXX toBytes seems to be needed here in python3, despite the text = True
os.write(tempOutputFD, utils.toBytes(line.replace('"','').strip().strip(',') + '\n'))
inputFile.close()
with open(sourceFileName) as inputFile:
for line in inputFile:
# Line ends with a comma followed by a newline
#XXX toBytes seems to be needed here in python3, despite the text = True
os.write(tempOutputFD, utils.toBytes(line.replace('"','').strip().strip(',') + '\n'))
os.close(tempOutputFD)
shutil.move(tempOutputFileName, destFileName + '.csv')
return destFileName # Return the name without the .csv on it...RAVEN will add it
5 changes: 2 additions & 3 deletions ravenframework/CodeInterfaceClasses/PHISICS/DecayParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ def __init__(self, inputFiles, workingDir, **pertDict):
self.inputFiles = inputFiles
self.pertDict = self.scientificNotation(pertDict)
# open the unperturbed file
openInputFile = open(self.inputFiles, "r")
lines = openInputFile.readlines()
openInputFile.close()
with open(self.inputFiles, "r") as openInputFile:
lines = openInputFile.readlines()

self.characterizeLibrary(lines)
self.fileReconstruction()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ def __init__(self, inputFiles, workingDir, **pertDict):
pertDict) # Perturbed variables

# open the unperturbed file
openInputFile = open(self.inputFiles, "r")
lines = openInputFile.readlines()
openInputFile.close()
with open(self.inputFiles, "r") as openInputFile:
lines = openInputFile.readlines()

self.characterizeLibrary(lines)
self.isotopeList = list(set(
Expand Down
5 changes: 2 additions & 3 deletions ravenframework/CodeInterfaceClasses/PHISICS/MassParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,8 @@ def printInput(self, workingDir):
@ Out, None
"""
# open the unperturbed file
openInputFile = open(self.inputFiles, "r")
lines = openInputFile.readlines()
openInputFile.close()
with open(self.inputFiles, "r") as openInputFile:
lines = openInputFile.readlines()

# remove the file if was already existing
if os.path.exists(self.inputFiles):
Expand Down
5 changes: 2 additions & 3 deletions ravenframework/CodeInterfaceClasses/PHISICS/PathParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ def printInput(self, workingDir):
@ Out, None
"""
# open the unperturbed file
openInputFile = open(self.inputFiles, "r")
lines = openInputFile.readlines()
openInputFile.close()
with open(self.inputFiles, "r") as openInputFile:
lines = openInputFile.readlines()

# remove the file if was already existing
if os.path.exists(self.inputFiles):
Expand Down
5 changes: 2 additions & 3 deletions ravenframework/CodeInterfaceClasses/PHISICS/QValuesParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,8 @@ def printInput(self, workingDir):
@ Out, None
"""
# open the unperturbed file
openInputFile = open(self.inputFiles, "r")
lines = openInputFile.readlines()
openInputFile.close()
with open(self.inputFiles, "r") as openInputFile:
lines = openInputFile.readlines()

# remove the file if was already existing
if os.path.exists(self.inputFiles):
Expand Down
5 changes: 2 additions & 3 deletions ravenframework/CodeInterfaceClasses/PHISICS/XSCreator.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ def generateXML(self,workingDir,bool,inputFiles,tabMapFileName):
for count,(group,value) in enumerate(self.listedDict.get('XS').get(tabulation).get(material).get(isotope).get(typeOfXs).get(reaction).items()):
reactionChild = SubElement(libraryChild, self.formatXS(reaction), {'g':group})
reactionChild.text = value
fileObj = open(inputFiles, 'w')
fileObj.write(self.prettify(top))
fileObj.close()
with open(inputFiles, 'w') as fileObj:
fileObj.write(self.prettify(top))

def formatXS(self,reaction):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,9 @@ def createNewInput(self, inputs, oinputs, samplerType, **Kwargs):
elif line.lstrip().startswith("--output-directory="):
self._outputDirectory = line.split("=",1)[1].rstrip()
newLines.append(line)
newFile = open(singleInput.getAbsFile(),"w")
for line in newLines:
newFile.write(line)
newFile.close()
with open(singleInput.getAbsFile(),"w") as newFile:
for line in newLines:
newFile.write(line)
elif singleInput.getType() == 'PrescientInput':
#print("SampledVars", Kwargs["SampledVars"])
#print("Modifying", singleInput)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,16 +456,15 @@ def writeNewInput(self,inFiles=None,**Kwargs):
@ Out, None.
"""
for outFile in inFiles:
newFile = open(outFile.getAbsFile(),'w')
tree = self.xmlsDict[outFile.getFilename()]
root = tree.getroot()
for child in root:
for mat in child:
matID = mat.attrib['ID'].strip()
if matID not in self.aliases.keys():
continue
self._replaceXMLNodeText(mat,self.pertLib[matID])
with open(outFile.getAbsFile(),'w') as newFile:
tree = self.xmlsDict[outFile.getFilename()]
root = tree.getroot()
for child in root:
for mat in child:
matID = mat.attrib['ID'].strip()
if matID not in self.aliases.keys():
continue
self._replaceXMLNodeText(mat,self.pertLib[matID])

toWrite = self._prettify(tree)
newFile.writelines(toWrite)
newFile.close()
toWrite = self._prettify(tree)
newFile.writelines(toWrite)
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,5 @@ def writeXmlForDET(filename,trigger,listDict,stopInfo):
var.set('actual_value',str(varInfo['new_value']))
if 'associated_pb' in varInfo.keys():
var.set('probability',str(varInfo['associated_pb']))
fileObject = open(filename,'w')
fileObject.write(minidom.parseString(ET.tostring(root, 'utf-8')).toprettyxml(indent="\t"))
fileObject.close()
with open(filename,'w') as fileObject:
fileObject.write(minidom.parseString(ET.tostring(root, 'utf-8')).toprettyxml(indent="\t"))