Skip to content

Commit 1108ccf

Browse files
committed
Remove pipreqs method and begin reformat
1 parent 648e30b commit 1108ccf

File tree

1 file changed

+20
-51
lines changed

1 file changed

+20
-51
lines changed

src/sasctl/pzmm/write_json_files.py

Lines changed: 20 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import pickle
1515
import pickletools
1616
import os
17-
from pipreqs import pipreqs
1817

1918
# %%
2019
class JSONFiles:
@@ -58,17 +57,6 @@ def writeVarJSON(cls, inputData, isInput=True, jPath=Path.cwd()):
5857
else:
5958
isStr = False
6059

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-
firstRow = predict.loc[predict.first_valid_index()]
69-
dType = predict.dtypes.name
70-
isStr = type(firstRow) is str
71-
7260
if isStr:
7361
outputLevel = "nominal"
7462
outputType = "string"
@@ -315,6 +303,7 @@ def writeFileMetadataJSON(cls, modelPrefix, jPath=Path.cwd(), isH2OModel=False):
315303
)
316304
)
317305

306+
@classmethod
318307
def writeBaseFitStat(
319308
self, csvPath=None, jPath=Path.cwd(), userInput=False, tupleList=None
320309
):
@@ -448,6 +437,7 @@ def writeBaseFitStat(
448437
)
449438
)
450439

440+
@classmethod
451441
def calculateFitStat(
452442
self, validateData=None, trainData=None, testData=None, jPath=Path.cwd()
453443
):
@@ -606,6 +596,7 @@ def calculateFitStat(
606596
)
607597
)
608598

599+
@classmethod
609600
def generateROCLiftStat(
610601
self,
611602
targetName,
@@ -974,9 +965,9 @@ def convertDataRole(self, dataRole):
974965

975966
return conversion
976967

977-
def get_imports(self):
968+
def getCurrentScopedImports(self):
978969
"""
979-
Gets the Python modules from the current scope's global variables.
970+
Gets the Python modules from the current scope's global variables.
980971
981972
Yields
982973
-------
@@ -992,8 +983,8 @@ def get_imports(self):
992983
elif isinstance(val, type):
993984
name = val.__module__.split(".")[0]
994985
yield name
995-
996-
def get_pickle_file(self, pPath):
986+
987+
def getPickleFile(self, pPath):
997988
"""
998989
Given a file path, retrieve the pickle file(s).
999990
@@ -1002,21 +993,21 @@ def get_pickle_file(self, pPath):
1002993
pPath : str
1003994
File location for the input pickle file. Default is the current
1004995
working directory.
1005-
996+
1006997
Returns
1007998
-------
1008999
list
10091000
A list of pickle files.
10101001
"""
1011-
1002+
10121003
fileNames = []
10131004
fileNames.extend(sorted(Path(pPath).glob("*.pickle")))
10141005
return fileNames
10151006

1016-
def get_modules_from_pickle_file(self, pickle_file):
1007+
def getDependenciesFromPickleFile(self, pickleFile):
10171008
"""
1018-
Reads the pickled byte stream from a file object, serializes the pickled byte
1019-
stream as a bytes object, and inspects the bytes object for all Python modules
1009+
Reads the pickled byte stream from a file object, serializes the pickled byte
1010+
stream as a bytes object, and inspects the bytes object for all Python modules
10201011
and aggregates them in a set.
10211012
10221013
Parameters
@@ -1030,13 +1021,14 @@ def get_modules_from_pickle_file(self, pickle_file):
10301021
A set of modules obtained from the pickle stream.
10311022
"""
10321023

1033-
with (open(pickle_file, "rb")) as openfile:
1024+
with (open(pickleFile, "rb")) as openfile:
10341025
obj = pickle.load(openfile)
10351026
dumps = pickle.dumps(obj)
10361027

1037-
modules = {mod.split(".")[0] for mod, _ in self.get_names(dumps)}
1028+
modules = {mod.split(".")[0] for mod, _ in self.getNames(dumps)}
10381029
return modules
10391030

1031+
@classmethod
10401032
def createRequirementsJSON(self, jPath=Path.cwd()):
10411033
"""
10421034
Searches the root of the project for all Python modules and writes them to a requirements.json file.
@@ -1047,32 +1039,12 @@ def createRequirementsJSON(self, jPath=Path.cwd()):
10471039
The path to a Python project, by default Path.cwd().
10481040
"""
10491041

1050-
# imports = list(set(self.get_imports()))
1051-
1052-
# with open(os.path.join(jPath, "imports.py"), "w") as file:
1053-
# for item in imports:
1054-
# file.write("import %s\n" % item)
1055-
1056-
pipreqs.init(
1057-
{
1058-
"<path>": jPath,
1059-
"--savepath": None,
1060-
"--print": False,
1061-
"--use-local": None,
1062-
"--force": True,
1063-
"--proxy": None,
1064-
"--pypi-server": None,
1065-
"--diff": None,
1066-
"--clean": None,
1067-
}
1068-
)
1069-
10701042
module_version_map = {}
10711043
pickle_files = self.get_pickle_file(jPath)
10721044
requirements_txt_file = os.path.join(jPath, "requirements.txt")
10731045
with open(requirements_txt_file, "r") as f:
10741046
modules_requirements_txt = set()
1075-
for pickle_file in pickle_files:
1047+
for pickle_file in pickle_files:
10761048
modules_pickle = self.get_modules_from_pickle_file(pickle_file)
10771049
for line in f:
10781050
module_parts = line.rstrip().split("==")
@@ -1105,13 +1077,11 @@ def createRequirementsJSON(self, jPath=Path.cwd()):
11051077
)
11061078
with open(os.path.join(jPath, "requirements.json"), "w") as file:
11071079
print(j, file=file)
1108-
1109-
# Delete requirements.txt file after requirements.json has been written.
1110-
os.remove(requirements_txt_file)
11111080

1112-
def get_names(self, stream):
1081+
def getNames(self, stream):
11131082
"""
1114-
Generates (module, class_name) tuples from a pickle stream. Extracts all class names referenced by GLOBAL and STACK_GLOBAL opcodes.
1083+
Generates (module, class_name) tuples from a pickle stream. Extracts all class names referenced
1084+
by GLOBAL and STACK_GLOBAL opcodes.
11151085
11161086
Credit: https://stackoverflow.com/questions/64850179/inspecting-a-pickle-dump-for-dependencies
11171087
More information here: https://github.com/python/cpython/blob/main/Lib/pickletools.py
@@ -1168,5 +1138,4 @@ def get_names(self, stream):
11681138
if len(after) == 1 and op.arg is not None:
11691139
stack.append(arg)
11701140
else:
1171-
stack.extend(after)
1172-
1141+
stack.extend(after)

0 commit comments

Comments
 (0)