Skip to content

Commit f555b2a

Browse files
brtieusmlindauer
authored andcommitted
Make a call to generate requirements.json in writeJSONFIles()
1 parent 0be128a commit f555b2a

File tree

5 files changed

+905
-18
lines changed

5 files changed

+905
-18
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"step": "install pandas",
4+
"command": "pip install pandas==1.2.4"
5+
},
6+
{
7+
"step": "install numpy",
8+
"command": "pip install numpy==1.20.1"
9+
},
10+
{
11+
"step": "install sklearn",
12+
"command": "pip install sklearn"
13+
}
14+
]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"step": "install pandas",
4+
"command": "pip install pandas==1.2.4"
5+
},
6+
{
7+
"step": "install numpy",
8+
"command": "pip install numpy==1.20.1"
9+
},
10+
{
11+
"step": "install sklearn",
12+
"command": "pip install sklearn"
13+
}
14+
]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"step": "install pandas",
4+
"command": "pip install pandas==1.2.4"
5+
},
6+
{
7+
"step": "install numpy",
8+
"command": "pip install numpy==1.20.1"
9+
},
10+
{
11+
"step": "install sklearn",
12+
"command": "pip install sklearn"
13+
}
14+
]

examples/pzmmModelImportExampleReqJSON.ipynb

Lines changed: 815 additions & 0 deletions
Large diffs are not rendered by default.

src/sasctl/pzmm/write_json_files.py

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import types
1414
import pickle
1515
import pickletools
16+
import os
1617
from pipreqs import pipreqs
1718

1819

@@ -57,7 +58,24 @@ def writeVarJSON(cls, inputData, isInput=True, jPath=Path.cwd()):
5758
else:
5859
isStr = False
5960

60-
if isStr:
61+
# loop through all predict variables to determine their name, length,
62+
# type, and level; append each to outputJSON
63+
for name in predictNames:
64+
if isSeries:
65+
predict = inputDF
66+
else:
67+
predict = inputDF[name]
68+
print(predict)
69+
firstRow = predict.loc[predict.first_valid_index()]
70+
dType = predict.dtypes.name
71+
isStr = type(firstRow) is str
72+
73+
if isStr:
74+
outputLevel = "nominal"
75+
outputType = "string"
76+
outputLength = predict.str.len().max()
77+
else:
78+
if dType == "category":
6179
outputLevel = "nominal"
6280
outputType = "string"
6381
outputLength = 8
@@ -975,7 +993,7 @@ def get_imports(self):
975993
elif isinstance(val, type):
976994
name = val.__module__.split(".")[0]
977995
yield name
978-
996+
979997
def get_pickle_file(self, pPath):
980998
"""
981999
Given a file path, retrieve the pickle file(s).
@@ -985,10 +1003,16 @@ def get_pickle_file(self, pPath):
9851003
pPath : str
9861004
File location for the input pickle file. Default is the current
9871005
working directory.
1006+
1007+
Returns
1008+
-------
1009+
list
1010+
A list of pickle files.
9881011
"""
989-
1012+
9901013
fileNames = []
9911014
fileNames.extend(sorted(Path(pPath).glob("*.pickle")))
1015+
return fileNames
9921016

9931017
def get_modules_from_pickle_file(self, pickle_file):
9941018
"""
@@ -1024,11 +1048,11 @@ def createRequirementsJSON(self, jPath=Path.cwd()):
10241048
The path to a Python project, by default Path.cwd().
10251049
"""
10261050

1027-
imports = list(set(self.get_imports()))
1051+
# imports = list(set(self.get_imports()))
10281052

1029-
with open("./imports.py", "w") as file:
1030-
for item in imports:
1031-
file.write("import %s\n" % item)
1053+
# with open(os.path.join(jPath, "imports.py"), "w") as file:
1054+
# for item in imports:
1055+
# file.write("import %s\n" % item)
10321056

10331057
pipreqs.init(
10341058
{
@@ -1045,17 +1069,18 @@ def createRequirementsJSON(self, jPath=Path.cwd()):
10451069
)
10461070

10471071
module_version_map = {}
1048-
pickle_file = self.get_pickle_file(jPath)
1049-
filename = "./requirements.txt"
1050-
with open(filename, "r") as f:
1072+
pickle_files = self.get_pickle_file(jPath)
1073+
requirements_txt_file = os.path.join(jPath, "requirements.txt")
1074+
with open(requirements_txt_file, "r") as f:
10511075
modules_requirements_txt = set()
1052-
modules_pickle = self.get_modules_from_pickle_file(pickle_file)
1053-
for line in f:
1054-
module_parts = line.rstrip().split("==")
1055-
module = module_parts[0]
1056-
version = module_parts[1]
1057-
module_version_map[module] = version
1058-
modules_requirements_txt.add(module)
1076+
for pickle_file in pickle_files:
1077+
modules_pickle = self.get_modules_from_pickle_file(pickle_file)
1078+
for line in f:
1079+
module_parts = line.rstrip().split("==")
1080+
module = module_parts[0]
1081+
version = module_parts[1]
1082+
module_version_map[module] = version
1083+
modules_requirements_txt.add(module)
10591084
pip_name_list = list(modules_requirements_txt.union(modules_pickle))
10601085

10611086
for item in pip_name_list:
@@ -1079,8 +1104,12 @@ def createRequirementsJSON(self, jPath=Path.cwd()):
10791104
],
10801105
indent=4,
10811106
)
1082-
with open("./requirements.json", "w") as file:
1107+
with open(os.path.join(jPath, "requirements.json"), "w") as file:
10831108
print(j, file=file)
1109+
1110+
# Delete requirements.txt file after requirements.json has been written.
1111+
os.remove(requirements_txt_file)
1112+
print('removed!')
10841113

10851114
def get_names(self, stream):
10861115
"""
@@ -1142,3 +1171,4 @@ def get_names(self, stream):
11421171
stack.append(arg)
11431172
else:
11441173
stack.extend(after)
1174+
# %%

0 commit comments

Comments
 (0)