13
13
import types
14
14
import pickle
15
15
import pickletools
16
+ import os
16
17
from pipreqs import pipreqs
17
18
18
19
@@ -57,7 +58,24 @@ def writeVarJSON(cls, inputData, isInput=True, jPath=Path.cwd()):
57
58
else :
58
59
isStr = False
59
60
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" :
61
79
outputLevel = "nominal"
62
80
outputType = "string"
63
81
outputLength = 8
@@ -975,7 +993,7 @@ def get_imports(self):
975
993
elif isinstance (val , type ):
976
994
name = val .__module__ .split ("." )[0 ]
977
995
yield name
978
-
996
+
979
997
def get_pickle_file (self , pPath ):
980
998
"""
981
999
Given a file path, retrieve the pickle file(s).
@@ -985,10 +1003,16 @@ def get_pickle_file(self, pPath):
985
1003
pPath : str
986
1004
File location for the input pickle file. Default is the current
987
1005
working directory.
1006
+
1007
+ Returns
1008
+ -------
1009
+ list
1010
+ A list of pickle files.
988
1011
"""
989
-
1012
+
990
1013
fileNames = []
991
1014
fileNames .extend (sorted (Path (pPath ).glob ("*.pickle" )))
1015
+ return fileNames
992
1016
993
1017
def get_modules_from_pickle_file (self , pickle_file ):
994
1018
"""
@@ -1024,11 +1048,11 @@ def createRequirementsJSON(self, jPath=Path.cwd()):
1024
1048
The path to a Python project, by default Path.cwd().
1025
1049
"""
1026
1050
1027
- imports = list (set (self .get_imports ()))
1051
+ # imports = list(set(self.get_imports()))
1028
1052
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)
1032
1056
1033
1057
pipreqs .init (
1034
1058
{
@@ -1045,17 +1069,18 @@ def createRequirementsJSON(self, jPath=Path.cwd()):
1045
1069
)
1046
1070
1047
1071
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 :
1051
1075
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 )
1059
1084
pip_name_list = list (modules_requirements_txt .union (modules_pickle ))
1060
1085
1061
1086
for item in pip_name_list :
@@ -1079,8 +1104,12 @@ def createRequirementsJSON(self, jPath=Path.cwd()):
1079
1104
],
1080
1105
indent = 4 ,
1081
1106
)
1082
- with open ("./ requirements.json" , "w" ) as file :
1107
+ with open (os . path . join ( jPath , " requirements.json") , "w" ) as file :
1083
1108
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!' )
1084
1113
1085
1114
def get_names (self , stream ):
1086
1115
"""
@@ -1142,3 +1171,4 @@ def get_names(self, stream):
1142
1171
stack .append (arg )
1143
1172
else :
1144
1173
stack .extend (after )
1174
+ # %%
0 commit comments