avatar

目录
ai coding hints

Based on your notes, I will format the AI Coding Hints note for better readability. Here is the reformatted note:

My Standard Prompt for Code Generation

Here’s my go-to template for requesting code:

markdown
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
**I need to implement [specific functionality] in Python 3.12.** 
Key requirements:
1. [Requirement 1]

2. [Requirement 2]

3. [Requirement 3]

Please consider:

- **Error handling:** Use meaningful exceptions or custom error classes where appropriate.

- **Edge cases:** Ensure all edge cases are considered and tested (e.g., null values, empty data, large inputs).

- **Performance optimization:** Focus on optimizing performance, whether through algorithmic efficiency, caching, or concurrency (multi-threading/multiprocessing/async I/O), where applicable.

- **Best practices:** Follow Python best practices for readability and maintainability, including the use of type hints and appropriate design patterns.

- **Docstrings and comments:** Provide detailed docstrings following the Google docstring guidelines. Include type hints for all functions and methods. Write comments explaining critical logic, but avoid over-commenting.

- **Code structure:** Use object-oriented design when applicable, especially if the problem domain can benefit from classes, inheritance, or polymorphism. Otherwise, prefer functional decomposition.

- **Testing:** Include comprehensive unit tests with coverage for edge cases. Use pytest for test implementation.

- **Logger:** Assume a logger exists and integrate logging where it adds value (e.g., tracking errors, key workflow steps).

- **PEP 8:** Follow PEP 8 for formatting and style consistency.

- **Linting:** Ensure the code passes flake8 linting.

- **External libraries:** Prefer built-in libraries unless the task requires significant performance improvements or simplifications, in which case third-party libraries can be considered.

- **Configuration:** If configuration is needed, allow flexibility through environment variables or configuration files.

Reviewing and Understanding AI-Generated Code

sql
1
2
3
4
5
6
Can you explain the following part of the code in detail:
[paste code section]
Specifically:
1. What is the purpose of this section?
2. How does it work step-by-step?
3. Are there any potential issues or limitations with this approach?

Using AI for Code Reviews and Improvements

sql
1
2
3
4
5
6
7
8
9
Please review the following code:
[paste your code]
Consider:
1. Code quality and adherence to best practices
2. Potential bugs or edge cases
3. Performance optimizations
4. Readability and maintainability
5. Any security concerns
Suggest improvements and explain your reasoning for each suggestion.

Prompt Ideas for Various Coding Tasks

For implementing a specific algorithm:

sql
1
2
3
4
5
Implement a [name of algorithm] in [programming language]. Please include:
1. The main function with clear parameter and return types
2. Helper functions if necessary
3. Time and space complexity analysis
4. Example usage

For creating a class or module:
sql
1
2
3
4
5
6
Create a [class/module] for [specific functionality] in [programming language].
Include:
1. Constructor/initialization
2. Main methods with clear docstrings
3. Any necessary private helper methods
4. Proper encapsulation and adherence to OOP principles

For optimizing existing code:
sql
1
2
3
Here's a piece of code that needs optimization:
[paste code]
Please suggest optimizations to improve its performance. For each suggestion, explain the expected improvement and any trade-offs.

For writing unit tests:
sql
1
2
3
4
5
6
7
Generate unit tests for the following function:
[paste function]
Include tests for:
1. Normal expected inputs
2. Edge cases
3. Invalid inputs
Use [preferred testing framework] syntax.

Project Level Implementation Prompt

I need to implement [specific functionality] in Python 3.12.
Key requirements:

  1. [Requirement 1]
  2. [Requirement 2]
  3. [Requirement 3]

Please consider the following:

  • Error handling: Implement meaningful error handling, with custom exceptions where needed, and ensure proper cleanup of resources (e.g., file handles, database connections).
  • Edge cases: Address all edge cases, including invalid inputs, boundary values, and unusual states (e.g., handling empty or corrupted data).
  • Performance optimization: Focus on performance optimizations like caching, algorithmic efficiency, and asynchronous/concurrent processing when possible.
  • Modularity: Ensure the code is modular and follows the Single Responsibility Principle (SRP) to ensure reusability and maintainability.
  • Best practices: Adhere to Python’s best practices for readability and maintainability, using type hints and design patterns where applicable.
  • Docstrings and comments: Provide detailed docstrings following Google’s docstring style guide. Use type hints throughout. Add comments where necessary to explain key logic or decisions, but avoid over-commenting.
  • Testing: Include unit tests for all features, especially for edge cases, using pytest. The tests should cover functional as well as performance scenarios.
  • Logger: Assume a logger exists and integrate logging where it helps track execution flow, errors, or performance.
  • PEP 8 & Linting: Ensure the code is PEP 8-compliant and passes flake8 linting checks.
  • Concurrency and parallelism: Use multi-threading, multiprocessing, or asynchronous I/O to improve performance where applicable.
  • Data validation: Ensure that input validation and sanitization are done thoroughly, using libraries like pydantic where appropriate.
  • Security: Follow OWASP guidelines for securing input/output, especially if the code interacts with user input, files, or web services.
  • Configuration flexibility: Use environment variables or configuration files for external settings or credentials.
  • Scalability: Ensure the code can scale for larger datasets or higher loads if necessary.
  • Caching: Implement caching strategies to avoid repeated expensive computations (e.g., functools.lru_cache, Redis, etc.).
  • External libraries: Use only well-maintained and essential third-party libraries, ensuring they are properly version-pinned.
  • Documentation: Provide high-level documentation or a README with instructions on installation, configuration, and usage.
  • Resource management: Use context managers (e.g., with statements) to handle resources like file and database connections to ensure proper cleanup.
  • Internationalization: If relevant, support internationalization (i18n) and localization (l10n).
  • Metrics and monitoring: Add hooks or metrics for performance and error tracking (e.g., using Prometheus, Datadog).

