Boss lesson · Unit 08
A task manager
One whole program: model, commands, saving, tests, refactor
What you practise
- One program from an empty file: a data model, commands, saving and loading, tests, and a refactor at the end. What to learn next
Try a question from this lesson
Pick one
A task manager starts with the shape of one task: a title, and whether it is done. Here that is a dataclass. What does this program show?
from dataclasses import dataclass
@dataclass
class Task:
title: str
done: bool = False
tasks = [Task("plan"), Task("test", True)]
todo = [t.title for t in tasks
if not t.done]
print(todo)Hint
One task was made with a second value. What does the other one hold for it?
Correct answer
['plan']
Why
"plan" was made without a second value, so its done is the default, False. "test" was made as done. The comprehension keeps the titles of the tasks that are not done: only "plan".
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
The plan
A whole program is built in layers, and each layer uses only the ones under it.
Decide the data first. Then write one small function for each thing a user can do. Then make the data survive between runs. Test each layer before you build on it.
1. model Task: id, title, done
2. store a list of Task objects
3. commands add, complete, remove
4. files save and load as JSON
5. tests assert after every changeConcept
Tests
A test is a small function that calls your code with input you chose and checks the result with assert.
If an assert is false, the program stops right there and shows you the line. If every test passes, you can change the code and find out in a second whether you broke it.
def test_add():
tasks = []
add(tasks, "write")
assert len(tasks) == 1
assert tasks[0].title == "write"
assert not tasks[0].done
test_add()
print("all tests pass")Concept
What to learn next
You can now read and write real Python: data, functions, files, classes, generators, tests.
Two roadmaps follow on. Python in Depth goes under the surface, to what names, functions and classes really are. Algorithm Foundations teaches you to count the work your code does.
And keep building. The fastest way forward is a program you want to exist.
9 more exercises in the app
- 4× type the answer
- 1× order the lines
- 3× multiple choice
- 1× fill the blanks
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.