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