(code_content: str, data_directory: Path)
| 324 | |
| 325 | |
| 326 | def preprocess_code_paths(code_content: str, data_directory: Path) -> str: |
| 327 | |
| 328 | |
| 329 | patterns = [ |
| 330 | r"pd\.read_csv\s*\(\s*['\"]([^'\"]+)['\"]", |
| 331 | r"pd\.read_excel\s*\(\s*['\"]([^'\"]+)['\"]", |
| 332 | r"np\.loadtxt\s*\(\s*['\"]([^'\"]+)['\"]", |
| 333 | r"open\s*\(\s*['\"]([^'\"]+)['\"]", |
| 334 | |
| 335 | r"(['\"])([^'\"]+\.(?:csv|xlsx|txt))(['\"])", |
| 336 | ] |
| 337 | |
| 338 | |
| 339 | def replace_path_generic(match): |
| 340 | start_quote = match.group(1) |
| 341 | original_path = match.group(2) |
| 342 | end_quote = match.group(3) |
| 343 | |
| 344 | if Path(original_path).is_absolute(): |
| 345 | return match.group(0) |
| 346 | |
| 347 | absolute_path = data_directory / original_path |
| 348 | |
| 349 | return f"r'{absolute_path}'" |
| 350 | |
| 351 | processed_code = code_content |
| 352 | for pattern in patterns: |
| 353 | |
| 354 | if "csv|xlsx|txt" in pattern: |
| 355 | processed_code = re.sub(pattern, replace_path_generic, processed_code) |
| 356 | else: |
| 357 | |
| 358 | |
| 359 | pass |
| 360 | |
| 361 | |
| 362 | final_code = code_content |
| 363 | |
| 364 | |
| 365 | |
| 366 | path_like_pattern = r"(['\"])([^'\"]+\.(?:csv|xlsx|txt|json))(['\"])" |
| 367 | final_code = re.sub(path_like_pattern, replace_path_generic, final_code) |
| 368 | |
| 369 | return final_code |
| 370 | |
| 371 | |
| 372 | def execute_code_in_process(code_content: str, data_directory: Path, result_queue, timeout_seconds: int): |
no outgoing calls
no test coverage detected