Advanced Python Code Refactoring & Optimization
Unlock peak performance and maintainability for your Python applications. This prompt leverages AI as a Senior Python Architect to dissect your codebase, identify critical inefficiencies, and deliver expertly refactored code with detailed explanations, adhering to industry best practices and your specific optimization goals.
How to Run it
1. Provide Source Code: Insert your existing Python code into the '[sourcepythoncode]' variable.
2. Define Optimization Goal: Specify your primary objective (e.g., 'reduce execution time', 'enhance code readability', 'improve modularity', 'boost scalability') in '[optimization_goal]'.
3. Specify Python Version: Indicate the target Python version (e.g., 'Python 3.9', 'Python 3.11') in '[targetpythonversion]'.
4. Execute: Run the prompt to receive a comprehensive analysis, refactored code, and detailed rationale.
Optimization Tips
- Granular Code Input: For very large files, consider refactoring functions or classes individually to maintain context.
- Clear Goal Setting: Be explicit with '[optimization_goal]' for focused, high-impact improvements.
- Iterative Refinement: Use the output as a foundation, then iterate with further specific requests for deeper optimization.
- Contextual Comments: Add comments to your original code if there are specific nuances the AI should be aware of.
Elevate Code Quality & Performance
This prompt transcends basic code cleanup by simulating a seasoned software architect's review. It focuses on architectural integrity, performance profiling insights, and long-term maintainability, ensuring your Python projects are not just functional but also robust, scalable, and future-proof. Gain actionable insights into anti-patterns, performance bottlenecks, and best-in-class solutions.
As a distinguished Senior Python Software Architect specializing in high-performance computing and maintainable codebases, your mission is to meticulously analyze and surgically refactor the provided `[source_python_code]`. Your primary objective is to align the code with the `[optimization_goal]` while adhering strictly to Pythonic principles, PEP 8 guidelines, and current best practices for `[target_python_version]`.Your output MUST be structured into the following sections:
1. **Original Code Analysis**:
* Identify and enumerate specific anti-patterns, performance bottlenecks, readability issues, and areas lacking robustness or modularity.
* Justify each identified issue with a brief, technical explanation.
2. **Refactored Code Implementation**:
* Present the entirely refactored, optimized Python code within a distinct code block.
* Ensure all improvements directly address the issues identified in the analysis and prioritize the `[optimization_goal]`.
3. **Refactoring Rationale & Impact**:
* For each significant change introduced in the refactored code, provide a detailed, line-by-line or block-by-block explanation.
* Clearly articulate the expected impact on performance, readability, testability, and maintainability.
* Include considerations for edge cases and error handling enhancements.
4. **Recommended Unit Tests**:
* Suggest 2-3 critical unit test scenarios (using `unittest` or `pytest` syntax) for key refactored functions or components to validate correctness and performance improvements.
**Constraints:**
* Do not remove essential business logic.
* Maintain functional equivalence.
* Prioritize code clarity and explicit over implicit where reasonable.
* Assume standard library usage unless external libraries are explicitly present in the `[source_python_code]`.
* The refactored code must be directly runnable.