3
3
import pathlib
4
4
import typing
5
5
import operator
6
+ from datetime import datetime
6
7
7
8
from grid_info import NUM_QUESTIONS , Field , RealOrVirtualField , VirtualField
8
9
import list_utils
22
23
KEY_NOT_FOUND_MESSAGE = "NO KEY FOUND"
23
24
24
25
26
+ def format_timestamp_for_file (timestamp : datetime ) -> str :
27
+ return timestamp .isoformat (sep = "_" ).replace (":" , "-" )
28
+
29
+
25
30
class OutputSheet ():
26
31
"""A lightweight matrix of data to be exported. Faster than a dataframe but
27
32
can be easily converted to one when the need arises."""
@@ -40,12 +45,15 @@ def __init__(self, columns: typing.List[RealOrVirtualField]):
40
45
self .data = [field_column_names + answer_columns ]
41
46
self .row_count = 0
42
47
43
- def save (self , path : pathlib .PurePath , sort : bool ):
48
+ def save (self , path : pathlib .PurePath , filebasename : str , sort : bool ,
49
+ timestamp : datetime ) -> pathlib .PurePath :
44
50
if sort :
45
51
self .sortByName ()
46
- with open (str (path ), 'w+' , newline = '' ) as output_file :
52
+ output_path = path / f"{ format_timestamp_for_file (timestamp )} __{ filebasename } .csv"
53
+ with open (str (output_path ), 'w+' , newline = '' ) as output_file :
47
54
writer = csv .writer (output_file )
48
55
writer .writerows (self .data )
56
+ return output_path
49
57
50
58
def sortByName (self ):
51
59
data = self .data [1 :]
@@ -131,7 +139,8 @@ def clean_up(self, replace_empty_with: str = ""):
131
139
132
140
133
141
def save_reordered_version (sheet : OutputSheet , arrangement_file : pathlib .Path ,
134
- save_path : pathlib .Path ):
142
+ save_path : pathlib .Path , filebasename : str ,
143
+ timestamp : datetime ) -> pathlib .PurePath :
135
144
"""Reorder the output sheet based on a key arrangement file and save CSV."""
136
145
# order_map will be a dict matching form code keys to a list where the
137
146
# new index of question `i` in `key` is `order_map[key][i]`
@@ -167,6 +176,8 @@ def save_reordered_version(sheet: OutputSheet, arrangement_file: pathlib.Path,
167
176
results .append (row_reordered )
168
177
else :
169
178
results .append (row )
170
- with open (str (save_path ), 'w+' , newline = '' ) as output_file :
179
+ output_path = save_path / f"{ format_timestamp_for_file (timestamp )} __{ filebasename } .csv"
180
+ with open (str (output_path ), 'w+' , newline = '' ) as output_file :
171
181
writer = csv .writer (output_file )
172
182
writer .writerows (results )
183
+ return output_path
0 commit comments