10
10
11
11
from pathlib import Path
12
12
13
+
13
14
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
15
16
scoreCodeType in the ModelProperties.json file should be labelled as 'Python'.
16
17
The file associated with the 'score' role should be a Python file.
17
18
@@ -22,63 +23,66 @@ def convertMetadata(zPath, pythonScoreCode=None):
22
23
pythonScoreCode : string, optional
23
24
File name of the Python score code. If None, then the name is
24
25
determined by the files in the model zip. Default value is None.
25
-
26
+
26
27
Returns
27
28
-------
28
29
scoreResource : string
29
30
File name of the score resource file.
30
31
pythonScoreCode : string
31
32
File name of the Python score code file.
32
-
33
+
33
34
Raises
34
35
------
35
- SyntaxError :
36
+ SyntaxError :
36
37
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
38
39
clarification as to which file is the Python score code.
39
- '''
40
+ """
40
41
# Replace the value of scoreCodeType to 'Python' in ModelProperties.json
41
42
with open (Path (zPath ) / "ModelProperties.json" , "r" ) as jFile :
42
43
modelProperties = json .loads (jFile .read ())
43
-
44
+
44
45
modelProperties .update ({"scoreCodeType" : "Python" })
45
46
propIndex = list (modelProperties .keys ())
46
47
propValues = list (modelProperties .values ())
47
48
outputJSON = pd .Series (propValues , index = propIndex )
48
-
49
+
49
50
with open (Path (zPath ) / "ModelProperties.json" , "w" ) as jFile :
50
51
dfDump = pd .Series .to_dict (outputJSON .transpose ())
51
52
json .dump (dfDump , jFile , indent = 4 , skipkeys = True )
52
53
print ("ModelProperties.json has been modified and rewritten for SAS Viya 4" )
53
-
54
+
54
55
# Replace the 'score' role file with Python score code file
55
56
with open (Path (zPath ) / "fileMetaData.json" , "r" ) as jFile :
56
57
metaData = json .loads (jFile .read ())
57
58
if pythonScoreCode is None :
58
59
numPyFiles = 0
59
- for file in zPath .glob (' *.py' ):
60
+ for file in zPath .glob (" *.py" ):
60
61
pythonScoreCode = file .name
61
62
numPyFiles = numPyFiles + 1
62
63
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
+ )
66
69
raise SyntaxError (message )
67
70
for i in range (len (metaData )):
68
71
if metaData [i ]["role" ] == "score" :
69
- metaData [i ].update ({"name" : pythonScoreCode })
72
+ metaData [i ].update ({"name" : pythonScoreCode })
70
73
if metaData [i ]["role" ] == "scoreResource" :
71
74
scoreResource = metaData [i ]["name" ]
72
75
with open (Path (zPath ) / "fileMetaData.json" , "w" ) as jFile :
73
76
json .dump (metaData , jFile , indent = 4 , skipkeys = True )
74
77
print ("fileMetaData.json has been modified and rewritten for SAS Viya 4" )
75
-
78
+
76
79
return scoreResource , pythonScoreCode
77
80
81
+
78
82
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
80
84
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
82
86
'settings.pickle_path' + <scoreResource>.
83
87
84
88
Parameters
@@ -89,14 +93,14 @@ def convertScoreCode(zPath, scoreResource, pythonScoreCode):
89
93
File name of the score resource file.
90
94
pythonScoreCode : string
91
95
File name of the Python score code file.
92
- '''
96
+ """
93
97
# Read entire text of score code
94
98
with open (Path (zPath ) / pythonScoreCode , "r" ) as pyFile :
95
99
scoreCode = pyFile .read ()
96
-
100
+
97
101
# Add the import settings line to the score code
98
102
scoreCode = "import settings\n " + scoreCode
99
-
103
+
100
104
# Search for all directory paths in score code that contain the scoreResource
101
105
oldString = re .findall (r"'\/.*?\.[\w:]+'" , scoreCode )
102
106
oldString = [s for s in oldString if scoreResource in s ]
@@ -106,28 +110,32 @@ def convertScoreCode(zPath, scoreResource, pythonScoreCode):
106
110
newString = "settings.pickle_path + '{}'" .format (scoreResource )
107
111
for oldStr in oldString :
108
112
scoreCode = scoreCode .replace (oldStr , newString )
109
-
113
+
110
114
# Write new text of score code to file
111
115
with open (Path (zPath ) / pythonScoreCode , "w" ) as pyFile :
112
116
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
+
114
121
115
122
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
117
124
needed in SAS Viya 4. These files are typically named score.sas,
118
125
dmcas_packagescorecode.sas, or dmcas_epscorescode.sas.
119
126
120
127
Parameters
121
128
----------
122
129
zPath : string or Path object
123
130
Location of files in the SAS Viya 3.5 model zip.
124
- '''
131
+ """
125
132
zPath = Path (zPath )
126
- for file in zPath .glob (' *.sas' ):
133
+ for file in zPath .glob (" *.sas" ):
127
134
file .unlink ()
128
135
136
+
129
137
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
131
139
SAS Viya 4. Then the function removes any .sas files, adjusts the ModelProperties.json
132
140
and fileMetaData.json files, and modifies the score code.
133
141
@@ -138,8 +146,7 @@ def convertModelZip(zPath, pythonScoreCode=None):
138
146
pythonScoreCode : string, optional
139
147
File name of the Python score code. If None, then the name is
140
148
determined by the files in the model zip. Default value is None.
141
- '''
149
+ """
142
150
deleteSASFiles (zPath )
143
151
scoreResource , pythonScoreCode = convertMetadata (zPath , pythonScoreCode = None )
144
152
convertScoreCode (zPath , scoreResource , pythonScoreCode )
145
-
0 commit comments