|
| 1 | +# 🎯 How to Use Cursor Rules with AI Prompts |
| 2 | + |
| 3 | +This guide explains how to effectively use the Fledge Cursor rules for Python development and documentation in your AI prompts and development workflow. |
| 4 | + |
| 5 | +## 📁 Directory Structure |
| 6 | + |
| 7 | +Rules are organized for Python development and documentation: |
| 8 | + |
| 9 | +``` |
| 10 | +.cursor/rules/ |
| 11 | +├── README.md # This usage guide |
| 12 | +├── python/ # Python-specific rules (Python 3.8.10-3.12, Ubuntu LTS 20.04+, Raspberry Pi) |
| 13 | +│ ├── core.mdc # Core Python standards + platform requirements |
| 14 | +│ ├── api.mdc # REST API + web framework dependencies |
| 15 | +│ ├── config.mdc # Configuration management + validation deps |
| 16 | +│ └── quality.mdc # Dependencies, logging, performance + requirements.txt |
| 17 | +├── tests/ # Testing-specific rules |
| 18 | +│ └── python/ # Python testing rules |
| 19 | +│ ├── unit.mdc # Unit testing rules - pytest, coverage, best practices |
| 20 | +│ └── api.mdc # API integration testing rules - conftest fixtures, http.client patterns |
| 21 | +└── docs.mdc # Documentation guidelines |
| 22 | +``` |
| 23 | + |
| 24 | +## 📋 Available Rule Files |
| 25 | + |
| 26 | +| Rule File | Purpose | Applies To | |
| 27 | +|-----------|---------|------------| |
| 28 | +| `@python/core` | Core Python standards, naming, imports | `*.py`, `python/**/*` | |
| 29 | +| `@python/api` | REST APIs, routes, middleware | API files, routes.py, web middleware | |
| 30 | +| `@python/config` | Configuration system, data formats | Config files, configuration modules | |
| 31 | +| `@python/quality` | Dependencies, logging, performance | Requirements files | |
| 32 | +| `@tests/python/unit` | Unit testing with pytest | Unit test files, test configuration | |
| 33 | +| `@tests/python/api` | API integration testing with http.client | API integration test files, conftest.py | |
| 34 | +| `@docs` | Documentation writing | `docs/**/*`, `*.rst` | |
| 35 | + |
| 36 | +## 🏗️ Shared Platform & Dependencies |
| 37 | + |
| 38 | +All Python rules include consistent platform and dependency information: |
| 39 | + |
| 40 | +### **Platform Requirements** (Built into all Python rules) |
| 41 | +- **Python Versions**: 3.8.10 - 3.12 (inclusive) |
| 42 | +- **Ubuntu**: LTS versions, 20.04 onwards (x86_64 & aarch64) |
| 43 | +- **Raspberry Pi OS**: Bullseye and Bookworm (aarch64 & armv7l) |
| 44 | + |
| 45 | +### **Dependencies Management** (Referenced in all Python rules) |
| 46 | +- **[python/requirements.txt](python/requirements.txt)** - Runtime dependencies |
| 47 | +- **[python/requirements-dev.txt](python/requirements-dev.txt)** - Development dependencies |
| 48 | +- **[python/requirements-test.txt](python/requirements-test.txt)** - Testing dependencies |
| 49 | + |
| 50 | +### **Automatic Context** (No need to repeat in prompts) |
| 51 | +When you use any `@python/*` rule, the AI automatically knows: |
| 52 | +```bash |
| 53 | +# Instead of writing this every time: |
| 54 | +"Create a Python function that works on Python 3.8.10-3.12, Ubuntu LTS 20.04+, Raspberry Pi, uses requirements.txt for dependencies..." |
| 55 | + |
| 56 | +# You can simply write: |
| 57 | +@python/core "Create a Python function" |
| 58 | +# The AI already knows the platform and dependency constraints! |
| 59 | +``` |
| 60 | + |
| 61 | +## 🔄 Automatic Rule Application |
| 62 | + |
| 63 | +Cursor automatically applies rules based on the files you're working with: |
| 64 | + |
| 65 | +```yaml |
| 66 | +# Example: Working on Python files automatically applies python/core rules |
| 67 | +python/fledge/services/core/server.py → @python/core rules active |
| 68 | + |
| 69 | +# Working on API files applies both core and API rules |
| 70 | +python/fledge/services/core/api/auth.py → @python/core + @python/api rules active |
| 71 | + |
| 72 | +# Documentation files apply docs rules |
| 73 | +docs/quick_start/installing.rst → @docs rules active |
| 74 | +``` |
| 75 | + |
| 76 | +## 🎯 Explicit Rule References in Prompts |
| 77 | + |
| 78 | +### Direct Rule Invocation |
| 79 | +``` |
| 80 | +@python/core Can you help me write a function that follows Fledge Python standards? |
| 81 | +
|
| 82 | +@python/api I need to create a new REST endpoint for device management |
| 83 | +
|
| 84 | +@docs Help me write documentation for this new feature |
| 85 | +``` |
| 86 | + |
| 87 | +### Multiple Rule References |
| 88 | +``` |
| 89 | +@python/core @python/quality Help me refactor this code with proper error handling |
| 90 | +
|
| 91 | +@python/api @python/config Create an API endpoint for configuration management |
| 92 | +
|
| 93 | +@docs @python/api Document this REST API following both documentation and API standards |
| 94 | +
|
| 95 | +@python/core @tests/python/unit Create a service class with comprehensive unit tests |
| 96 | +
|
| 97 | +@tests/python/api @python/api Create API integration tests for new REST endpoints |
| 98 | +``` |
| 99 | + |
| 100 | +## 💡 Context-Aware Prompts |
| 101 | + |
| 102 | +### When Working on Python Files |
| 103 | +``` |
| 104 | +# Cursor automatically knows to apply Python rules |
| 105 | +"Create a new service class that handles sensor data processing" |
| 106 | +
|
| 107 | +# The AI will automatically follow: |
| 108 | +- snake_case naming conventions |
| 109 | +- Type hints and docstrings |
| 110 | +- FLCoreLogger usage |
| 111 | +- Async/await patterns |
| 112 | +- Error handling standards |
| 113 | +- Python 3.8.10-3.12 compatibility |
| 114 | +``` |
| 115 | + |
| 116 | +### When Working on Documentation |
| 117 | +``` |
| 118 | +# In docs/ directory, rules automatically apply |
| 119 | +"Document this new plugin API" |
| 120 | +
|
| 121 | +# The AI will automatically: |
| 122 | +- Use reStructuredText format |
| 123 | +- Follow Sphinx conventions |
| 124 | +- Avoid "Fledge" in headings where possible |
| 125 | +- Include proper cross-references |
| 126 | +- Use correct heading hierarchy |
| 127 | +``` |
| 128 | + |
| 129 | +## 🛠️ Specific Rule-Based Requests |
| 130 | + |
| 131 | +### Configuration Management |
| 132 | +``` |
| 133 | +Using @python/config rules, create a configuration category for my new plugin with: |
| 134 | +- String, integer, and boolean parameters |
| 135 | +- Proper validation |
| 136 | +- Default values wrapped in quotes |
| 137 | +- Reserved category name checking |
| 138 | +``` |
| 139 | + |
| 140 | +### API Development |
| 141 | +``` |
| 142 | +Following @python/api rules, create a REST endpoint that: |
| 143 | +- Handles role-based access through middleware |
| 144 | +- Returns camelCase JSON responses |
| 145 | +- Includes proper error handling |
| 146 | +- Checks for route conflicts |
| 147 | +- Uses FLCoreLogger for logging |
| 148 | +``` |
| 149 | + |
| 150 | +### Unit Testing |
| 151 | +``` |
| 152 | +Using @tests/python/unit rules, create unit tests that: |
| 153 | +- Use pytest framework |
| 154 | +- Include proper mocking with pytest-mock |
| 155 | +- Test both success and failure cases |
| 156 | +- Follow the test file naming conventions |
| 157 | +- Include code coverage setup |
| 158 | +``` |
| 159 | + |
| 160 | +### API integration Testing |
| 161 | +``` |
| 162 | +Using @tests/python/api rules, create API integration tests that: |
| 163 | +- Use http.client library exclusively (no requests) |
| 164 | +- Leverage conftest.py fixtures like reset_and_start_fledge |
| 165 | +- Test API endpoints with proper authentication |
| 166 | +- Use fledge_url and storage_plugin fixtures |
| 167 | +- Follow system test organization patterns |
| 168 | +``` |
| 169 | + |
| 170 | +### Documentation |
| 171 | +``` |
| 172 | +Following @docs rules, create documentation that: |
| 173 | +- Uses reStructuredText format |
| 174 | +- Includes proper Sphinx directives |
| 175 | +- Avoids excessive "Fledge" branding |
| 176 | +- Has correct heading hierarchy |
| 177 | +- Includes cross-references to related docs |
| 178 | +``` |
| 179 | + |
| 180 | +## 🔀 Advanced Rule Usage |
| 181 | + |
| 182 | +### API Documentation |
| 183 | +``` |
| 184 | +Using @docs rules, create documentation for this Python API (@python/api) |
| 185 | +that includes proper Sphinx directives and avoids excessive Fledge branding. |
| 186 | +``` |
| 187 | + |
| 188 | +### Complete Feature Development |
| 189 | +``` |
| 190 | +I'm creating a new Fledge service that includes: |
| 191 | +- Python backend (@python/core @python/api) |
| 192 | +- Configuration management (@python/config) |
| 193 | +- Unit testing (@tests/python/unit) |
| 194 | +- API integration testing (@tests/python/api) |
| 195 | +- Complete documentation (@docs) |
| 196 | +``` |
| 197 | + |
| 198 | +## 🔍 Rule-Aware Code Reviews |
| 199 | + |
| 200 | +``` |
| 201 | +Review this code against @python/core and @python/quality rules: |
| 202 | +- Check naming conventions (snake_case vs camelCase) |
| 203 | +- Verify proper logging usage (FLCoreLogger) |
| 204 | +- Ensure type hints are present |
| 205 | +- Validate error handling patterns |
| 206 | +- Check Python version compatibility |
| 207 | +
|
| 208 | +Review this test code against @tests/python/unit rules: |
| 209 | +- Validate pytest usage and fixture patterns |
| 210 | +- Check mocking strategies and test isolation |
| 211 | +- Ensure proper test organization and naming |
| 212 | +- Verify code coverage approach |
| 213 | +``` |
| 214 | + |
| 215 | +## 🚀 Platform-Specific Development |
| 216 | + |
| 217 | +``` |
| 218 | +# Old way (verbose, repetitive): |
| 219 | +Using @python/core rules, help me optimize this code for: |
| 220 | +- Raspberry Pi ARM architecture (aarch64, armv7l) |
| 221 | +- Python 3.8.10-3.12 compatibility |
| 222 | +- Edge device memory constraints |
| 223 | +- Ubuntu LTS 20.04+ deployment |
| 224 | +
|
| 225 | +# New way (automatic platform context): |
| 226 | +@python/core Optimize this code for edge device performance |
| 227 | +
|
| 228 | +# The AI automatically knows: |
| 229 | +# - Python 3.8.10-3.12 compatibility |
| 230 | +# - Ubuntu LTS 20.04+ (x86_64 & aarch64) |
| 231 | +# - Raspberry Pi OS (aarch64 & armv7l) |
| 232 | +# - Edge device memory constraints |
| 233 | +# - Requirements.txt dependency management |
| 234 | +``` |
| 235 | + |
| 236 | +## 🐛 Troubleshooting with Rules |
| 237 | + |
| 238 | +``` |
| 239 | +This code isn't following @python/api middleware patterns. |
| 240 | +Help me fix the authentication and role validation. |
| 241 | +
|
| 242 | +This documentation doesn't follow @docs anti-branding guidelines. |
| 243 | +Help me remove excessive "Fledge" references while maintaining clarity. |
| 244 | +``` |
| 245 | + |
| 246 | +## 🔧 Pro Tips for Using Rules Effectively |
| 247 | + |
| 248 | +### 1. Let Rules Work Automatically |
| 249 | +- Just open files in the appropriate directories |
| 250 | +- Cursor applies rules based on file patterns (globs) |
| 251 | +- No need to explicitly mention rules for basic tasks |
| 252 | +- Rules are automatically in context |
| 253 | + |
| 254 | +### 2. Use Rule Names for Specific Guidance |
| 255 | +- When you need specific standards applied |
| 256 | +- When working across multiple technologies |
| 257 | +- When you want to ensure compliance with particular guidelines |
| 258 | +- When combining multiple rule sets |
| 259 | + |
| 260 | +### 3. Combine Rules for Complex Tasks |
| 261 | +- Use multiple @ references for cross-cutting concerns |
| 262 | +- Leverage rule interactions (e.g., API + Config + Testing) |
| 263 | +- Apply domain-specific and quality rules together |
| 264 | + |
| 265 | +### 4. Rule-Based Learning |
| 266 | +``` |
| 267 | +Explain the difference between @python/core naming conventions |
| 268 | +and @python/api response formatting. |
| 269 | +
|
| 270 | +How do @python/config validation rules work with @python/api endpoints? |
| 271 | +``` |
| 272 | + |
| 273 | +### 5. Validation Against Rules |
| 274 | +``` |
| 275 | +Does this code follow @python/quality standards for: |
| 276 | +- Dependencies management |
| 277 | +- Logging practices |
| 278 | +- Performance optimization |
| 279 | +
|
| 280 | +Does this testing code follow @tests/python/unit standards for: |
| 281 | +- pytest usage and fixtures |
| 282 | +- Mocking patterns |
| 283 | +- Test coverage |
| 284 | +- Unit testing best practices |
| 285 | +
|
| 286 | +Does this API test follow @tests/python/api standards for: |
| 287 | +- http.client usage |
| 288 | +- conftest.py fixture usage |
| 289 | +- API testing patterns |
| 290 | +
|
| 291 | +Validate this documentation against @docs standards for: |
| 292 | +- reStructuredText formatting |
| 293 | +- Sphinx directives |
| 294 | +- Cross-references |
| 295 | +- Branding guidelines |
| 296 | +``` |
| 297 | + |
| 298 | +## 📖 Rule-Specific Examples |
| 299 | + |
| 300 | +### Python Core (@python/core) |
| 301 | +``` |
| 302 | +Create a device manager class that: |
| 303 | +- Uses snake_case naming |
| 304 | +- Includes proper docstrings |
| 305 | +- Has type hints for all methods |
| 306 | +- Uses FLCoreLogger for logging |
| 307 | +- Follows the server.py architectural pattern |
| 308 | +``` |
| 309 | + |
| 310 | +### API Development (@python/api) |
| 311 | +``` |
| 312 | +Create a REST endpoint for asset management that: |
| 313 | +- Uses role-based middleware validation |
| 314 | +- Returns camelCase JSON responses |
| 315 | +- Handles route conflicts |
| 316 | +- Includes proper error handling |
| 317 | +- Uses async/await patterns |
| 318 | +``` |
| 319 | + |
| 320 | +### Configuration (@python/config) |
| 321 | +``` |
| 322 | +Design a configuration category that: |
| 323 | +- Includes string, integer, boolean, and JSON types |
| 324 | +- Has proper validation rules |
| 325 | +- Uses quoted default values |
| 326 | +- Avoids reserved category names |
| 327 | +- Includes optional validation constraints |
| 328 | +``` |
| 329 | + |
| 330 | +### Documentation (@docs) |
| 331 | +``` |
| 332 | +Write API documentation that: |
| 333 | +- Uses reStructuredText format |
| 334 | +- Includes proper Sphinx directives |
| 335 | +- Avoids excessive "Fledge" branding |
| 336 | +- Has correct heading hierarchy |
| 337 | +- Includes cross-references to related docs |
| 338 | +``` |
| 339 | + |
| 340 | +### Unit Testing (@tests/python/unit) |
| 341 | +``` |
| 342 | +Create comprehensive unit tests that: |
| 343 | +- Use pytest with proper fixtures |
| 344 | +- Mock external dependencies appropriately |
| 345 | +- Achieve meaningful test coverage |
| 346 | +- Follow unit testing best practices |
| 347 | +- Test both success and failure scenarios |
| 348 | +``` |
| 349 | + |
| 350 | +### API integration Testing (@tests/python/api) |
| 351 | +``` |
| 352 | +Create API integration tests that: |
| 353 | +- Use http.client library exclusively |
| 354 | +- Leverage conftest.py fixtures for environment setup |
| 355 | +- Test API endpoints with authentication flows |
| 356 | +- Use reset_and_start_fledge for clean test environments |
| 357 | +- Follow system test organization patterns |
| 358 | +``` |
| 359 | + |
| 360 | +### Dependencies & Quality (@python/quality) |
| 361 | +``` |
| 362 | +Manage dependencies and code quality: |
| 363 | +- Use requirements.txt for dependency management |
| 364 | +- Follow FLCoreLogger patterns for logging |
| 365 | +- Optimize for edge device performance |
| 366 | +- Ensure Python version compatibility |
| 367 | +- Document dependency constraints |
| 368 | +``` |
| 369 | + |
| 370 | +## 🎯 Best Practices Summary |
| 371 | + |
| 372 | +1. **Trust Automatic Application**: Let Cursor apply rules based on file context |
| 373 | +2. **Use @ References Explicitly**: When you need specific rule compliance |
| 374 | +3. **Combine Rules Strategically**: For Python development with documentation |
| 375 | +4. **Validate Against Rules**: Use rules for code review and quality checks |
| 376 | +5. **Focus on Core Technologies**: Leverage Python and documentation rules together |
| 377 | + |
| 378 | +The rules work best when you let them guide development naturally - they'll automatically apply standards and catch issues as you code! |
0 commit comments