Sub topic 11
Tracking habits that are not daily
This chapter assumes you have the daily tracker from the earlier chapters. That means a log of habit names mapped to sets of dates, plus the current_streak and completion_rate functions. It also assumes you are comfortable with date, timedelta and weekday().
Why daily streaks break down
Some habits are not meant to happen every day. "Go for a run three times a week" and "water the plants on Mondays and Thursdays" are both reasonable goals. A daily streak punishes them. If you run Monday, Wednesday and Friday, your daily streak is never longer than 1, even though you did exactly what you planned.
The data model does not need to change. Each habit is still a set of dates. What changes is the unit you measure: a week, or a set of scheduled weekdays, instead of a single day.
Option 1: a weekly target
For a goal like "three times a week", group the check-ins by week and compare each week's count to the target. This chapter treats weeks as starting on Monday. date.weekday() returns 0 for Monday, so subtracting it from a date gives that week's Monday.
The weekly streak is the number of consecutive weeks that met the target. It follows the same rule as the daily streak. If the current week has not met the target yet, start counting from last week, so the streak is not lost while the week is still in progress.
from datetime import date, timedelta
def week_start(day):
return day - timedelta(days=day.weekday()) # the Monday of that week
def weekly_counts(days):
counts = {}
for d in days:
start = week_start(d)
counts[start] = counts.get(start, 0) + 1
return counts
def weekly_streak(days, target, today=None):
today = today or date.today()
counts = weekly_counts(days)
week = week_start(today)
if counts.get(week, 0) < target:
week -= timedelta(weeks=1) # this week is still in progress
streak = 0
while counts.get(week, 0) >= target:
streak += 1
week -= timedelta(weeks=1)
return streak
days = {
date(2026, 11, 9), date(2026, 11, 11), date(2026, 11, 13), # week of Nov 9: 3
date(2026, 11, 16), date(2026, 11, 18), date(2026, 11, 20), # week of Nov 16: 3
date(2026, 11, 23), # week of Nov 23: 1 so far
}
print(weekly_streak(days, target=3, today=date(2026, 11, 27))) # 2The current week has only one check-in, so it does not count yet. The two earlier weeks both reached 3, so the streak is 2.
Option 2: fixed weekdays
When the habit has specific days, store the schedule as a set of weekday numbers, such as (0, 3) for Monday and Thursday. Then measure the completion rate only over the days that were due. Unscheduled days are neither successes nor failures.
from datetime import date, timedelta
def scheduled_rate(days, end, scheduled=(0, 2, 4), window=28):
start = end - timedelta(days=window - 1)
window_days = [start + timedelta(days=i) for i in range(window)]
due = [d for d in window_days if d.weekday() in scheduled]
if not due:
return 0.0 # avoid dividing by zero for an empty schedule
done = sum(1 for d in due if d in days)
return done / len(due)
days = {
date(2026, 11, 9), date(2026, 11, 11), date(2026, 11, 13),
date(2026, 11, 16), date(2026, 11, 18), date(2026, 11, 20),
date(2026, 11, 23),
}
print(round(scheduled_rate(days, end=date(2026, 11, 27)), 2)) # 0.58The 28-day window ending on 27 November contains 12 Monday, Wednesday and Friday dates. Seven of them were logged, and 7 / 12 is about 0.58.
A scheduled-day streak works the same way. Walk backwards from today and skip any day that is not scheduled. Stop at the first scheduled day that has no check-in. As with the daily streak, a scheduled day that is today and not yet logged should not end the streak.
Common errors
- Dividing by the wrong total. Dividing scheduled check-ins by the window length (28) makes a perfect Monday/Wednesday/Friday record look like 43%. Divide by the number of due days.
- Counting extra check-ins. With a target of 3, a week with 5 check-ins still counts as one successful week. Compare with
>=, and do not add the surplus to the streak. - Judging an unfinished week. Treating the current week as failed on Monday morning hides your real streak. Skip it, as
weekly_streakdoes, until it meets the target. - Check-ins on unscheduled days. Decide whether they count. Under a weekly target they do. Under fixed weekdays,
scheduled_rateignores them, since it only looks at due days.
Choosing between the two
| Habit wording | Better fit | Why |
|---|---|---|
| "Exercise 3 times a week" | Weekly target | Any three days are fine |
| "Take out the bins on Tuesday" | Fixed weekdays | The day matters |
| "Meditate daily" | Daily streak | Every day is due |
Exercise
Today is Monday and you have not logged anything this week. Last week you logged 3 check-ins and your target is 3. What does weekly_streak report if the weeks before last week also met the target, and why?
Show answer
It counts the earlier weeks that met the target, starting from last week. The current week has 0 check-ins, which is below the target. The function steps back one week instead of treating the current week as a failure, so the streak is not lost while the week is still in progress.