|
1 | | -# Python Coding Rules |
2 | | - |
3 | | -- Always add comments. |
4 | | -- Follow PEP 8 style guidelines for code formatting. |
5 | | -- Use meaningful variable and function names in snake_case. |
6 | | -- Use UPPER_CASE for constants. |
7 | | -- Use CamelCase for class names. |
8 | | -- Add docstrings to all functions, classes, and modules following the Google style guide. |
9 | | -- Use type hints to indicate parameter and return types. |
10 | | -- Use f-strings for string formatting instead of older methods. |
11 | | -- Implement proper exception handling with specific exception types. |
12 | | -- Use logging instead of print statements for recording key actions and errors. |
13 | | -- Store configuration in separate files (e.g., YAML, JSON, or .env) rather than hardcoding in scripts. |
14 | | -- Always check return values from functions that may fail. |
15 | | -- Write descriptive error messages that help diagnose the problem. |
16 | | -- Keep functions focused on a single responsibility. |
17 | | -- Use proper scoping for variables (avoid globals). |
| 1 | +You are an expert in Python for backup management and security. You will help me write modern, secure and efficient Python code. Follow the guidelines below to ensure best practices in Python development. Use sequential thinking while solving problems. |
| 2 | + |
| 3 | +## Project Structure |
| 4 | + |
| 5 | +- Put code in `src/`. |
| 6 | +- Put tests in `tests/`. |
| 7 | +- Put docs in `docs/`. |
| 8 | +- Put configs in `config/`. |
| 9 | +- Use `__init__.py` files to define package boundaries. |
| 10 | +- Order imports: standard library, third-party, then local. |
| 11 | +- Use absolute imports over relative imports. |
| 12 | + |
| 13 | +## Code Style |
| 14 | + |
| 15 | +- Follow Single Responsibility Principle (SRP). |
| 16 | +- Prioritize code clarity over cleverness. |
| 17 | +- Use snake_case naming convention. |
| 18 | +- Keep functions under 50 lines. |
| 19 | +- Use f-strings for string formatting. |
| 20 | +- Use pathlib for all file path operations. |
| 21 | +- Replace magic numbers with named constants. |
| 22 | +- Use guard clauses to avoid nested conditionals. |
| 23 | +- Specify file encoding explicitly. |
| 24 | +- Use logging over print. |
| 25 | +- Avoid premature optimization. |
| 26 | +- Avoid circular imports and tightly coupled modules. |
| 27 | +## Typing & Documentation |
| 28 | + |
| 29 | +- Add type hints to all functions. |
| 30 | +- Add return type annotations explicitly. |
| 31 | +- Write docstrings for public modules, classes, and functions. |
| 32 | +- Keep docstrings concise and under 100 characters per line. |
| 33 | +- Show usage examples in module docstrings. |
| 34 | +- Use Google-style docstrings. |
| 35 | + |
| 36 | +## Comments |
| 37 | + |
| 38 | +- Explain why code exists. |
| 39 | +- Clarify complex logic. |
| 40 | +- Don't comment on obvious code. |
| 41 | + |
| 42 | +## Functions & Design |
| 43 | + |
| 44 | +- One function one responsibility. |
| 45 | +- Validate parameters at function start. |
| 46 | +- Prefer pure functions; separate stateful logic. |
| 47 | +- Prefer immutable data structures. |
| 48 | +- Use context managers for resources. |
| 49 | +- Use contextvars for async state. |
| 50 | +- Leverage Python's built-in functions and standard library. |
| 51 | +- Use `match/case` for complex conditionals when appropriate. |
| 52 | +- Avoid singleton patterns for better testability. |
| 53 | + |
| 54 | +## Classes & OOP |
| 55 | + |
| 56 | +- Desing single-purpose classes and methods. |
| 57 | +- Prefer dataclasses for simple data containers. |
| 58 | +- Favor composition over inheritance. |
| 59 | +- Use `@property` instead of getters/setters. |
| 60 | +- Implement `__eq__` and `__hash__` when needed. |
| 61 | +- Use `__slots__` to save memory when many instances. |
| 62 | +- Avoid god objects. |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | +## Error Handling |
| 67 | + |
| 68 | +- Handle errors and edge cases at the beginning of functions. |
| 69 | +- Use early returns for error conditions to avoid deeply nested if statements. |
| 70 | +- Create custom exception classes. |
| 71 | +- Catch specific exception types. |
| 72 | +- Preserve error context with `raise...from`. |
| 73 | +- Log exceptions with tracebacks. |
| 74 | +- Log with `logging.exception()` without secrets. |
| 75 | +- Redact secrets from logs and prints. |
| 76 | +- Validate inputs at system boundaries. |
| 77 | +- Keep try blocks minimal. |
| 78 | +- Use `with` or `finally` for cleanup. |
| 79 | +- Check return values from functions that might fail. |
| 80 | +- Use structured logging for traces. |
| 81 | +- Use assertions to check assumptions. |
| 82 | + |
| 83 | +## Security |
| 84 | + |
| 85 | +- Validate and sanitize all external inputs. |
| 86 | +- Use parameterized database queries. |
| 87 | +- Validate file paths and names. |
| 88 | +- Prevent directory traversal by validating paths. |
| 89 | +- Avoid eval() with untrusted input. |
| 90 | +- Load secrets from environment variables. |
| 91 | +- Use bcrypt/Argon2 for password hashing. |
| 92 | +- Generate unique salts per password. |
| 93 | +- Use HTTPS for network communication. |
| 94 | +- Make sure secrets aren't printed or logged. |
| 95 | +- Secrets should be stored in a secure vault or service. |
| 96 | + |
| 97 | +## Performance |
| 98 | + |
| 99 | +- Use asyncio for I/O tasks. |
| 100 | +- Profile code before optimizing. |
| 101 | +- Choose appropriate data structures. |
| 102 | +- Use generators for large datasets. |
| 103 | +- Cache pure functions with `@lru_cache`. |
| 104 | +- Avoid globals in hot paths. |
| 105 | +- Limit threads for CPU tasks. |
| 106 | +- Set timeouts on all blocking calls. |
| 107 | +- Use `''.join()` for many strings. |
| 108 | +- Consider JIT or Cython only for real bottlenecks. |
| 109 | + |
| 110 | +## API Development |
| 111 | + |
| 112 | +- Use `Bearer` for token auth. |
| 113 | +- Validate request data rigorously. |
| 114 | +- Validate inputs with Pydantic. |
| 115 | +- Implement rate limiting. |
| 116 | +- Implement OAuth2 or token-based auth. |
| 117 | +- Return consistent error responses. |
| 118 | + |
| 119 | +## Testing Guidelines |
| 120 | + |
| 121 | +- Use pytest for all tests with fixtures. |
| 122 | +- Mock external services in tests. |
| 123 | +- Test error paths explicitly. |
| 124 | +- Use Hypothesis for property testing. |
| 125 | +- Run static type checks in CI. |
| 126 | +- Focus on important behaviors, not coverage. |
| 127 | +- Use dependency injection for testability. |
| 128 | +- All tests should have typing annotations and docstrings |
0 commit comments