-
Notifications
You must be signed in to change notification settings - Fork 0
Implement task tracker CLI with JSON persistence and tests #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Rahulthespy
wants to merge
1
commit into
master
Choose a base branch
from
codex/develop-complete-code-solution
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| __pycache__/ | ||
| *.pyc | ||
| .venv/ | ||
| .pytest_cache/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,35 @@ | ||
| # Rahul-s-LAB | ||
| My LAB respository.. | ||
|
|
||
| A complete, working lab solution implemented as a **Task Tracker CLI**. | ||
|
|
||
| ## Features | ||
| - Add tasks | ||
| - List tasks | ||
| - Mark tasks as complete | ||
| - Delete tasks | ||
| - JSON file persistence | ||
|
|
||
| ## Quickstart | ||
| ```bash | ||
| python -m venv .venv | ||
| source .venv/bin/activate | ||
| pip install -e . pytest | ||
| ``` | ||
|
|
||
| Run commands: | ||
| ```bash | ||
| labtracker add "Write the complete code" | ||
| labtracker list | ||
| labtracker done 1 | ||
| labtracker delete 1 | ||
| ``` | ||
|
|
||
| Use a custom database file: | ||
| ```bash | ||
| labtracker --db ./my_tasks.json add "Sample" | ||
| ``` | ||
|
|
||
| ## Test | ||
| ```bash | ||
| pytest | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| [build-system] | ||
| requires = ["setuptools>=68", "wheel"] | ||
| build-backend = "setuptools.build_meta" | ||
|
|
||
| [project] | ||
| name = "rahul-lab" | ||
| version = "0.1.0" | ||
| description = "A complete command-line lab task tracker solution" | ||
| readme = "README.md" | ||
| requires-python = ">=3.10" | ||
| dependencies = [] | ||
|
|
||
| [project.scripts] | ||
| labtracker = "src.main:main" | ||
|
|
||
| [tool.pytest.ini_options] | ||
| pythonpath = ["."] | ||
| testpaths = ["tests"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| from pathlib import Path | ||
|
|
||
| from src.tracker import TaskTracker | ||
|
|
||
|
|
||
| def _build_parser() -> argparse.ArgumentParser: | ||
| parser = argparse.ArgumentParser(description="Lab Task Tracker") | ||
| parser.add_argument( | ||
| "--db", | ||
| default=".lab/tasks.json", | ||
| help="Path to the tracker JSON file (default: .lab/tasks.json)", | ||
| ) | ||
|
|
||
| subparsers = parser.add_subparsers(dest="command", required=True) | ||
|
|
||
| add_parser = subparsers.add_parser("add", help="Add a new task") | ||
| add_parser.add_argument("title", help="Task title") | ||
|
|
||
| subparsers.add_parser("list", help="List tasks") | ||
|
|
||
| done_parser = subparsers.add_parser("done", help="Mark task as completed") | ||
| done_parser.add_argument("id", type=int, help="Task id") | ||
|
|
||
| delete_parser = subparsers.add_parser("delete", help="Delete a task") | ||
| delete_parser.add_argument("id", type=int, help="Task id") | ||
|
|
||
| return parser | ||
|
|
||
|
|
||
| def main() -> None: | ||
| parser = _build_parser() | ||
| args = parser.parse_args() | ||
|
|
||
| tracker = TaskTracker(Path(args.db)) | ||
|
|
||
| if args.command == "add": | ||
| task = tracker.add_task(args.title) | ||
| print(f"Added task #{task.id}: {task.title}") | ||
| elif args.command == "list": | ||
| tasks = tracker.list_tasks() | ||
| if not tasks: | ||
| print("No tasks yet.") | ||
| return | ||
|
|
||
| for task in tasks: | ||
| status = "✅" if task.done else "⬜" | ||
| print(f"{status} [{task.id}] {task.title}") | ||
| elif args.command == "done": | ||
| task = tracker.complete_task(args.id) | ||
| print(f"Completed task #{task.id}: {task.title}") | ||
| elif args.command == "delete": | ||
| tracker.delete_task(args.id) | ||
| print(f"Deleted task #{args.id}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import asdict, dataclass | ||
| from pathlib import Path | ||
| import json | ||
|
|
||
|
|
||
| @dataclass | ||
| class Task: | ||
| id: int | ||
| title: str | ||
| done: bool = False | ||
|
|
||
|
|
||
| class TaskTracker: | ||
| def __init__(self, storage_path: Path) -> None: | ||
| self.storage_path = storage_path | ||
| self.tasks: list[Task] = [] | ||
| self._load() | ||
|
|
||
| def _load(self) -> None: | ||
| if not self.storage_path.exists(): | ||
| self.tasks = [] | ||
| return | ||
|
|
||
| with self.storage_path.open("r", encoding="utf-8") as f: | ||
| payload = json.load(f) | ||
|
|
||
| self.tasks = [Task(**item) for item in payload] | ||
|
|
||
| def _save(self) -> None: | ||
| self.storage_path.parent.mkdir(parents=True, exist_ok=True) | ||
| with self.storage_path.open("w", encoding="utf-8") as f: | ||
| json.dump([asdict(task) for task in self.tasks], f, indent=2) | ||
|
|
||
| def add_task(self, title: str) -> Task: | ||
| next_id = 1 if not self.tasks else max(task.id for task in self.tasks) + 1 | ||
| task = Task(id=next_id, title=title.strip(), done=False) | ||
| if not task.title: | ||
| raise ValueError("Task title cannot be empty.") | ||
| self.tasks.append(task) | ||
| self._save() | ||
| return task | ||
|
|
||
| def list_tasks(self) -> list[Task]: | ||
| return self.tasks | ||
|
|
||
| def complete_task(self, task_id: int) -> Task: | ||
| task = self._find(task_id) | ||
| task.done = True | ||
| self._save() | ||
| return task | ||
|
|
||
| def delete_task(self, task_id: int) -> None: | ||
| task = self._find(task_id) | ||
| self.tasks.remove(task) | ||
| self._save() | ||
|
|
||
| def _find(self, task_id: int) -> Task: | ||
| for task in self.tasks: | ||
| if task.id == task_id: | ||
| return task | ||
| raise ValueError(f"Task with id={task_id} was not found.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| from src.tracker import TaskTracker | ||
|
|
||
|
|
||
| def test_add_and_list_tasks(tmp_path: Path) -> None: | ||
| tracker = TaskTracker(tmp_path / "tasks.json") | ||
| tracker.add_task("Write code") | ||
| tracker.add_task("Build solution") | ||
|
|
||
| tasks = tracker.list_tasks() | ||
| assert [t.title for t in tasks] == ["Write code", "Build solution"] | ||
| assert [t.id for t in tasks] == [1, 2] | ||
|
|
||
|
|
||
| def test_complete_and_delete_task(tmp_path: Path) -> None: | ||
| tracker = TaskTracker(tmp_path / "tasks.json") | ||
| tracker.add_task("Task A") | ||
| tracker.complete_task(1) | ||
| assert tracker.list_tasks()[0].done is True | ||
|
|
||
| tracker.delete_task(1) | ||
| assert tracker.list_tasks() == [] | ||
|
|
||
|
|
||
| def test_raises_for_invalid_task(tmp_path: Path) -> None: | ||
| tracker = TaskTracker(tmp_path / "tasks.json") | ||
| with pytest.raises(ValueError): | ||
| tracker.complete_task(9) | ||
|
|
||
|
|
||
| def test_rejects_blank_title(tmp_path: Path) -> None: | ||
| tracker = TaskTracker(tmp_path / "tasks.json") | ||
| with pytest.raises(ValueError): | ||
| tracker.add_task(" ") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TaskTrackerraisesValueErrorfor normal user mistakes (e.g., unknown task IDs or blank titles), but this call path is not wrapped in error handling, so common input errors print full Python tracebacks and look like crashes. For example,doneon a non-existent ID currently exits with a stack trace instead of a clean CLI error; this should be handled inmain()with a user-facing message and non-zero exit.Useful? React with 👍 / 👎.