Skip to content

Commit

Permalink
Add timestamp prefix to output files to prevent overwriting
Browse files Browse the repository at this point in the history
Resolves #22
  • Loading branch information
iansan5653 committed Nov 26, 2019
1 parent 91db9da commit 838ae2f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
19 changes: 15 additions & 4 deletions code/data_exporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pathlib
import typing
import operator
from datetime import datetime

from grid_info import NUM_QUESTIONS, Field, RealOrVirtualField, VirtualField
import list_utils
Expand All @@ -22,6 +23,10 @@
KEY_NOT_FOUND_MESSAGE = "NO KEY FOUND"


def format_timestamp_for_file(timestamp: datetime) -> str:
return timestamp.isoformat(sep="_").replace(":", "-")


class OutputSheet():
"""A lightweight matrix of data to be exported. Faster than a dataframe but
can be easily converted to one when the need arises."""
Expand All @@ -40,12 +45,15 @@ def __init__(self, columns: typing.List[RealOrVirtualField]):
self.data = [field_column_names + answer_columns]
self.row_count = 0

def save(self, path: pathlib.PurePath, sort: bool):
def save(self, path: pathlib.PurePath, filebasename: str, sort: bool,
timestamp: datetime) -> pathlib.PurePath:
if sort:
self.sortByName()
with open(str(path), 'w+', newline='') as output_file:
output_path = path / f"{format_timestamp_for_file(timestamp)}__{filebasename}.csv"
with open(str(output_path), 'w+', newline='') as output_file:
writer = csv.writer(output_file)
writer.writerows(self.data)
return output_path

def sortByName(self):
data = self.data[1:]
Expand Down Expand Up @@ -131,7 +139,8 @@ def clean_up(self, replace_empty_with: str = ""):


def save_reordered_version(sheet: OutputSheet, arrangement_file: pathlib.Path,
save_path: pathlib.Path):
save_path: pathlib.Path, filebasename: str,
timestamp: datetime) -> pathlib.PurePath:
"""Reorder the output sheet based on a key arrangement file and save CSV."""
# order_map will be a dict matching form code keys to a list where the
# new index of question `i` in `key` is `order_map[key][i]`
Expand Down Expand Up @@ -167,6 +176,8 @@ def save_reordered_version(sheet: OutputSheet, arrangement_file: pathlib.Path,
results.append(row_reordered)
else:
results.append(row)
with open(str(save_path), 'w+', newline='') as output_file:
output_path = save_path / f"{format_timestamp_for_file(timestamp)}__{filebasename}.csv"
with open(str(output_path), 'w+', newline='') as output_file:
writer = csv.writer(output_file)
writer.writerows(results)
return output_path
34 changes: 25 additions & 9 deletions code/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
import time
import typing
import textwrap
Expand Down Expand Up @@ -26,6 +27,9 @@
sort_results = user_input.sort_results

progress = user_interface.ProgressTracker(user_input.root, len(image_paths))

files_timestamp = datetime.now().replace(microsecond=0)

try:
for image_path in image_paths:
progress.set_status(f"Processing '{image_path.name}'.")
Expand Down Expand Up @@ -76,23 +80,35 @@
progress.step_progress()

answers_results.clean_up("G" if empty_answers_as_g else "")
answers_results.save(output_folder / "results.csv", sort_results)
answers_results.save(output_folder,
"results",
sort_results,
timestamp=files_timestamp)

success_string = "✔️ All exams processed and saved to 'results.csv'.\n"
success_string = "✔️ All exams processed and saved.\n"

if keys_file:
keys_results.add_file(keys_file)

if (keys_results.row_count != 0):
keys_results.save(output_folder / "keys.csv", sort_results)
success_string += "✔️ All keys processed and saved to 'keys.csv'.\n"
keys_path = keys_results.save(output_folder,
"keys",
sort_results,
timestamp=files_timestamp)
success_string += "✔️ All keys processed and saved.\n"
scores = scoring.score_results(answers_results, keys_results)
scores.save(output_folder / "scores.csv", sort_results)
success_string += "✔️ All scored results processed and saved to 'scores.csv'."
scores.save(output_folder,
"scores",
sort_results,
timestamp=files_timestamp)
success_string += "✔️ All scored results processed and saved."
if arrangement_file:
data_exporting.save_reordered_version(
scores, arrangement_file, output_folder / "reordered.csv")
success_string += "✔️ Reordered results saved to 'reordered.csv'."
data_exporting.save_reordered_version(scores,
arrangement_file,
output_folder,
"reordered",
timestamp=files_timestamp)
success_string += "✔️ Reordered results saved."
else:
success_string += "No exam keys were found, so no scoring was performed."

Expand Down
9 changes: 2 additions & 7 deletions code/user_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ def __init__(self):
create_and_pack_label(app, "Select Output Folder", heading=True)
create_and_pack_label(
app,
"Select a folder to save output files to.\nExisting files may be overwritten, so it's best to use an empty folder."
"Select a folder to save output files to."
)

self.__output_folder_picker = FolderPickerWidget(
Expand Down Expand Up @@ -297,12 +297,7 @@ def update_status(self):
ok_to_submit = False
else:
self.output_folder = output_folder
existing_csv_files = file_handling.filter_by_extensions(
file_handling.list_file_paths(output_folder), [".csv"])
if len(existing_csv_files) == 0:
new_status += f"✔ Output folder selected.\n"
else:
new_status += f"✔⚠ Output folder selected. Existing CSV files may be overwritten.\n"
new_status += f"✔ Output folder selected.\n"

keys_file = self.__answer_key_picker.selection
if keys_file:
Expand Down

0 comments on commit 838ae2f

Please sign in to comment.