You are a Python programmer tasked with refactoring code. You are proficient in…
- User
SYSTEM: You are a Python programmer tasked with refactoring code. You are proficient in Python 3.12 and use modern features where appropriate. Your code is clear, easy to read, and free of unnecessary verbosity. You use single-line docstrings and occasional comments for documentation. Often, the code you receive is just one part of a larger program. USER: Combine in one function to reduce redundency. Code: def check_file_for_corruption(file_path: Path, error_queue: Queue[Any]) -> None: """Check a file for corruption using FFmpeg.""" try: file_extension = file_path.suffix.lower() allowed_extensions = ( Config.VIDEO_EXTENSIONS | Config.AUDIO_EXTENSIONS | Config.IMAGE_EXTENSIONS )
if file_extension not in allowed_extensions: return base_command = [ str(Config.FFMPEG), "-v", "error", "-i", str(file_path), ] if file_extension in Config.VIDEO_EXTENSIONS: base_command += ["-map", "0:a:0"] base_command += ["-f", "null", "-"] result = subprocess.run( base_command, capture_output=True, text=True, encoding="utf-8", errors="ignore", ) combined_output = f"{result.stdout}\n{result.stderr}" error_messages = [ line.strip() for line in combined_output.splitlines() if "error" in line.lower() ] if error_messages: error_queue.put((file_path.resolve(), error_messages)) except Exception as e: error_queue.put( ( file_path.resolve(), [f"Error processing file {file_path}: {e}"], ) )def check_for_corrupted_files(): """Check for corrupted files in the input folder.""" image_processor.reset_results()
try: check_ffmpeg() except FileNotFoundError as error: log_error(str(error)) return valid_extensions = ( Config.VIDEO_EXTENSIONS | Config.AUDIO_EXTENSIONS | Config.IMAGE_EXTENSIONS ) tasks = [ file_path for file_path in Config.INPUT_FOLDER.rglob('*') if file_path.is_file() and file_path.suffix.lower() in valid_extensions ] if not tasks: log_error(f"No valid media files found in '{Config.INPUT_FOLDER}'") return error_queue = Queue() start_time = time.time() total_files = len(tasks) def process_task(file_path: Path, error_queue: Queue, start_time: float, total_files: int): """Process a single file for corruption.""" check_file_for_corruption(file_path, error_queue) with shared_state.processed_files_lock: shared_state.processed_files_count += 1 update_status(start_time, shared_state.processed_files_count, total_files) run_with_thread_pool(tasks, process_task, error_queue, start_time, total_files) log_errors(error_queue) - Assistant