Skip to content

Commit 9fd70ee

Browse files
committed
black reformatting for modelMigration.py
1 parent 40b4dcf commit 9fd70ee

File tree

1 file changed

+35
-28
lines changed

1 file changed

+35
-28
lines changed

src/sasctl/utils/modelMigration.py

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111
from pathlib import Path
1212

13+
1314
def convertMetadata(zPath, pythonScoreCode=None):
14-
'''Modify the model metadata to match new requirements in SAS Viya 4. The
15+
"""Modify the model metadata to match new requirements in SAS Viya 4. The
1516
scoreCodeType in the ModelProperties.json file should be labelled as 'Python'.
1617
The file associated with the 'score' role should be a Python file.
1718
@@ -22,63 +23,66 @@ def convertMetadata(zPath, pythonScoreCode=None):
2223
pythonScoreCode : string, optional
2324
File name of the Python score code. If None, then the name is
2425
determined by the files in the model zip. Default value is None.
25-
26+
2627
Returns
2728
-------
2829
scoreResource : string
2930
File name of the score resource file.
3031
pythonScoreCode : string
3132
File name of the Python score code file.
32-
33+
3334
Raises
3435
------
35-
SyntaxError :
36+
SyntaxError :
3637
If no pythonScoreCode name is provided, but there are multiple Python
37-
files in the zPath, then a SyntaxError is raised asking for further
38+
files in the zPath, then a SyntaxError is raised asking for further
3839
clarification as to which file is the Python score code.
39-
'''
40+
"""
4041
# Replace the value of scoreCodeType to 'Python' in ModelProperties.json
4142
with open(Path(zPath) / "ModelProperties.json", "r") as jFile:
4243
modelProperties = json.loads(jFile.read())
43-
44+
4445
modelProperties.update({"scoreCodeType": "Python"})
4546
propIndex = list(modelProperties.keys())
4647
propValues = list(modelProperties.values())
4748
outputJSON = pd.Series(propValues, index=propIndex)
48-
49+
4950
with open(Path(zPath) / "ModelProperties.json", "w") as jFile:
5051
dfDump = pd.Series.to_dict(outputJSON.transpose())
5152
json.dump(dfDump, jFile, indent=4, skipkeys=True)
5253
print("ModelProperties.json has been modified and rewritten for SAS Viya 4")
53-
54+
5455
# Replace the 'score' role file with Python score code file
5556
with open(Path(zPath) / "fileMetaData.json", "r") as jFile:
5657
metaData = json.loads(jFile.read())
5758
if pythonScoreCode is None:
5859
numPyFiles = 0
59-
for file in zPath.glob('*.py'):
60+
for file in zPath.glob("*.py"):
6061
pythonScoreCode = file.name
6162
numPyFiles = numPyFiles + 1
6263
if numPyFiles > 1:
63-
message = ("More than one Python file was found, therefore the score code" +
64-
" the score code could not be determined. Please provide the" +
65-
" name of the Python score code file as an argument.")
64+
message = (
65+
"More than one Python file was found, therefore the score code"
66+
+ " the score code could not be determined. Please provide the"
67+
+ " name of the Python score code file as an argument."
68+
)
6669
raise SyntaxError(message)
6770
for i in range(len(metaData)):
6871
if metaData[i]["role"] == "score":
69-
metaData[i].update({"name" : pythonScoreCode})
72+
metaData[i].update({"name": pythonScoreCode})
7073
if metaData[i]["role"] == "scoreResource":
7174
scoreResource = metaData[i]["name"]
7275
with open(Path(zPath) / "fileMetaData.json", "w") as jFile:
7376
json.dump(metaData, jFile, indent=4, skipkeys=True)
7477
print("fileMetaData.json has been modified and rewritten for SAS Viya 4")
75-
78+
7679
return scoreResource, pythonScoreCode
7780

81+
7882
def convertScoreCode(zPath, scoreResource, pythonScoreCode):
79-
'''Convert the Python score code used in SAS Viya 3.5 into score code usable in
83+
"""Convert the Python score code used in SAS Viya 3.5 into score code usable in
8084
SAS Viya 4. The two adjustments are including an 'import settings' statement and
81-
replacing score resource access calls that reference an explicit path with
85+
replacing score resource access calls that reference an explicit path with
8286
'settings.pickle_path' + <scoreResource>.
8387
8488
Parameters
@@ -89,14 +93,14 @@ def convertScoreCode(zPath, scoreResource, pythonScoreCode):
8993
File name of the score resource file.
9094
pythonScoreCode : string
9195
File name of the Python score code file.
92-
'''
96+
"""
9397
# Read entire text of score code
9498
with open(Path(zPath) / pythonScoreCode, "r") as pyFile:
9599
scoreCode = pyFile.read()
96-
100+
97101
# Add the import settings line to the score code
98102
scoreCode = "import settings\n" + scoreCode
99-
103+
100104
# Search for all directory paths in score code that contain the scoreResource
101105
oldString = re.findall(r"'\/.*?\.[\w:]+'", scoreCode)
102106
oldString = [s for s in oldString if scoreResource in s]
@@ -106,28 +110,32 @@ def convertScoreCode(zPath, scoreResource, pythonScoreCode):
106110
newString = "settings.pickle_path + '{}'".format(scoreResource)
107111
for oldStr in oldString:
108112
scoreCode = scoreCode.replace(oldStr, newString)
109-
113+
110114
# Write new text of score code to file
111115
with open(Path(zPath) / pythonScoreCode, "w") as pyFile:
112116
pyFile.write(scoreCode)
113-
print("{} has been modified and rewritten for SAS Viya 4".format(pythonScoreCode))
117+
print(
118+
"{} has been modified and rewritten for SAS Viya 4".format(pythonScoreCode)
119+
)
120+
114121

115122
def deleteSASFiles(zPath):
116-
'''Remove .sas score files created for SAS Viya 3.5, which are no longer
123+
"""Remove .sas score files created for SAS Viya 3.5, which are no longer
117124
needed in SAS Viya 4. These files are typically named score.sas,
118125
dmcas_packagescorecode.sas, or dmcas_epscorescode.sas.
119126
120127
Parameters
121128
----------
122129
zPath : string or Path object
123130
Location of files in the SAS Viya 3.5 model zip.
124-
'''
131+
"""
125132
zPath = Path(zPath)
126-
for file in zPath.glob('*.sas'):
133+
for file in zPath.glob("*.sas"):
127134
file.unlink()
128135

136+
129137
def convertModelZip(zPath, pythonScoreCode=None):
130-
'''Pass the directory path of the model to be converted from SAS Viya 3.5 to
138+
"""Pass the directory path of the model to be converted from SAS Viya 3.5 to
131139
SAS Viya 4. Then the function removes any .sas files, adjusts the ModelProperties.json
132140
and fileMetaData.json files, and modifies the score code.
133141
@@ -138,8 +146,7 @@ def convertModelZip(zPath, pythonScoreCode=None):
138146
pythonScoreCode : string, optional
139147
File name of the Python score code. If None, then the name is
140148
determined by the files in the model zip. Default value is None.
141-
'''
149+
"""
142150
deleteSASFiles(zPath)
143151
scoreResource, pythonScoreCode = convertMetadata(zPath, pythonScoreCode=None)
144152
convertScoreCode(zPath, scoreResource, pythonScoreCode)
145-

0 commit comments

Comments
 (0)