Sub topic 2
Modeling check-ins as dates
This chapter assumes you know basic Python (dictionaries, sets, loops) and the date and timedelta types from the datetime module. It decides how the tracker stores its data. Every later calculation depends on that choice.
What a check-in records
The previous chapter defined a habit as something you can answer with yes or no for a given day. A check-in therefore needs only two facts: which habit, and which calendar day. It needs no time of day, no count and no note.
That gives a simple rule. A habit was done on a day if that day is stored for the habit. A habit was missed if the day is absent. The tracker never stores misses. They are the gaps between stored days.
One set of dates per habit
The tracker stores a dictionary. Each key is a habit name, and each value is a set of date objects.
from datetime import date
log = {
"read": {date(2026, 11, 24), date(2026, 11, 25), date(2026, 11, 27)},
"stretch": {date(2026, 11, 26)},
}
print(date(2026, 11, 25) in log["read"]) # True
print(date(2026, 11, 26) in log["read"]) # False
print(sorted(log["read"]))Here "read" was done on the 24th, 25th and 27th. The 26th is missing, so that day was missed.
Why a set fits
A set matches the question the tracker asks, which is whether a given day is in the habit's record. It has three properties that help here.
- No duplicates. Adding a date that is already present changes nothing. Checking in twice on the same day cannot inflate any number. The next chapter relies on this.
- Fast membership tests.
day in daystakes roughly constant time. The streak calculation asks this question once per day it walks back, so the cost matters. - No order. A set does not remember insertion order. When you need chronological order, as for the longest streak, call
sorted()on it.
A list of dates would work, but you would have to remove duplicates yourself and every in test would scan the whole list.
| Structure | Duplicate check-in | `day in days` | Ordered |
|---|---|---|---|
| `list` of dates | Stored twice unless you guard | Scans the list | Only if you keep it sorted |
| `set` of dates | Ignored | Roughly constant time | No, use `sorted()` |
Use date, not datetime
Store date objects, not datetime objects. A datetime includes the time of day, so two check-ins on the same day at different times are different values. They would count as two entries.
from datetime import date, datetime
moment = datetime(2026, 11, 27, 8, 59)
print(moment.date()) # 2026-11-27
print(moment == date(2026, 11, 27)) # False
mixed = {moment, date(2026, 11, 27)}
print(len(mixed)) # 2, one day counted twicedatetime is a subclass of date, which makes this mistake easy to miss. The two compare as unequal, so a set holds both. If you ever start from a datetime, convert it with .date() before storing it.
The calendar day is the unit
A check-in belongs to a calendar day, not to a 24-hour period. If you log at 23:50 and again at 00:10, those are two different days and both count. date.today() reads the computer's local clock, so "today" follows your own timezone. For a personal tracker that is what you want.
The model also keeps a clear line between data and results. The log holds only facts: these days were done. Streaks and completion rates are derived from those facts each time you need them. They are never stored. If you stored a streak counter, a missed day or a correction would leave it stale. Recomputing from the dates cannot drift out of sync.
Reading the model
These expressions all follow from the structure.
len(log["read"])is the total number of days "read" was done.max(log["read"])is the most recent check-in. It raisesValueErroron an empty set, so guard against that.log.get("walk", set())gives an empty set for a habit with no check-ins yet, which avoids aKeyError.
Exercise
A learner stores check-ins as datetime.now() values in a set. They log "read" once on Monday evening and once on Tuesday morning, then check whether Monday is in the set with date(2026, 11, 23) in days. The answer is False. Give two reasons the design is wrong and say what to change.
Show answer
First, a datetime never equals a date, so membership tests against date objects fail. Second, two check-ins on the same day at different times would be stored as two separate entries, so duplicates are no longer ignored. Fix it by storing datetime.now().date() (or date.today()), so each entry is a calendar date.