Helper function to convert the flexible 'initial_program' input into a file path.
(
initial_program: Union[str, Path, List[str]], temp_dir: Optional[str], temp_files: List[str]
)
| 170 | |
| 171 | |
| 172 | def _prepare_program( |
| 173 | initial_program: Union[str, Path, List[str]], temp_dir: Optional[str], temp_files: List[str] |
| 174 | ) -> str: |
| 175 | """Helper function to convert the flexible 'initial_program' input into a file path.""" |
| 176 | |
| 177 | # If the input is already a path to an existing file, use it directly. |
| 178 | if isinstance(initial_program, (str, Path)): |
| 179 | if os.path.exists(str(initial_program)): |
| 180 | return str(initial_program) |
| 181 | |
| 182 | # If it's not a path, treat it as code content. |
| 183 | if isinstance(initial_program, list): |
| 184 | # Join a list of lines into a single string. |
| 185 | code = "\n".join(initial_program) |
| 186 | else: |
| 187 | # It's already a string. |
| 188 | code = str(initial_program) |
| 189 | |
| 190 | # Ensure the code has evolution markers. If not, wrap the entire code in them. |
| 191 | if "EVOLVE-BLOCK-START" not in code: |
| 192 | code = f"""# EVOLVE-BLOCK-START |
| 193 | {code} |
| 194 | # EVOLVE-BLOCK-END""" |
| 195 | |
| 196 | # Write the code to a temporary file. |
| 197 | if temp_dir is None: |
| 198 | temp_dir = tempfile.gettempdir() |
| 199 | |
| 200 | program_file = os.path.join(temp_dir, f"program_{uuid.uuid4().hex[:8]}.py") |
| 201 | with open(program_file, "w") as f: |
| 202 | f.write(code) |
| 203 | # Keep track of the temporary file for later cleanup. |
| 204 | temp_files.append(program_file) |
| 205 | |
| 206 | return program_file |
| 207 | |
| 208 | |
| 209 | def _prepare_evaluator( |
no outgoing calls
no test coverage detected