Self-contained, skip-friendly lab starting points (#103)
Co-authored-by: claude <claude@jpaul.io> Co-committed-by: claude <claude@jpaul.io>
This commit was merged in pull request #103.
This commit is contained in:
@@ -202,6 +202,18 @@ Passing for the right reason is the skill.
|
||||
|
||||
## Hands-on lab
|
||||
|
||||
|
||||
> **Starting point (this lab is skip-friendly).** You do not need to have done the earlier labs.
|
||||
> To begin from a clean, known state, copy this module's snapshot into a fresh `tasks-app` and
|
||||
> make the first commit:
|
||||
>
|
||||
> ```bash
|
||||
> mkdir -p ~/ai-workflow-course/tasks-app
|
||||
> cp -r ~/ai-workflow-course/modules/13-testing-in-the-ai-era/lab/start/. ~/ai-workflow-course/tasks-app/
|
||||
> cd ~/ai-workflow-course/tasks-app && git init -b main && git add -A && git commit -m "start: module 13"
|
||||
> ```
|
||||
>
|
||||
> Already carrying your `tasks-app` from earlier modules? Keep using it and ignore this box.
|
||||
**Lab language:** Python (standard-library `unittest`), with a couple of shell commands to run the
|
||||
suite. Nothing to install.
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
# Demo app: `tasks` (Module 13 copy)
|
||||
|
||||
The same tiny task tracker from Modules 1 and 2, with one feature added: a `count` command backed
|
||||
by `TaskList.pending_count()`. Use this copy for the Module 13 lab so everyone starts from the same
|
||||
code, including the same latent bug.
|
||||
|
||||
If you already have a `tasks-app` from earlier modules, you can use that instead; just make sure it
|
||||
has a `count` command (the Module 2 lab added one). The planted bug in this copy is there on purpose.
|
||||
|
||||
## Files
|
||||
|
||||
- `tasks.py`: core logic (`Task`, `TaskList`), now with `pending_count()`.
|
||||
- `cli.py`: command-line front end. Adds `count`.
|
||||
|
||||
## Run it
|
||||
|
||||
```bash
|
||||
python cli.py add "write the tests"
|
||||
python cli.py add "fix the bug"
|
||||
python cli.py done 0
|
||||
python cli.py list
|
||||
python cli.py count
|
||||
```
|
||||
|
||||
Requires Python 3.10+. No third-party packages; tests use the standard library `unittest`.
|
||||
@@ -0,0 +1,59 @@
|
||||
"""Tiny command-line front end for the demo task app.
|
||||
|
||||
Run it:
|
||||
python cli.py add "write the lesson"
|
||||
python cli.py list
|
||||
python cli.py count
|
||||
|
||||
State is kept in tasks.json next to this file. Same minimal app from Modules 1 and 2, with a
|
||||
`count` command bolted on.
|
||||
"""
|
||||
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from tasks import Task, TaskList
|
||||
|
||||
STATE = Path(__file__).parent / "tasks.json"
|
||||
|
||||
|
||||
def load() -> TaskList:
|
||||
if not STATE.exists():
|
||||
return TaskList()
|
||||
raw = json.loads(STATE.read_text())
|
||||
return TaskList(tasks=[Task(**t) for t in raw])
|
||||
|
||||
|
||||
def save(tlist: TaskList) -> None:
|
||||
STATE.write_text(json.dumps([t.__dict__ for t in tlist.tasks], indent=2))
|
||||
|
||||
|
||||
def main(argv: list[str]) -> int:
|
||||
tlist = load()
|
||||
if not argv:
|
||||
print("usage: python cli.py [add <title> | list | done <index> | count]")
|
||||
return 1
|
||||
|
||||
command = argv[0]
|
||||
if command == "add":
|
||||
title = " ".join(argv[1:])
|
||||
tlist.add(title)
|
||||
save(tlist)
|
||||
print(f"added: {title}")
|
||||
elif command == "list":
|
||||
print(tlist.render())
|
||||
elif command == "done":
|
||||
tlist.complete(int(argv[1]))
|
||||
save(tlist)
|
||||
print("updated")
|
||||
elif command == "count":
|
||||
print(f"{tlist.pending_count()} task(s) pending")
|
||||
else:
|
||||
print(f"unknown command: {command}")
|
||||
return 1
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main(sys.argv[1:]))
|
||||
@@ -0,0 +1,43 @@
|
||||
"""Core task logic for the demo app.
|
||||
|
||||
Same running example from Modules 1 and 2, carried forward. It has grown one feature since then:
|
||||
a `pending_count()` helper that the AI added to back a `count` command. The feature "works" in
|
||||
the obvious case, which is exactly the kind of code this module teaches you to verify properly.
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
|
||||
@dataclass
|
||||
class Task:
|
||||
title: str
|
||||
done: bool = False
|
||||
|
||||
|
||||
@dataclass
|
||||
class TaskList:
|
||||
tasks: list[Task] = field(default_factory=list)
|
||||
|
||||
def add(self, title: str) -> Task:
|
||||
task = Task(title=title)
|
||||
self.tasks.append(task)
|
||||
return task
|
||||
|
||||
def complete(self, index: int) -> None:
|
||||
self.tasks[index].done = True
|
||||
|
||||
def pending(self) -> list[Task]:
|
||||
return [t for t in self.tasks if not t.done]
|
||||
|
||||
def pending_count(self) -> int:
|
||||
# Added by the AI to support `cli.py count`. Looks right, ran fine in a quick check.
|
||||
return len(self.tasks)
|
||||
|
||||
def render(self) -> str:
|
||||
if not self.tasks:
|
||||
return "(no tasks yet)"
|
||||
lines = []
|
||||
for i, task in enumerate(self.tasks):
|
||||
box = "[x]" if task.done else "[ ]"
|
||||
lines.append(f"{i}. {box} {task.title}")
|
||||
return "\n".join(lines)
|
||||
Reference in New Issue
Block a user