tamil-mani-p/log-result icon
public
Published on 5/21/2025
log-result

Prompts
log-result
A sample prompt
Create a function `log_results(context)` that logs test input conditions and measured outputs from `context['raw_result']` into a CSV file. This function should **not perform any pass/fail validation** — it must simply persist the data. ---
### 🧠 Instructions
- The `context['raw_result']` will contain:
  - **Input conditions** (e.g., `vin`, `iload`, `temp`, etc.)
  - **Measured outputs** (e.g., `vout_measured`, `dropout_voltage`, etc.)
- Keys in `raw_result` are dynamic — do not assume fixed field names.
- Add a `timestamp` field to each row using **UTC ISO 8601 format**, if not already present.
- Write this data as a row in a file named `results.csv` in the current working directory.
  - If `results.csv` does not exist, create it and include a header row using `raw_result` keys.
  - If it exists, append the row without rewriting headers.
- After writing, update the context with the absolute file path:
  ```python
  context["log_result"] = {"file_path": "<absolute path>"}
  ```
### Example Template
  ```python
  import csv
  import os
  import time

  def log_results(context: dict) -> dict:
      raw = context["raw_result"]
      if "timestamp" not in raw:
          raw["timestamp"] = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime())

      filename = "results.csv"
      file_exists = os.path.isfile(filename)

      with open(filename, mode="a", newline="") as f:
          writer = csv.DictWriter(f, fieldnames=raw.keys())
          if not file_exists:
              writer.writeheader()
          writer.writerow(raw)

      context["log_result"] = {"file_path": os.path.abspath(filename)}
      return context
  ```
### Constraints - Do not hardcode test condition names. - Do not include any pass/fail logic. - Ensure compatibility with Python 3.8+. - Output must be a well-formed CSV with dynamic headers based on the keys in raw_result. - Keep the function idempotent and safe for repeated appends.