mirror of
https://github.com/sanjeed5/awesome-cursor-rules-mdc.git
synced 2026-11-30 06:35:34 +08:00
Generate a large set of Markdown Configuration (MDC) files covering best practices across multiple domains, including: - AI/ML libraries (PyTorch, TensorFlow, scikit-learn) - Backend frameworks (Django, FastAPI, Express, Spring Boot) - Frontend frameworks (React, Vue, Angular) - Cloud platforms (AWS, Azure, GCP) - Development tools (Docker, Kubernetes, Terraform) - State management libraries - UI component libraries - Web technologies and APIs Enhance project documentation with detailed, standardized best practice guidelines for different technologies and frameworks
174 lines
9.1 KiB
Plaintext
174 lines
9.1 KiB
Plaintext
---
|
|
description: This rule provides comprehensive best practices for developing with the modal library, covering code organization, performance, security, testing, and common pitfalls. It aims to guide developers towards writing maintainable, efficient, and secure modal applications.
|
|
globs: **/*.py
|
|
---
|
|
- **Purpose and Clarity:** Ensure the modal's title clearly states its purpose and use concise headers. Use power words to grab attention (e.g., 'New,' 'Exclusive,' 'You').
|
|
- **User Control:** Modals should only be used for critical information and should not disrupt the user experience. Provide visible options for users to close the modal easily. Use a dark overlay to guide attention.
|
|
- **Accessibility:** Test modals with assistive technologies to ensure they are accessible to all users. Limit the use of system-initiated modals to avoid user frustration. Adhere to WAI-ARIA guidelines for modal implementation.
|
|
|
|
## 1. Code Organization and Structure
|
|
|
|
- **Directory Structure:**
|
|
- `modal.py`: Core module containing the `modal` class and related functions.
|
|
- `modal/`: A directory containing submodules:
|
|
- `modal/config.py`: Configuration settings for modal deployment and runtime.
|
|
- `modal/stub.py`: Definition and handling of modal stubs for local development.
|
|
- `modal/secret.py`: Secure handling of secrets required by modal applications.
|
|
- `modal/image.py`: Image definition and building processes.
|
|
- `modal/volume.py`: Management of persistent volumes for data storage.
|
|
- **File Naming Conventions:** Use descriptive names (e.g., `modal_api.py`, `modal_utils.py`). Follow PEP 8 guidelines for Python file names.
|
|
- **Module Organization:** Group related functionality into modules (e.g., `modal.network`, `modal.security`).
|
|
- **Component Architecture:**
|
|
- Decouple components for reusability and testability.
|
|
- Use dependency injection to manage dependencies between components.
|
|
- **Code Splitting Strategies:** Break down large files into smaller, more manageable modules. Consider lazy-loading modules that are not immediately needed.
|
|
|
|
## 2. Common Patterns and Anti-patterns
|
|
|
|
- **Design Patterns:**
|
|
- **Singleton:** Use sparingly, typically for global configuration objects.
|
|
- **Factory:** For creating modal objects with complex initialization logic.
|
|
- **Observer:** For handling events triggered by modal state changes.
|
|
- **Recommended Approaches:**
|
|
- Use context managers (`with` statement) for resource management.
|
|
- Employ decorators for adding functionality (e.g., caching, logging).
|
|
- **Anti-patterns:**
|
|
- Avoid global state, as it can lead to unpredictable behavior.
|
|
- Avoid deeply nested code, which is hard to read and maintain.
|
|
- Avoid hardcoding configuration values.
|
|
- **State Management:**
|
|
- Use immutable data structures for state to prevent unintended modifications.
|
|
- Implement clear state transition logic.
|
|
- Consider using a state management library like `modal.aio_libs.AioState` for complex scenarios.
|
|
- **Error Handling:**
|
|
- Use try-except blocks to handle potential exceptions.
|
|
- Log errors with sufficient context for debugging.
|
|
- Raise custom exceptions for specific error conditions.
|
|
|
|
## 3. Performance Considerations
|
|
|
|
- **Optimization Techniques:**
|
|
- Use caching to avoid redundant computations.
|
|
- Profile code to identify performance bottlenecks.
|
|
- Optimize database queries.
|
|
- **Memory Management:**
|
|
- Use generators to process large datasets in chunks.
|
|
- Explicitly release resources when they are no longer needed.
|
|
- Be mindful of memory leaks.
|
|
- **Rendering Optimization:** (If applicable for UI elements within modal).
|
|
- Minimize DOM manipulations.
|
|
- Use efficient rendering algorithms.
|
|
- **Bundle Size Optimization:**
|
|
- Remove unused code.
|
|
- Use code minification and compression.
|
|
- Leverage tree shaking to eliminate dead code.
|
|
- **Lazy Loading:** Defer loading non-critical resources until they are needed.
|
|
|
|
## 4. Security Best Practices
|
|
|
|
- **Common Vulnerabilities:**
|
|
- Input validation vulnerabilities (e.g., SQL injection, cross-site scripting).
|
|
- Authentication and authorization vulnerabilities.
|
|
- Data breaches due to insecure storage.
|
|
- **Input Validation:**
|
|
- Validate all user inputs before processing them.
|
|
- Use input sanitization techniques to prevent injection attacks.
|
|
- **Authentication and Authorization:**
|
|
- Use strong authentication mechanisms (e.g., multi-factor authentication).
|
|
- Implement role-based access control.
|
|
- Securely store and manage user credentials using `modal.Secret`.
|
|
- **Data Protection:**
|
|
- Encrypt sensitive data at rest and in transit.
|
|
- Use secure communication protocols (e.g., HTTPS).
|
|
- Comply with relevant data privacy regulations (e.g., GDPR, CCPA).
|
|
- **Secure API Communication:**
|
|
- Use API keys or tokens for authentication.
|
|
- Rate limit API requests to prevent abuse.
|
|
- Implement proper error handling to avoid leaking sensitive information.
|
|
|
|
## 5. Testing Approaches
|
|
|
|
- **Unit Testing:**
|
|
- Test individual functions and classes in isolation.
|
|
- Use mocking to isolate dependencies.
|
|
- Write test cases for different scenarios, including edge cases.
|
|
- **Integration Testing:**
|
|
- Test the interaction between different components.
|
|
- Verify that data flows correctly between modules.
|
|
- **End-to-end Testing:**
|
|
- Test the entire application workflow.
|
|
- Simulate user interactions to verify functionality.
|
|
- **Test Organization:**
|
|
- Organize tests into separate directories based on the module being tested.
|
|
- Use descriptive test names.
|
|
- **Mocking and Stubbing:**
|
|
- Use mocking to replace external dependencies with controlled substitutes.
|
|
- Use stubs to provide predefined responses for external calls.
|
|
|
|
## 6. Common Pitfalls and Gotchas
|
|
|
|
- **Frequent Mistakes:**
|
|
- Incorrectly handling asynchronous operations.
|
|
- Not properly closing resources (e.g., files, network connections).
|
|
- Ignoring error conditions.
|
|
- **Edge Cases:**
|
|
- Handling empty or invalid input data.
|
|
- Dealing with network timeouts and failures.
|
|
- Ensuring compatibility with different operating systems and environments.
|
|
- **Version-Specific Issues:**
|
|
- Be aware of breaking changes in newer versions of the library.
|
|
- Check the release notes for known issues and workarounds.
|
|
- **Compatibility Concerns:**
|
|
- Ensure compatibility with different Python versions.
|
|
- Consider cross-platform compatibility.
|
|
- **Debugging Strategies:**
|
|
- Use debuggers to step through code and inspect variables.
|
|
- Use logging to track program execution and identify errors.
|
|
- Use assertions to verify expected conditions.
|
|
|
|
## 7. Tooling and Environment
|
|
|
|
- **Recommended Development Tools:**
|
|
- IDE: VS Code, PyCharm.
|
|
- Debugger: pdb, ipdb.
|
|
- Profiler: cProfile.
|
|
- **Build Configuration:**
|
|
- Use a build system like `setuptools` or `poetry` to manage dependencies and create distributable packages.
|
|
- **Linting and Formatting:**
|
|
- Use linters (e.g., pylint, flake8) to enforce coding style guidelines.
|
|
- Use formatters (e.g., black, autopep8) to automatically format code.
|
|
- **Deployment Best Practices:**
|
|
- Use containerization (e.g., Docker) to create consistent and reproducible environments.
|
|
- Use infrastructure-as-code (e.g., Terraform, CloudFormation) to automate infrastructure provisioning.
|
|
- **CI/CD Integration:**
|
|
- Use CI/CD pipelines to automatically build, test, and deploy code changes.
|
|
- Integrate linters and formatters into the CI/CD pipeline.
|
|
|
|
## 8. modal Specific Best Practices
|
|
|
|
- **Efficient Use of Images:**
|
|
- Use pre-built base images whenever possible to reduce build times.
|
|
- Leverage caching mechanisms in `modal.Image` to avoid rebuilding unchanged images.
|
|
- **Data Serialization:**
|
|
- Be aware of the data serialization overhead when passing data between modal functions.
|
|
- Use efficient serialization formats like `pickle` or `protobuf` for large datasets.
|
|
- **Secret Management:**
|
|
- Use `modal.Secret` to securely manage sensitive information like API keys and passwords.
|
|
- Avoid hardcoding secrets in your code.
|
|
- **Concurrency and Parallelism:**
|
|
- Utilize `modal.method.call` and `modal.Queue` for concurrent and parallel processing.
|
|
- Understand the limitations of Python's Global Interpreter Lock (GIL) when writing multi-threaded code.
|
|
- **Resource Limits:**
|
|
- Be aware of resource limits (CPU, memory, disk space) imposed by the modal platform.
|
|
- Optimize your code to stay within these limits.
|
|
- **Monitoring and Logging:**
|
|
- Implement monitoring and logging to track the performance of your modal applications.
|
|
- Use tools like Prometheus and Grafana for visualization.
|
|
- **Cold Starts:**
|
|
- Be aware of cold start times when invoking modal functions for the first time.
|
|
- Optimize your code to reduce cold start times.
|
|
- **Version Control and Rollbacks:**
|
|
- Always use version control (e.g., Git) to track changes to your code.
|
|
- Implement rollback mechanisms to quickly revert to previous versions in case of errors.
|
|
|
|
By following these best practices, developers can build robust, scalable, and secure modal applications. |