1- # _bundle.py contains private methods for gzip bundling of object
1+ # _bundle.py contains private methods for gzip bundling of the model object
22import gzip
33import pickle
44import os
77import sclblpy ._globals as glob
88
99
10- def _gzip_save (object , filename : str = glob .BUNDLE_NAME , _verbose = False ):
10+ def _gzip_save (object , filename : str = glob .GZIP_BUNDLE ):
1111 """Saves a compressed object to disk.
1212
1313 Function is used to pickle and gzip an sclblpy model object as created
1414 by the upload() function. This package is send to the toolchain.
1515
1616 Args:
1717 object: A dictionary containing all the information to be send.
18- filename: a string stating where to store the .zip file. Default glob.BUNDLE_NAME
19- _verbose: Bool indicating whether feedback should be printed. Default False.
18+ filename: a string stating where to store the .zip file. Default glob.GZIP_BUNDLE
2019
2120 Returns:
21+ True if the bundle is successfully saved, False if not.
2222
23- Raises:
23+ Raises (in debug mode) :
2424 ModelBundleError if unable to store the model bundle.
2525
2626 """
2727 try :
28+ os .makedirs (os .path .dirname (filename ), exist_ok = True ) # create the folder if it does not exists.
2829 fp = gzip .open (filename , 'wb' )
29- pickle .dump (object , fp )
30+ pickle .dump (object , fp , protocol = 4 ) # protocol = 4 is python 3.4 or higher
3031 fp .close ()
31- if _verbose :
32- print ("File successfully stored." )
32+ if glob .DEBUG :
33+ print ("Bundle message: File successfully stored." )
34+ return True
3335 except Exception as e :
34- if _verbose :
35- print ("Exception: " + str (e ))
36- raise ModelBundleError ("Unable to pickle and gzip your model." )
36+ if glob .DEBUG :
37+ print ("Bundle exception: " + str (e ))
38+ raise ModelBundleError ("Bundle error: Unable to pickle and gzip your model." )
39+ return False
3740
38- return
3941
40-
41- def _gzip_load (filename : str = glob .BUNDLE_NAME , _verbose = True ):
42+ def _gzip_load (filename : str = glob .GZIP_BUNDLE ):
4243 """Loads a compressed object from disk.
4344
4445 Currently not used in the sclblpy package but syntax should be used on the toolchain side.
4546
4647 Args:
47- filename: Name of the gzipped pickle. Default "temp_sclbl_mod.gzip" .
48- _verbose: Bool indicating whether feedback should be printed. Default True.
48+ filename: Name of the gzipped pickle. Default glob.GZIP_BUNDLE .
49+
4950 Returns:
5051 obj: A dictonary containing the unpickled and unzipped contents of the file. Empty dict if unable to open.
52+ False otherwise.
5153
52- Raises:
54+ Raises (in debug mode) :
5355 ModelBundleError if fails.
5456
5557 """
@@ -59,47 +61,52 @@ def _gzip_load(filename: str=glob.BUNDLE_NAME, _verbose=True):
5961 fp = gzip .open (filename , 'rb' )
6062 obj = pickle .load (fp )
6163 fp .close ()
62- if _verbose :
63- print ("Model bundle successfully loaded." )
64+ if glob . DEBUG :
65+ print ("Bundle message: Model bundle successfully loaded." )
6466 else :
65- if _verbose :
66- print ("Model bundle not found." )
67+ if glob .DEBUG :
68+ print ("Bundle message: Model bundle not found." )
69+ return False
6770 except Exception as e :
68- if _verbose :
69- print ("Exception: " + str (e ))
70- raise ModelBundleError ("Unable to load model bundle." )
71+ if glob .DEBUG :
72+ print ("Bundle exception: " + str (e ))
73+ raise ModelBundleError ("Bundle error: Unable to load model bundle." )
74+ return False
7175
7276 return obj
7377
7478
75- def _gzip_delete (filename :str = glob .BUNDLE_NAME , _verbose = False ):
79+ def _gzip_delete (filename :str = glob .GZIP_BUNDLE ):
7680 """Deletes a file from user machine.
7781
7882 Args:
79- filename: Str name of the file to delete, default: temp_sclbl_mod.gzip.
80- _verbose: Bool indicating whether feedback should be printed. Default False.
83+ filename: Str name of the file to delete. Default: glob.GZIP_BUNDLE.
8184
8285 Returns:
86+ True if all is well, False otherwise.
8387
84- Raises:
88+ Raises (in debug mode) :
8589 ModelBundleError if fails.
8690
8791 """
8892
8993 try :
9094 if os .path .exists (filename ):
9195 os .remove (filename )
92- if _verbose :
93- print ("Deleted gzipped model bundle." )
96+ if glob . DEBUG :
97+ print ("Bundle message: Deleted gzipped model bundle." )
9498 else :
95- if _verbose :
96- print ("File not found." )
99+ if glob .DEBUG :
100+ print ("Bundle message: File not found." )
101+ return False
97102 except Exception as e :
98- print ("Exception: " + str (e ))
99- raise ModelBundleError ("Unable to delete file." )
103+ if glob .DEBUG :
104+ print ("Bundle message: exception: " + str (e ))
105+ raise ModelBundleError ("Bundle error: Unable to delete file." )
106+ return False
100107
101- return
108+ return True
102109
103110
104111if __name__ == '__main__' :
105- print ("No command line options yet for _bundle.py." )
112+ print ("No command line options for _bundle.py." )
0 commit comments