Sub topic 6
Measuring completion rate over a window
A streak tells you how many days in a row you have shown up. It resets to zero after one missed day, so it says little about how consistent you were before the miss. The completion rate fills that gap. It is the share of days in a fixed period on which you did the habit.
This chapter assumes you know Python sets and datetime.date/timedelta, and that you have the check-in log from earlier chapters: a dictionary mapping each habit name to a set of dates.
The idea
Pick a window, such as the last 30 days, ending on a given date. Count the logged days inside it and divide by the window length.
The code from the plan:
from datetime import timedelta
def completion_rate(days, end, window=30):
start = end - timedelta(days=window - 1)
done = sum(1 for d in days if start <= d <= end)
return done / windowThe function takes the set of dates for one habit, so you call it as completion_rate(log["read"], date.today()).
Why `window - 1`
The window includes the end date itself. A 30-day window ending on 27 November starts on 29 October, which is 29 days earlier. If you subtracted window days instead, you would cover 31 calendar days and divide by 30. A perfect record could then score above 1.0.
The check start <= d <= end also discards dates outside the window. Old check-ins and any accidental future dates cannot inflate the result.
A worked example
This script is complete and runs as shown. It builds a log that skips every third day and measures it over two windows.
from datetime import date, timedelta
def completion_rate(days, end, window=30):
start = end - timedelta(days=window - 1)
done = sum(1 for d in days if start <= d <= end)
return done / window
end = date(2026, 11, 27)
days = {end - timedelta(days=i) for i in range(30) if i % 3 != 0}
print(f"30 days: {completion_rate(days, end):.0%}")
print(f" 7 days: {completion_rate(days, end, window=7):.0%}")The output is:
30 days: 67%
7 days: 57%The days at offsets 0, 3, 6 and so on are skipped. That leaves 20 logged days out of 30, and 4 out of 7 in the shorter window. Comparing the two windows shows a trend. If the 7-day rate is below the 30-day rate, you are slipping lately. If it is above, you are recovering.
| Window | Logged days | Rate |
|---|---|---|
| 30 days | 20 | 67% |
| 7 days | 4 | 57% |
Choosing a window
A short window reacts quickly but swings widely, because one missed day in 7 costs 14 points. A long window is stable but hides recent changes. Many people track a 7-day and a 30-day rate side by side.
Common errors
- Off-by-one windows. Using
timedelta(days=window)makes the window one day too long, as described above. - Dividing by the wrong length. If you divide by
len(days)you get 100% for any habit, because every logged day is a logged day. The denominator must be the window length, not the number of check-ins. - Counting days before the habit existed. A habit started 10 days ago and done every day scores only 33% over a 30-day window. The 20 days before you started count as misses. If that is misleading, clamp the window to the first day you tracked the habit.
- Using
datetimeinstead ofdate. Adatetimewith a time of day never equals a plaindate, and comparing the two raisesTypeError. Storedateobjects only. - A zero or negative window.
window=0raisesZeroDivisionError. Validate the argument if it comes from user input.
Here is a version that handles the third error:
from datetime import date, timedelta
def completion_rate_since(days, end, first_day, window=30):
start = max(end - timedelta(days=window - 1), first_day)
length = (end - start).days + 1
if length <= 0:
return 0.0
done = sum(1 for d in days if start <= d <= end)
return done / length
end = date(2026, 11, 27)
days = {end - timedelta(days=i) for i in range(10)}
print(f"{completion_rate_since(days, end, end - timedelta(days=9)):.0%}") # 100%The max call picks whichever start date is later, the window start or the day you began. The length is then the number of days actually available. The guard returns 0.0 if the first day lies after the end date.
Using the number
A completion rate is only useful if you compare it with a target. For a daily habit, 80% over 30 days means about six misses, which is often a healthy level. Chapter 10 uses this rate and the longest streak to decide whether to keep, shrink or reschedule a habit.
Exercise
A habit has 15 logged days inside a 30-day window ending today, and 6 logged days inside the 7-day window ending today. What are the two completion rates, and what do they suggest about the trend?
Show answer
The 30-day rate is 15/30 = 50%. The 7-day rate is 6/7, about 86%. The recent week is much stronger than the month overall, so the habit is picking up.
Exercise
A learner changes completion_rate to use start = end - timedelta(days=window). A habit is done every day for 40 days. What does the function return for window=30, and why is it wrong?
Show answer
It returns 31/30, about 1.03. The window now spans 31 calendar days, so 31 logged days fall inside it, but the divisor is still 30. A rate above 100% is impossible, so the start date must be end - timedelta(days=window - 1).