wael-bousfira/python-rules icon
public
Published on 3/19/2025
Python General Rules

Rules
python-rules
#### Build & Development Commands
- **Backend (Python):**
  - `python -m venv venv`: Create a virtual environment.
  - `source venv/bin/activate`: Activate the virtual environment.
  - `pip install -r requirements.txt`: Install dependencies.
  - `python app.py`: Run the backend server.

- **Frontend (ReactJSX/Astro):**
  - `npm install`: Install dependencies.
  - `npm run dev`: Start the development server.
  - `npm run build`: Build the project for production.

#### Testing Guidelines
- **Unit Tests:**
  - Write unit tests for individual components and functions.
  - Use testing frameworks like `pytest` for Python and `Jest` for ReactJSX.

- **Integration Tests:**
  - Test the interaction between different components and services.
  - Ensure that the frontend and backend communicate correctly.

- **End-to-End Tests:**
  - Simulate user interactions and test the entire flow.
  - Use tools like `Cypress` for end-to-end testing.

#### Code Style & Guidelines
- **Python:**
  - Follow PEP 8 guidelines for code style.
  - Use linters like `flake8` to enforce code quality.

- **ReactJSX/Astro:**
  - Follow Airbnb JavaScript Style Guide.
  - Use `ESLint` to enforce code style and quality.

- **General:**
  - Write clean, modular, and reusable code.
  - Use meaningful variable and function names.
  - Comment code where necessary to explain complex logic.

#### Documentation Guidelines
- **Code Documentation:**
  - Use docstrings in Python to document functions and classes.
  - Use JSdoc in JavaScript/ReactJSX to document components and functions.

- **Project Documentation:**
  - Maintain a README file with project setup instructions.
  - Document API endpoints and their usage.
  - Provide examples and usage guidelines for custom components.

- **User Documentation:**
  - Create user guides and tutorials for using the AI assistant.
  - Provide troubleshooting tips and FAQs.

### Additional Python Deployment Rules

#### Environment Management
- **Virtual Environments:**
  - Always use virtual environments to manage dependencies. This ensures that the project's dependencies are isolated from other projects.
  - Use tools like `venv`, `virtualenv`, or `conda` to create virtual environments.

- **Dependency Management:**
  - Use `requirements.txt` or `Pipfile` to list project dependencies.
  - Pin dependency versions to ensure consistency across different environments.

#### Configuration Management
- **Environment Variables:**
  - Store configuration settings such as API keys, database URLs, and other sensitive information in environment variables.
  - Use libraries like `python-dotenv` to manage environment variables.

- **Configuration Files:**
  - Use configuration files (e.g., `config.py`, `settings.py`) to manage application settings.
  - Avoid hardcoding configuration values in the source code.

#### Logging and Monitoring
- **Logging:**
  - Implement logging to track application behavior and errors.
  - Use Python's built-in `logging` module or libraries like `loguru`.
  - Ensure logs are stored in a centralized location for easy access and analysis.

- **Monitoring:**
  - Integrate monitoring tools to track application performance and errors.
  - Use tools like Prometheus, Grafana, or Sentry for monitoring and alerting.

#### Scalability and Performance
- **Asynchronous Programming:**
  - Use asynchronous programming with `asyncio` or frameworks like FastAPI to handle concurrent tasks efficiently.

- **Caching:**
  - Implement caching to improve performance for frequently accessed data.
  - Use caching libraries like `cachetools` or integrate with caching services like Redis.

#### Security
- **Input Validation:**
  - Validate all user inputs to prevent security vulnerabilities like SQL injection and cross-site scripting (XSS).

- **Authentication and Authorization:**
  - Implement robust authentication and authorization mechanisms.
  - Use libraries like `Flask-Login` or `Django's authentication system`.

#### Deployment Automation
- **CI/CD Pipelines:**
  - Set up Continuous Integration/Continuous Deployment (CI/CD) pipelines to automate testing and deployment.
  - Use tools like GitHub Actions, GitLab CI, or Jenkins.

- **Containerization:**
  - Use Docker to containerize the application for consistent deployment across different environments.
  - Create a `Dockerfile` to define the application's environment and dependencies.

#### Documentation
- **Deployment Guides:**
  - Provide detailed deployment guides, including step-by-step instructions and troubleshooting tips.
  - Document environment setup, configuration, and common deployment issues.