(code_content: str, data_directory: Path)
| 284 | |
| 285 | |
| 286 | def preprocess_code_paths(code_content: str, data_directory: Path) -> str: |
| 287 | |
| 288 | |
| 289 | patterns = [ |
| 290 | r"pd\.read_csv\s*\(\s*['\"]([^'\"]+)['\"]", |
| 291 | r"pd\.read_excel\s*\(\s*['\"]([^'\"]+)['\"]", |
| 292 | r"np\.loadtxt\s*\(\s*['\"]([^'\"]+)['\"]", |
| 293 | r"open\s*\(\s*['\"]([^'\"]+)['\"]", |
| 294 | |
| 295 | r"(['\"])([^'\"]+\.(?:csv|xlsx|txt))(['\"])", |
| 296 | ] |
| 297 | |
| 298 | |
| 299 | def replace_path_generic(match): |
| 300 | start_quote = match.group(1) |
| 301 | original_path = match.group(2) |
| 302 | end_quote = match.group(3) |
| 303 | |
| 304 | if Path(original_path).is_absolute(): |
| 305 | return match.group(0) |
| 306 | |
| 307 | absolute_path = data_directory / original_path |
| 308 | |
| 309 | return f"r'{absolute_path}'" |
| 310 | |
| 311 | processed_code = code_content |
| 312 | for pattern in patterns: |
| 313 | |
| 314 | if "csv|xlsx|txt" in pattern: |
| 315 | processed_code = re.sub(pattern, replace_path_generic, processed_code) |
| 316 | else: |
| 317 | |
| 318 | |
| 319 | pass |
| 320 | |
| 321 | |
| 322 | final_code = code_content |
| 323 | |
| 324 | |
| 325 | |
| 326 | path_like_pattern = r"(['\"])([^'\"]+\.(?:csv|xlsx|txt|json))(['\"])" |
| 327 | final_code = re.sub(path_like_pattern, replace_path_generic, final_code) |
| 328 | |
| 329 | return final_code |
| 330 | |
| 331 | |
| 332 | def execute_code_in_process(code_content: str, data_directory: Path, result_queue, timeout_seconds: int): |
no outgoing calls
no test coverage detected