| name | pythonista-patterning |
| description | Pattern discovery and code reuse for Python projects. Use when writing new code, implementing features, or refactoring. Triggers on "pattern", "reuse", "duplicate", "extract", "helper", "refactor", "architecture", "existing code", "search for", "similar", "copy", "paste", or when about to write new functionality. |
Pattern Discovery and Architecture Trust
Core Philosophy
Before writing ANY new code, actively search for existing patterns and reuse opportunities.
Duplicating code with subtle variations is one of the most damaging things you can do to a codebase.
The Pattern Discovery Workflow
When implementing ANY feature:
1. Search for Similar Implementations
grep -r "similar_function" src/
ls src/services/
2. Document Patterns Found
- Note the common structure/approach
- Identify what varies vs what stays the same
- List helper functions or utilities used
3. Follow Established Patterns Exactly
- Use the same parameter names and types
- Use the same error handling approach
- Use the same validation strategy
4. Identify Reuse Opportunities
- Spot duplicated logic that can be extracted
- Find DB queries that appear multiple times
- Notice validation patterns repeated across files
Balance: Don't Over-Engineer, Don't Under-Engineer
Under-Engineering (BAD)
async def update_campaign(community_handle: str, campaign: Campaign):
if campaign.version == 0:
current = await collection.find_one({"community_handle": community_handle})
next_version = (current["campaign"]["version"] + 1) if current else 1
campaign.version = next_version
async def update_drip(community_handle: str, drip: Drip):
if drip.version == 0:
current = await collection.find_one({"community_handle": community_handle})
next_version = (current["drip"]["version"] + 1) if current else 1
drip.version = next_version
Over-Engineering (BAD)
class VersionedEntityManager(ABC, Generic[T]):
@abstractmethod
async def get_current_version(self, handle: str) -> int | None: ...
Right Balance (GOOD)
async def auto_increment_version(
collection,
community_handle: str,
field_name: str,
entity: VersionedEntity,
) -> VersionedEntity:
"""Auto-increment version using version=0 sentinel."""
if entity.version == 0:
current = await collection.find_one({"community_handle": community_handle})
current_version = current.get(field_name, {}).get("version", 0) if current else 0
entity.version = current_version + 1
return entity
campaign = await auto_increment_version(collection, handle, "campaign", campaign)
drip = await auto_increment_version(collection, handle, "drip", drip)
Simple Helper vs Infrastructure
Simple Helper - Extract Immediately
- Duplicated calculations
- Repeated DB queries (same query in 3+ places)
- Filtering/validation logic repeated across functions
- Action: Extract now, include in current PR
Infrastructure - Ask First
- Affects how multiple components interact
- Introduces new architectural layer
- Changes system structure
- Action: Ask user, likely separate PR
Trust the Architecture
NEVER duplicate validation that already exists in lower layers.
Wrong - Service Layer Blocks API
async def _create_item(self, name, item):
versions = await api.list_item_versions(make_id(name), include_deleted=False)
if versions:
raise ValueError(f"Item '{name}' already exists")
return await api.create_library_item(item, user_id=user_id)
Correct - Service Layer Trusts API
async def _create_item(self, name, item):
validate_name(name)
return await api.create_library_item(item, user_id=user_id)
Before Adding Validation, Ask:
- Does a lower layer already handle this?
- What is this layer's responsibility?
- Am I preventing the lower layer from doing its job?
Merge Conflicts: Investigate Before Restoring
ALWAYS investigate WHY code is missing before restoring it.
git show main:path/to/file.py | grep -A 20 "function_name"
git log --oneline --all -- path/to/file.py | head -20
Ask These Questions
- Was this deletion intentional?
- What's the new way to accomplish this?
- Why is the new approach better?
- Are approaches complementary, competing, or extensible?
Checklist
Before writing new code:
Related Skills
- For debugging philosophy, see
/pythonista-debugging
- For testing patterns, see
/pythonista-testing
- For code review, see
/pythonista-reviewing