PY-57

Create a Separate Run and Record Its Status

  • Medium
  • Runs and Records
  • Python

Task

Write create_run(base_path, run_id, config, operation, write_record). run_id is safe only when it is non-empty and every character is a letter, digit, hyphen, or underscore. A safe run uses directory base_path / run_id; it must never overwrite an existing path.

For an unsafe ID, return:

For an existing run path, return the same shape with status "run exists" and error "run path already exists". In both cases, create and write nothing.

Otherwise create the run directory and call write_record(run_path / "status.record", record) with {"run_id": run_id, "status": "running", "configuration": config.copy()}. Then call operation(run_path, config).

On success, write the same record with status "complete" and the returned value under "result". Return:

{"status": "complete", "run_path": run_id, "result": result, "error": None}

If the operation raises ValueError, write a final record with status "failed" and "error": str(error), then return that evidence with a None result. Catch only ValueError; unrelated programming failures must remain visible.

Example

For run ID "trial-2", the operation receives the newly created trial-2 path. A successful run writes running and then complete records to the same status path. Calling the function again with "trial-2" reports "run exists" without calling the operation.

Your implementation

Edit solution.py and keep this function signature:

You may import Path from pathlib. Do not choose the status serialization, delete or overwrite a run, modify config, catch every exception, print, or ask for input.