-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoh_saving.py
367 lines (293 loc) · 10.6 KB
/
coh_saving.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
"""Class and methods for saving objects.
CLASSES
-------
SaveObject
- A class for inheriting specified attributes from another class object.
METHODS
-------
confirm_overwrite
- Asks the user to confirm whether a pre-existing file should be overwritten.
check_before_overwrite
- Checks whether a file exists at a specified filepath.
"""
from copy import deepcopy
import csv
import json
from os.path import exists
import pickle
from typing import Any, Union
from coh_exceptions import (
MissingFileExtensionError,
UnavailableProcessingError,
UnidenticalEntryError,
)
from coh_handle_files import check_ftype_present, identify_ftype
from coh_handle_objects import numpy_to_python
class SaveObject:
"""A class for inheriting specified attributes from another object.
PARAMETERS
----------
obj : Any
- The object to inherit attributes from.
attr_to_save : list[str]
- The names of the attributes to extract from the object.
"""
def __init__(self, obj: Any, attr_to_save: list[str]) -> None:
for attr_name in attr_to_save:
setattr(self, attr_name, deepcopy(getattr(obj, attr_name)))
def confirm_overwrite(fpath: str) -> bool:
"""Asks the user to confirm whether a pre-existing file should be
overwritten.
PARAMETERS
----------
fpath : str
- The filepath where the object will be saved.
RETURNS
-------
write : bool
- Whether or not the pre-existing file should be overwritten or not based
on the user's response.
"""
write = False
valid_response = False
while valid_response is False:
response = input(
f"The file '{fpath}' already exists.\nDo you want to "
"overwrite it? y/n: "
)
if response not in ["y", "n"]:
print(
"The only accepted responses are 'y' and 'n'. "
"Please input your response again."
)
if response == "n":
print(
"You have requested that the pre-existing file not "
"be overwritten. The new file has not been saved."
)
valid_response = True
if response == "y":
write = True
valid_response = True
return write
def check_before_overwrite(fpath: str) -> bool:
"""Checks whether a file exists at a specified filepath. If so, the user is
given the option of choosing whether to overwrite the file or not.
PARAMETERS
----------
fpath : str
- The filepath where the object will be saved.
RETURNS
-------
bool : str
- Whether or not the object should be saved to the filepath.
"""
if exists(fpath):
write = confirm_overwrite(fpath)
else:
write = True
return write
def save_as_json(
to_save: dict, fpath: str, convert_numpy_to_python: bool = False
) -> None:
"""Saves a dictionary as a json file.
PARAMETERS
----------
to_save : dict
- Dictionary in which the keys represent the names of the entries in
the json file, and the values represent the corresponding values.
fpath : str
- Location where the data should be saved.
convert_numpy_to_python : bool; default False
- Whether or not to convert numpy objects into their equivalent Python
types before saving.
"""
if convert_numpy_to_python:
to_save = numpy_to_python(to_save)
with open(fpath, "w", encoding="utf8") as file:
json.dump(to_save, file)
def save_as_csv(
to_save: dict, fpath: str, convert_numpy_to_python: bool = False
) -> None:
"""Saves a dictionary as a csv file.
PARAMETERS
----------
to_save : dict
- Dictionary in which the keys represent the names of the entries in
the csv file, and the values represent the corresponding values.
fpath : str
- Location where the data should be saved.
convert_numpy_to_python : bool; default False
- Whether or not to convert numpy objects into their equivalent Python
types before saving.
"""
if convert_numpy_to_python:
to_save = numpy_to_python(to_save)
with open(fpath, "wb") as file:
save_file = csv.writer(file)
save_file.writerow(to_save.keys())
save_file.writerow(to_save.values())
def save_as_pkl(
to_save: Any, fpath: str, convert_numpy_to_python: bool = False
) -> None:
"""Pickles and saves information in any format.
PARAMETERS
----------
to_save : Any
- Information that will be saved.
fpath : str
- Location where the information should be saved.
convert_numpy_to_python : bool; default False
- Whether or not to convert numpy objects into their equivalent Python
types before saving.
"""
if convert_numpy_to_python:
to_save = numpy_to_python(to_save)
with open(fpath, "wb") as file:
pickle.dump(to_save, file)
def save_object(
to_save: object,
fpath: str,
ask_before_overwrite: bool = True,
convert_numpy_to_python: bool = False,
verbose: bool = True,
) -> None:
"""Saves an object as a .pkl file.
PARAMETERS
----------
to_save : oibject
- Object to save.
fpath : str
- Location where the object should be saved. The filetype extension
(.pkl) can be included, otherwise it will be automatically added.
ask_before_overwrite : bool
- If True, the user is asked to confirm whether or not to overwrite a
pre-existing file if one exists.
- If False, the user is not asked to confirm this and it is done
automatically.
convert_numpy_to_python : bool; default False
- Whether or not to convert numpy objects into their equivalent Python
types before saving.
verbose : bool
- Whether or not to print a note of the saving process.
"""
if not check_ftype_present(fpath):
fpath += ".pkl"
if ask_before_overwrite:
write = check_before_overwrite(fpath)
else:
write = True
if write:
save_as_pkl(to_save, fpath, convert_numpy_to_python)
if verbose:
print(f"Saving the analysis object to:\n{fpath}")
def check_file_inputs(fpath: str, ftype: Union[str, None]) -> None:
"""Checks filepath and filetype inputs.
- If a filepath is given, checks whether a filetype extension is present.
- If so, checks whether this matches the extension given in 'ftype' (if
'ftype' is not 'None').
- If no extension is present in 'fpath', the extension given in 'ftype' is
used, in which case this cannot be 'None'.
PARAMETERS
----------
fpath : str
- A filepath, with or without a filetype extension.
ftype : str | None
- A filetype extension without the leading period.
- Can only be 'None' if a filetype is present in 'fpath'.
RETURNS
-------
fpath : str
- The filepath, with the file extension specified in 'ftype' if no
filetype was present in the provided 'fpath'.
ftype : str
- The filetype extension, derived from 'fpath'.
RAISES
------
UnidenticalEntryError
- Raised if the filetype in the filepath and the specified filetype do
not match.
MissingFileExtensionError
- Raised if no filetype is present in the filetype and one is not
specified.
"""
if check_ftype_present(fpath) and ftype is not None:
fpath_ftype = identify_ftype(fpath)
if fpath_ftype != ftype:
raise UnidenticalEntryError(
"Error when trying to save the results of the analysis:\n "
f"The filetypes in the filepath ({fpath_ftype}) and in the "
f"requested filetype ({ftype}) do not match.\n"
)
elif check_ftype_present(fpath) and ftype is None:
ftype = identify_ftype(fpath)
elif not check_ftype_present(fpath) and ftype is not None:
fpath = f"{fpath}.{ftype}"
else:
raise MissingFileExtensionError(
"Error when trying to save ta dictionary:\nNo filetype has been "
f"specified and it cannot be detected in the filepath:\n{fpath}\n"
)
return fpath, ftype
def save_dict(
to_save: dict,
fpath: str,
ftype: Union[str, None] = None,
ask_before_overwrite: bool = True,
convert_numpy_to_python: bool = False,
verbose: bool = True,
) -> None:
"""Saves a dictionary as a file.
PARAMETERS
----------
to_save : dict
- The dictionary to save.
fpath : str
- Location where the dictionary should be saved.
- Can contain a filetype (e.g. '.json'), in which case 'ftype' does not
need to be given and can be determined based on the filetype in 'fpath',
otherwise a filetype must be specified in 'ftype'.
ftype : str | None; default None
- The filetype of the dictionary that will be saved, without the leading
period. E.g. for saving the file in the json format, this would be
"json", not ".json".
- The information being saved must be an appropriate type for saving
in this format.
- If 'None', the filetype is automatically determined based on the the
filetype in 'fpath', in which case an identifiable filetype must be
present in 'fpath'.
ask_before_overwrite : bool; default True
- If True, the user is asked to confirm whether or not to overwrite a
pre-existing file if one exists.
- If False, the user is not asked to confirm this and it is done
automatically.
convert_numpy_to_python : bool; default False
- Whether or not to convert numpy objects into their equivalent Python
types before saving.
verbose : bool; default True
- Whether or not to print a note of the saving process.
RAISES
------
UnavailableProcessingError
- Raised if the given format for saving the file is in an unsupported
format.
"""
fpath, ftype = check_file_inputs(fpath=fpath, ftype=ftype)
if ask_before_overwrite:
write = check_before_overwrite(fpath)
else:
write = True
if write:
if verbose:
print(f"Saving the dictionary to:\n'{fpath}'.\n")
if ftype == "json":
save_as_json(to_save, fpath, convert_numpy_to_python)
elif ftype == "csv":
save_as_csv(to_save, fpath, convert_numpy_to_python)
elif ftype == "pkl":
save_as_pkl(to_save, fpath, convert_numpy_to_python)
else:
raise UnavailableProcessingError(
f"Error when trying to save a dictionary:\nThe {ftype} format "
"for saving is not supported.\n"
)