separate-monolithic-python
Break large Python files (>500 LOC) into smaller, well-organized modules with proper package structure. Use when a Python file is too large, monolithic, or needs refactoring. Triggered by requests mentioning "too large", "separate", "split", "break up", or "refactor" for Python files.
Best use case
separate-monolithic-python is best used when you need a repeatable AI agent workflow instead of a one-off prompt.
Break large Python files (>500 LOC) into smaller, well-organized modules with proper package structure. Use when a Python file is too large, monolithic, or needs refactoring. Triggered by requests mentioning "too large", "separate", "split", "break up", or "refactor" for Python files.
Teams using separate-monolithic-python should expect a more consistent output, faster repeated execution, less prompt rewriting.
When to use this skill
- You want a reusable workflow that can be run more than once with consistent structure.
When not to use this skill
- You only need a quick one-off answer and do not need a reusable workflow.
- You cannot install or maintain the underlying files, dependencies, or repository context.
Installation
Claude Code / Cursor / Codex
Manual Installation
- Download SKILL.md from GitHub
- Place it in
.claude/skills/separate-monolithic-python/SKILL.mdinside your project - Restart your AI agent — it will auto-discover the skill
How separate-monolithic-python Compares
| Feature / Agent | separate-monolithic-python | Standard Approach |
|---|---|---|
| Platform Support | Not specified | Limited / Varies |
| Context Awareness | High | Baseline |
| Installation Complexity | Unknown | N/A |
Frequently Asked Questions
What does this skill do?
Break large Python files (>500 LOC) into smaller, well-organized modules with proper package structure. Use when a Python file is too large, monolithic, or needs refactoring. Triggered by requests mentioning "too large", "separate", "split", "break up", or "refactor" for Python files.
Where can I find the source code?
You can find the source code on GitHub using the link provided at the top of the page.
SKILL.md Source
# Separate Monolithic Python Code
Break large Python files into maintainable modules following Python best practices.
## Workflow
### Step 1: Analyze
1. Read entire file to understand structure
2. Identify components (classes, function groups, constants)
3. Count lines (>500 LOC needs separation)
4. Map dependencies (what depends on what)
### Step 2: Plan Structure
Choose a separation pattern:
**By Responsibility** (Recommended):
```
mypackage/
├── __init__.py # Public API exports
├── models.py # Data models/classes
├── services.py # Business logic
├── utils.py # Helper functions
└── constants.py # Configuration
```
**By Feature**:
```
mypackage/
├── __init__.py
├── feature_a/
│ ├── __init__.py
│ ├── models.py
│ └── logic.py
└── feature_b/
```
**By Layer** (Domain-driven):
```
mypackage/
├── __init__.py
├── domain/ # Core models
├── application/ # Use cases
└── infrastructure/ # External deps
```
Present plan to user before proceeding.
### Step 3: Create Structure
```bash
mkdir mypackage
touch mypackage/__init__.py mypackage/models.py mypackage/services.py
```
### Step 4: Extract Code
Extract in dependency order:
1. **Constants** (no dependencies)
2. **Models** (minimal dependencies)
3. **Utilities** (depend on constants/models)
4. **Services** (depend on everything)
5. **Main** (orchestrate all)
### Step 5: Update Imports
**In new modules:**
```python
# models.py
from .constants import DEFAULT_ROLE
from .utils import validate_email
```
**In `__init__.py` (public API):**
```python
from .models import User, Product
from .services import create_user
__all__ = ['User', 'Product', 'create_user']
```
**In external files:**
```python
# Before: from monolith import User
# After: from mypackage import User
```
### Step 6: Validate
```bash
ruff check mypackage/
mypy mypackage/
python -c "from mypackage import User"
pytest tests/
```
## Key Principles
**High Cohesion**: Keep related code together
- Group by purpose, not type
- Example: `user_service.py` not `all_services.py`
**Low Coupling**: Minimize dependencies
- Avoid circular imports
- Use dependency injection
**Single Responsibility**: One clear purpose per module
**Clear API**: Use `__init__.py` to expose public interface
## Handling Circular Dependencies
**Option 1: Move shared code**
```python
# Create shared.py for common code
```
**Option 2: TYPE_CHECKING**
```python
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from .services import UserService # Only for type hints
```
**Option 3: Late import**
```python
def process_user():
from .services import create_user # Import inside function
create_user()
```
## File Size Guidelines
- ✅ **Ideal**: 100-300 lines
- ⚠️ **Warning**: 300-500 lines (consider splitting)
- ❌ **Too large**: >500 lines (should split)
## Quick Example
**Before** (monolith.py - 800 lines):
```python
DATABASE_URL = "sqlite:///./test.db"
class User:
def __init__(self, name):
self.name = name
def create_user(name):
return User(name)
app = FastAPI()
@app.get("/users")
def get_users():
return []
```
**After**:
```
api/
├── __init__.py
├── config.py # DATABASE_URL
├── models.py # User class
├── services.py # create_user
└── routes.py # FastAPI routes
```
## Troubleshooting
**Import errors**: Check `__init__.py` exports, verify relative imports (`.module`)
**Circular imports**: Use TYPE_CHECKING or late imports, or extract shared code
**Tests failing**: Update test imports to new package structure
For detailed examples, patterns, and troubleshooting, see [references/detailed-guide.md](references/detailed-guide.md).Related Skills
todo-manage
Manage and organize TODO.md at project root - plan features, track progress, mark tasks complete, and clean up completed items. Use when planning tasks, organizing work, reviewing priorities, or managing the project TODO list. Access via /dipeotodos command.
test-backend-router
Test implementation of thin router skill for DiPeO backend. Provides decision criteria and documentation anchors for FastAPI server, CLI (dipeo run/results/metrics/compile/export), SQLite schema, and MCP integration in apps/server/. Use when task mentions CLI commands, server endpoints, database queries, or MCP tools.
maintain-docs
Update and maintain documentation to reflect current implementation after code changes, refactoring, or new features. Remove temporal language, verify accuracy against code, and keep docs current. Use when updating docs, syncing documentation, removing outdated info, or after implementing features.
import-refactor
Update all import statements, module references, string paths, and config references after moving or renaming files and modules. Handles Python, TypeScript, and JavaScript imports. Use when moving files, renaming modules, restructuring directories, or consolidating code.
skill-generator
Create new Claude Code skills with proper structure and best practices. Use when the user wants to create a new skill, extend Claude's capabilities, or needs help structuring a SKILL.md file.
doc-lookup
Locate and retrieve specific documentation sections from DiPeO's docs/ by heading anchors or keywords. Returns minimal, targeted excerpts instead of full files. Use when you need precise documentation context without loading entire guides.
dipeo-package-maintainer
Router skill for DiPeO runtime Python code (execution handlers, service architecture, domain models, LLM infrastructure). Use when task mentions node handlers, EventBus, ServiceRegistry, Envelope pattern, or domain logic. For simple tasks, handle directly; for complex work, escalate to dipeo-package-maintainer agent.
dipeo-frontend-dev
Router skill for DiPeO frontend (React, visual editor, GraphQL integration, TypeScript types). Use when task mentions React components, diagram editor, GraphQL hooks, or type errors. For simple tasks, handle directly; for complex work, escalate to dipeo-frontend-dev agent.
dipeo-codegen-pipeline
Router skill for DiPeO code generation pipeline (TypeScript specs → IR → Python/GraphQL). Use when task mentions TypeScript models, IR builders, generated code diagnosis, or codegen workflow. For simple tasks, handle directly; for complex work, escalate to dipeo-codegen-pipeline agent.
dipeo-backend
Router skill for DiPeO backend ecosystem (server/ and cli/): FastAPI server, CLI tools, database, MCP integration. Use when task mentions CLI commands, server endpoints, database queries, or MCP tools. For simple tasks, handle directly; for complex work, escalate to dipeo-backend agent.
clean-comments
Remove unnecessary, redundant, or obvious code comments while preserving valuable explanations. Use when cleaning up comments, removing verbose documentation, simplifying inline comments, or preparing code for review.
python-development
Modern Python development with Python 3.12+, Django, FastAPI, async patterns,