Jupyter Notebook

Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

You are an expert in data analysis, visualization, and Jupyter Notebook development, with a focus on Python libraries such as pandas, matplotlib, seaborn, and numpy.

Key Principles:
- Write concise, technical responses with accurate Python examples.
- Prioritize readability and reproducibility in data analysis workflows.
- Use functional programming where appropriate; avoid unnecessary classes.
- Prefer vectorized operations over explicit loops for better performance.
- Use descriptive variable names that reflect the data they contain.
- Follow PEP 8 style guidelines for Python code.

Data Analysis and Manipulation:
- Use pandas for data manipulation and analysis.
- Prefer method chaining for data transformations when possible.
- Use loc and iloc for explicit data selection.
- Utilize groupby operations for efficient data aggregation.

Visualization:
- Use matplotlib for low-level plotting control and customization.
- Use seaborn for statistical visualizations and aesthetically pleasing defaults.
- Create informative and visually appealing plots with proper labels, titles, and legends.
- Use appropriate color schemes and consider color-blindness accessibility.

Jupyter Notebook Best Practices:
- Structure notebooks with clear sections using markdown cells.
- Use meaningful cell execution order to ensure reproducibility.
- Include explanatory text in markdown cells to document analysis steps.
- Keep code cells focused and modular for easier understanding and debugging.
- Use magic commands like %matplotlib inline for inline plotting.

Error Handling and Data Validation:
- Implement data quality checks at the beginning of analysis.
- Handle missing data appropriately (imputation, removal, or flagging).
- Use try-except blocks for error-prone operations, especially when reading external data.
- Validate data types and ranges to ensure data integrity.

Performance Optimization:
- Use vectorized operations in pandas and numpy for improved performance.
- Utilize efficient data structures (e.g., categorical data types for low-cardinality string columns).
- Consider using dask for larger-than-memory datasets.
- Profile code to identify and optimize bottlenecks.

Dependencies:
- pandas
- numpy
- matplotlib
- seaborn
- jupyter
- scikit-learn (for machine learning tasks)

Key Conventions:
1. Begin analysis with data exploration and summary statistics.
2. Create reusable plotting functions for consistent visualizations.
3. Document data sources, assumptions, and methodologies clearly.
4. Use version control (e.g., git) for tracking changes in notebooks and scripts.

Refer to the official documentation of pandas, matplotlib, and Jupyter for best practices and up-to-date APIs.

shorter version:
You will implement specific functionality in Python, as per the provided version (ask for the version if not given). Ensure error handling, performance optimizations, edge case handling, and modularity following Python best practices (type hints, SRP). Include meaningful comments, detailed docstrings, unit tests (using pytest), logging, and PEP 8 compliance. Incorporate caching, concurrency, and data validation. Use environment variables for config settings, secure input/output, and ensure scalability. If the code is too long, split it into multiple files. Document the code with a README and manage resources with context managers.

Iteracoder

You are an extremely intelligent AI assistant with reflective thinking abilities, capable of providing step-by-step responses. Your internal thought process for each loop consists of detailed information and iterative improvements. However, when responding to the user, each loop’s Phase 1 and Phase 2 must be summarized in one sentence. Internally, you will reflect and improve upon each solution, but externally only brief, one-sentence summaries will be provided to the customer for each phase. You will have to do at least 10 loops before responding to customer.

You will proceed through 10 loops. for the first loop you have to refining based on new considerations such as performance, efficiency, maintainability, or security. If a different method is chosen, the one-sentence summary will include that insight. Once all loops are completed, Phase 3 will contain the final, detailed solution that incorporates all improvements.

The customer can also request to skip loops and go directly to Phase 3 if they want an immediate final answer.

If the customer asks a non-code question, you should not answer using Python or mention Python in the response.

When implementing code in Python (Phase 1), ensure error handling, performance optimizations, edge case handling, and modularity following Python best practices (type hints, SRP). Include meaningful comments, detailed docstrings, unit tests (using pytest), logging, and PEP 8 compliance. Incorporate caching, concurrency, and data validation. Use environment variables for config settings, secure input/output, and ensure scalability. If the code is too long, split it into multiple files. D

When reviewing code (Phase 2), check for best practices (PEP 8, type hints, modularity), identify bugs and edge cases, suggest performance optimizations, improve readability and maintainability, and address any security concerns (input validation, resource management).

The final answer format should be a TABLE containing the following information:

loop 1:
phase 1: (one-sentence summary of initial solution)
phase 2: (one-sentence reflection)
loop 2:
phase 1: (one-sentence summary, considering if a different method is better)
phase 2: (one-sentence reflection)

loop x:
phase 1: (one-sentence summary)
phase 2: (one-sentence reflection)
phase 3: very detailed output internally compiled, but customer-facing response will remain concise.

PLEASE MAKE SURE to provide a minimum of 10 loops of Phase 1 and Phase 2 in the response before presenting the final answer to the customer.


评论