Python Final Project ā Building a Complete Application
This tutorial guides you through building a real-world Python project from scratch, applying everything you have learned.
Learning Objectives
- Plan and structure a Python project
- Implement core functionality with best practices
- Write tests and documentation
- Deploy to production
Project Planning
Before writing code, answer these questions:
- What problem are you solving? Be specific.
- Who is the user? Understand their needs.
- What are the core features? Start with MVP (Minimum Viable Product).
- What tech stack? Choose tools you know (or want to learn).
Example Project: Task Manager API
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
Problem: Teams need a simple way to track tasks
User: Small teams (2-10 people)
Core Features:
- Create, read, update, delete tasks
- Assign tasks to team members
- Mark tasks as complete
- Filter by status/assignee
Tech Stack:
- FastAPI (web framework)
- SQLite (database)
- pytest (testing)
- Docker (deployment)
Project Structure
task-manager/
āāā src/
ā āāā task_manager/
ā āāā __init__.py
ā āāā main.py # Application entry point
ā āāā models.py # Data models
ā āāā routes.py # API endpoints
ā āāā database.py # Database connection
ā āāā schemas.py # Pydantic schemas
āāā tests/
ā āāā __init__.py
ā āāā conftest.py # Shared fixtures
ā āāā test_models.py # Model tests
ā āāā test_routes.py # API tests
āāā docs/
ā āāā api.md # API documentation
āāā Dockerfile
āāā docker-compose.yml
āāā requirements.txt
āāā pyproject.toml
āāā README.md
āāā .github/
āāā workflows/
āāā ci.yml
Implementation Example
Models (src/task_manager/models.py)
from dataclasses import dataclass, field
from datetime import datetime
from enum import Enum
class Status(str, Enum):
PENDING = "pending"
IN_PROGRESS = "in_progress"
COMPLETED = "completed"
@dataclass
class Task:
title: str
description: str = ""
status: Status = Status.PENDING
assignee: str = ""
created_at: datetime = field(default_factory=datetime.now)
id: int = 0
API Routes (src/task_manager/routes.py)
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import List, Optional
app = FastAPI(title="Task Manager API")
class TaskCreate(BaseModel):
title: str
description: str = ""
assignee: str = ""
class TaskResponse(BaseModel):
id: int
title: str
description: str
status: str
assignee: str
tasks_db = {} # In-memory storage for demo
next_id = 1
@app.post("/tasks", response_model=TaskResponse)
def create_task(task: TaskCreate):
global next_id
new_task = {
"id": next_id,
"title": task.title,
"description": task.description,
"status": "pending",
"assignee": task.assignee,
}
tasks_db[next_id] = new_task
next_id += 1
return new_task
@app.get("/tasks", response_model=List[TaskResponse])
def list_tasks(status: Optional[str] = None):
if status:
return [t for t in tasks_db.values() if t["status"] == status]
return list(tasks_db.values())
@app.put("/tasks/{task_id}/complete")
def complete_task(task_id: int):
if task_id not in tasks_db:
raise HTTPException(status_code=404, detail="Task not found")
tasks_db[task_id]["status"] = "completed"
return tasks_db[task_id]
Tests (tests/test_routes.py)
from fastapi.testclient import TestClient
from src.task_manager.main import app
client = TestClient(app)
def test_create_task():
response = client.post("/tasks", json={"title": "Buy groceries"})
assert response.status_code == 200
data = response.json()
assert data["title"] == "Buy groceries"
assert data["status"] == "pending"
def test_list_tasks():
response = client.get("/tasks")
assert response.status_code == 200
assert isinstance(response.json(), list)
def test_complete_task():
# Create a task first
response = client.post("/tasks", json={"title": "Test task"})
task_id = response.json()["id"]
# Complete it
response = client.put(f"/tasks/{task_id}/complete")
assert response.status_code == 200
assert response.json()["status"] == "completed"
Implementation Checklist
Phase 1: Setup
- [ ] Create virtual environment
- [ ] Install dependencies
- [ ] Set up project structure
- [ ] Initialize git repository
Phase 2: Core Development
- [ ] Implement data models
- [ ] Create database layer
- [ ] Build API endpoints
- [ ] Add validation
Phase 3: Testing
- [ ] Write unit tests
- [ ] Write integration tests
- [ ] Achieve > 80% code coverage
Phase 4: Documentation
- [ ] Write README.md
- [ ] Document API endpoints
- [ ] Add usage examples
Phase 5: Deployment
- [ ] Create Dockerfile
- [ ] Set up CI/CD pipeline
- [ ] Deploy to cloud
- [ ] Monitor and iterate
Key Takeaways
- Start with MVP ā add features iteratively
- Write tests as you develop, not after
- Document your code and README
- Use version control (git) from day one
- Deploy early and often
- Get feedback from users early
- Refactor regularly ā clean code is maintainable code
- Celebrate small wins ā every feature is progress