Miscs (Interro 28)
This commit is contained in:
+62
-48
@@ -1,3 +1,4 @@
|
||||
import argparse
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
@@ -12,12 +13,13 @@ ODS_PATH = "/home/sebastien/Rust/gestion_classe/Staging/current_eval.ods"
|
||||
TARGET_DIR_NAME = "A Rendre"
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 2:
|
||||
# Default to current directory if not provided, or raise error
|
||||
work_dir = os.getcwd()
|
||||
else:
|
||||
work_dir = os.path.abspath(sys.argv[1])
|
||||
parser = argparse.ArgumentParser(description="Update ODS with student scores.")
|
||||
parser.add_argument("work_dir", nargs="?", default=os.getcwd(), help="Directory to process")
|
||||
parser.add_argument("--sum", action="store_true", help="Write only the total sum per student")
|
||||
args = parser.parse_args()
|
||||
|
||||
work_dir = os.path.abspath(args.work_dir)
|
||||
|
||||
all_labels = read_all_labels(Path(work_dir))
|
||||
|
||||
a_rendre_path = os.path.join(work_dir, TARGET_DIR_NAME)
|
||||
@@ -101,53 +103,65 @@ def main():
|
||||
# Start filling from Row 2 (index 2), immediately below the name line
|
||||
start_row = 2
|
||||
|
||||
# for i, key in enumerate(scores_data.keys()):
|
||||
for i, key in enumerate(all_labels):
|
||||
row_idx = start_row + i
|
||||
|
||||
# Ensure we don't go out of bounds
|
||||
if row_idx >= sheet.nrows():
|
||||
sheet.append_rows(1)
|
||||
|
||||
if key in scores_data:
|
||||
val_str = str(scores_data[key])
|
||||
else:
|
||||
val_str = ""
|
||||
|
||||
# Logic: if "" -> "NT"
|
||||
new_val = "NT" if val_str == "" else val_str
|
||||
|
||||
cell = sheet[row_idx, col_idx]
|
||||
current_val = cell.value
|
||||
|
||||
# Conflict Detection
|
||||
# Normalize current ODS value to string for comparison
|
||||
# ODS might store 2.0 as float 2.0. JSON has "2.0".
|
||||
is_different = False
|
||||
|
||||
if current_val is not None and current_val != "":
|
||||
# specific check to handle float/string mismatch (2.0 vs "2.0")
|
||||
if args.sum:
|
||||
# Calculate total
|
||||
total = 0.0
|
||||
for val in scores_data.values():
|
||||
try:
|
||||
if float(str(current_val)) != float(str(new_val)):
|
||||
is_different = True
|
||||
except ValueError:
|
||||
# If conversion fails (e.g. comparing "NT" to "2.0"), compare strings
|
||||
if str(current_val).strip() != str(new_val).strip():
|
||||
is_different = True
|
||||
total += float(val)
|
||||
except (ValueError, TypeError):
|
||||
continue
|
||||
|
||||
if is_different:
|
||||
print(f"DEBUG: Conflict for {item} at {key} (Row {row_idx}). "
|
||||
f"Existing: '{current_val}' vs New: '{new_val}'. Overwriting.")
|
||||
cell = sheet[start_row, col_idx]
|
||||
cell.set_value(total)
|
||||
print(f"Set sum for {item}: {total}")
|
||||
else:
|
||||
for i, key in enumerate(all_labels):
|
||||
row_idx = start_row + i
|
||||
|
||||
# Set value
|
||||
# Try to set as float if it looks like a number, otherwise string
|
||||
if new_val == "NT":
|
||||
cell.set_value(new_val)
|
||||
else:
|
||||
try:
|
||||
cell.set_value(float(new_val))
|
||||
except ValueError:
|
||||
# Ensure we don't go out of bounds
|
||||
if row_idx >= sheet.nrows():
|
||||
sheet.append_rows(1)
|
||||
|
||||
if key in scores_data:
|
||||
val_str = str(scores_data[key])
|
||||
else:
|
||||
val_str = ""
|
||||
|
||||
# Logic: if "" -> "NT"
|
||||
new_val = "NT" if val_str == "" else val_str
|
||||
|
||||
cell = sheet[row_idx, col_idx]
|
||||
current_val = cell.value
|
||||
|
||||
# Conflict Detection
|
||||
# Normalize current ODS value to string for comparison
|
||||
# ODS might store 2.0 as float 2.0. JSON has "2.0".
|
||||
is_different = False
|
||||
|
||||
if current_val is not None and current_val != "":
|
||||
# specific check to handle float/string mismatch (2.0 vs "2.0")
|
||||
try:
|
||||
if float(str(current_val)) != float(str(new_val)):
|
||||
is_different = True
|
||||
except ValueError:
|
||||
# If conversion fails (e.g. comparing "NT" to "2.0"), compare strings
|
||||
if str(current_val).strip() != str(new_val).strip():
|
||||
is_different = True
|
||||
|
||||
if is_different:
|
||||
print(f"DEBUG: Conflict for {item} at {key} (Row {row_idx}). "
|
||||
f"Existing: '{current_val}' vs New: '{new_val}'. Overwriting.")
|
||||
|
||||
# Set value
|
||||
# Try to set as float if it looks like a number, otherwise string
|
||||
if new_val == "NT":
|
||||
cell.set_value(new_val)
|
||||
else:
|
||||
try:
|
||||
cell.set_value(float(new_val))
|
||||
except ValueError:
|
||||
cell.set_value(new_val)
|
||||
|
||||
print("Saving ODS file...")
|
||||
doc.save()
|
||||
|
||||
Reference in New Issue
Block a user