python-best-practices
3
总安装量
3
周安装量
#60995
全站排名
安装命令
npx skills add https://github.com/pproenca/dot-claude --skill python-best-practices
Agent 安装分布
claude-code
3
mcpjam
2
command-code
2
junie
2
windsurf
2
zencoder
2
Skill 文档
Python Community Python >=3.11 Best Practices
Comprehensive performance optimization guide for Python >=3.11 applications. Contains 45 rules across 9 categories, prioritized by impact to guide automated refactoring and code generation.
When to Apply
Reference these guidelines when:
- Writing new Python async/await code
- Processing large datasets or files
- Implementing caching and memoization
- Choosing data structures for performance
- Reviewing code for performance issues
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | I/O Patterns | CRITICAL | io- |
| 2 | Async Concurrency | CRITICAL | async- |
| 3 | Memory Management | HIGH | mem- |
| 4 | Data Structures | HIGH | ds- |
| 5 | Algorithm Efficiency | MEDIUM-HIGH | algo- |
| 6 | Concurrency Model | MEDIUM | conc- |
| 7 | Serialization | MEDIUM | serial- |
| 8 | Caching and Memoization | LOW-MEDIUM | cache- |
| 9 | Runtime Tuning | LOW | runtime- |
Quick Reference
1. I/O Patterns (CRITICAL)
io-async-file-operations– Use async file I/O for non-blocking operationsio-batch-database-operations– Batch database operations to reduce round tripsio-connection-pooling– Use connection pooling for database and HTTP clientsio-streaming-large-files– Stream large files instead of loading into memoryio-use-buffered-io– Use buffered I/O for frequent small writes
2. Async Concurrency (CRITICAL)
async-avoid-async-overhead– Avoid async overhead for CPU-bound workasync-avoid-blocking-calls– Avoid blocking calls in async codeasync-create-task-fire-forget– Store references to fire-and-forget tasksasync-gather-independent-operations– Use asyncio.gather() for independent operationsasync-semaphore-rate-limiting– Use semaphores for concurrency limitingasync-taskgroup-structured-concurrency– Use TaskGroup for structured concurrency
3. Memory Management (HIGH)
mem-avoid-intermediate-lists– Avoid intermediate lists in pipelinesmem-generators-lazy-evaluation– Use generators for lazy evaluationmem-preallocate-lists– Preallocate lists when size is knownmem-slots-dataclass– Use slots for memory-efficient classesmem-string-interning– Leverage string interning for repeated stringsmem-weak-references– Use weak references for caches and observers
4. Data Structures (HIGH)
ds-counter-for-counting– Use Counter for frequency countingds-deque-for-queues– Use deque for O(1) queue operationsds-dict-get-default– Use dict.get() with default instead of KeyError handlingds-namedtuple-immutable-records– Use NamedTuple for immutable lightweight recordsds-set-for-membership– Use Set for O(1) membership testing
5. Algorithm Efficiency (MEDIUM-HIGH)
algo-avoid-repeated-computation– Cache expensive computations in loopsalgo-builtin-functions– Use built-in functions over manual implementationalgo-itertools-recipes– Use itertools for efficient iteration patternsalgo-list-comprehension– Use list comprehensions over manual loopsalgo-local-variable-lookup– Use local variables in hot loopsalgo-string-join– Use str.join() for string concatenation
6. Concurrency Model (MEDIUM)
conc-asyncio-queues– Use asyncio.Queue for producer-consumer patternsconc-avoid-lock-contention– Minimize lock contention in threaded codeconc-choose-right-model– Choose the right concurrency modelconc-process-pool-chunking– Use chunking for ProcessPoolExecutorconc-thread-safe-globals– Use thread-safe data structures for shared state
7. Serialization (MEDIUM)
serial-avoid-pickle-security– Avoid pickle for untrusted dataserial-msgpack-binary– Use MessagePack for compact binary serializationserial-orjson-over-json– Use orjson for high-performance JSONserial-pydantic-validation– Use Pydantic for validated deserialization
8. Caching and Memoization (LOW-MEDIUM)
cache-avoid-over-caching– Avoid over-caching low-value operationscache-cached-property– Use cached_property for expensive computed attributescache-lru-cache-decorator– Use lru_cache for expensive pure functionscache-ttl-expiration– Implement TTL for time-sensitive caches
9. Runtime Tuning (LOW)
runtime-avoid-global-lookups– Avoid repeated global and module lookupsruntime-exception-handling-cost– Minimize exception handling in hot pathsruntime-profile-before-optimizing– Profile before optimizingruntime-use-python311-plus– Upgrade to Python 3.11+ for free performance
How to Use
Read individual reference files for detailed explanations and code examples:
- Section definitions – Category structure and impact levels
- Rule template – Template for adding new rules
Each rule file contains:
- Brief explanation of why it matters
- Incorrect code example with explanation
- Correct code example with explanation
- Additional context and references
Reference Files
| File | Description |
|---|---|
| AGENTS.md | Complete compiled guide with all rules |
| references/_sections.md | Category definitions and ordering |
| assets/templates/_template.md | Template for new rules |
| metadata.json | Version and reference information |