grade.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import csv
  2. import os
  3. import subprocess
  4. import sys
  5. import tempfile
  6. from decimal import Decimal, InvalidOperation
  7. SCRIPT = "/app/summarize.py"
  8. TASK_INPUT = "/tests/task_input.csv"
  9. TEST_INPUT = "/tests/input.csv"
  10. SUMMARY = "/app/summary.csv"
  11. FILTERED = "/app/over_500.csv"
  12. DETAIL_HEADER = ["日期", "姓名", "类别", "金额"]
  13. SUMMARY_HEADER = ["姓名", "报销总额"]
  14. def read_csv(path):
  15. with open(path, encoding="utf-8-sig", newline="") as stream:
  16. return list(csv.reader(stream))
  17. def fail(message):
  18. os.makedirs("/logs/verifier", exist_ok=True)
  19. with open("/logs/verifier/reward.txt", "w", encoding="utf-8") as reward:
  20. reward.write("0\n")
  21. print(message)
  22. raise SystemExit(0)
  23. def expected_rows(source):
  24. if not source or source[0] != DETAIL_HEADER:
  25. raise ValueError("invalid verifier input")
  26. totals = {}
  27. order = []
  28. filtered = []
  29. for row in source[1:]:
  30. if len(row) != 4:
  31. raise ValueError("invalid verifier input")
  32. _, name, _, raw_amount = row
  33. amount = Decimal(raw_amount)
  34. if name not in totals:
  35. totals[name] = Decimal("0")
  36. order.append(name)
  37. totals[name] += amount
  38. if amount > Decimal("500"):
  39. filtered.append(row)
  40. summary = [SUMMARY_HEADER]
  41. summary.extend([[name, f"{totals[name]:.2f}"] for name in order])
  42. return summary, [DETAIL_HEADER, *filtered]
  43. def check_outputs(input_path, summary_path, filtered_path):
  44. source = read_csv(input_path)
  45. summary, filtered = expected_rows(source)
  46. if read_csv(summary_path) != summary or read_csv(filtered_path) != filtered:
  47. return False
  48. return True
  49. def main():
  50. try:
  51. if not os.path.isfile(SCRIPT):
  52. fail("Required summary program is missing.")
  53. if not check_outputs(TASK_INPUT, SUMMARY, FILTERED):
  54. fail("Outputs for the provided CSV do not match the task requirements.")
  55. test_source = read_csv(TEST_INPUT)
  56. expected_rows(test_source)
  57. with tempfile.TemporaryDirectory(prefix="expense-summary-") as temp_dir:
  58. summary_path = os.path.join(temp_dir, "summary.csv")
  59. filtered_path = os.path.join(temp_dir, "over_500.csv")
  60. try:
  61. subprocess.run(
  62. [sys.executable, SCRIPT, TEST_INPUT, summary_path, filtered_path],
  63. check=True,
  64. timeout=20,
  65. stdout=subprocess.DEVNULL,
  66. stderr=subprocess.DEVNULL,
  67. )
  68. except (OSError, subprocess.SubprocessError):
  69. fail("Summary program could not process the verifier CSV.")
  70. if not check_outputs(TEST_INPUT, summary_path, filtered_path):
  71. fail("Summary program output does not meet the task requirements.")
  72. except (OSError, csv.Error, InvalidOperation, ValueError, KeyError):
  73. fail("Required CSV files are missing, unreadable, or invalid.")
  74. os.makedirs("/logs/verifier", exist_ok=True)
  75. with open("/logs/verifier/reward.txt", "w", encoding="utf-8") as reward:
  76. reward.write("1\n")
  77. print("Outputs meet the task requirements.")
  78. if __name__ == "__main__":
  79. main()