Sub topic 4
Calculating the current streak
A streak is the number of consecutive days a habit was done, ending now. This chapter turns the set of check-in dates from the earlier chapters into that number.
This chapter assumes you can write Python functions and loops. It also assumes you know date and timedelta from the datetime module, and that each habit's check-ins are stored as a set of date objects.
What "current" means
The current streak is the run of consecutive logged days that ends at the present. Two questions decide the result:
- Where does the run end? It ends today if today is logged. If today is not logged, it can end yesterday.
- When does the run break? It breaks at the first calendar day that is missing from the set.
The second rule is why a set works well. Checking day in days is a constant-time lookup, so you can step backwards one day at a time and stop at the first gap.
The algorithm
- Choose a starting day. Use today if it is logged. Otherwise use yesterday.
- If the starting day is logged, add one to the streak and move back one day.
- Repeat until you reach a day that is not logged.
- Return the count.
The code
from datetime import date, timedelta
def current_streak(days, today=None):
today = today or date.today()
# If today is not logged yet, count back from yesterday so the streak is not lost mid-day.
day = today if today in days else today - timedelta(days=1)
streak = 0
while day in days:
streak += 1
day -= timedelta(days=1)
return streakThe function takes today as an optional argument. Production code calls it with no argument and gets the real date. Tests pass a fixed date, so the result does not depend on when you run them.
Running it on four cases
Add these lines below the function to see the behavior:
days = {date(2026, 11, 24), date(2026, 11, 25), date(2026, 11, 26)}
# Today not logged yet: the streak counts back from yesterday.
print(current_streak(days, today=date(2026, 11, 27))) # 3
# Today logged: the streak grows by one.
days.add(date(2026, 11, 27))
print(current_streak(days, today=date(2026, 11, 27))) # 4
# Two days with no check-in: the streak is broken.
print(current_streak(days, today=date(2026, 11, 29))) # 0
# A gap in the middle ends the walk at the gap.
gappy = {date(2026, 11, 20), date(2026, 11, 26)}
print(current_streak(gappy, today=date(2026, 11, 27))) # 1In the third case, the 28th and 29th are both missing. The start day is the 28th, which is not logged, so the loop never runs and the result is 0. In the fourth case, the walk stops at the 25th, so the older check-in on the 20th does not count.
Why yesterday is a valid starting point
Suppose you check in every morning and you open the tracker at 9:00. Today is not logged yet, but you have not failed. If the function started at today and found nothing, it would report 0 every morning until you checked in.
Starting from yesterday gives a "streak still alive" value. The streak only drops to 0 once a full calendar day has passed with no check-in.
Common errors
- Starting at today unconditionally. The streak reads 0 until you check in, which makes the tool feel broken in the morning.
- Counting the size of the set.
len(days)counts all check-ins ever made. It ignores gaps, so it is a total and not a streak. - Using
datetimeinstead ofdate. Adatetimeincludes a time of day, sodatetime.now()never equals a stored midnight value. Store and comparedateobjects only. - Looping forever. If the loop does not step back each time,
daynever changes andwhile day in daysnever ends. Keep theday -= timedelta(days=1)line inside the loop. - Walking past today. A check-in dated in the future, such as one typed with a wrong date, is never reached when you walk backwards from today. It is ignored instead of inflating the streak.
Cost
The loop runs once per day in the streak. It does not scan the whole set, so a 400-day streak takes about 400 lookups. That is fast enough that you never need to cache the result for a personal tracker.
Exercise
A habit has check-ins on 2026-03-01, 2026-03-02 and 2026-03-04. What does current_streak return when today is 2026-03-05, and what does it return when today is 2026-03-04?
Show answer
On 2026-03-05 it returns 0. Today is not logged, so the walk starts at 2026-03-04, which is logged, so the count becomes 1. The next day, 2026-03-03, is missing, so the walk stops. That gives 1, not 0. On 2026-03-04 it also returns 1, because today is logged and 2026-03-03 is missing. So the correct answers are 1 for both dates. The streak of 2026-03-01 and 2026-03-02 is not counted because the gap on the 3rd breaks the run.
Exercise
Why does current_streak accept a today argument instead of always calling date.today() inside?
Show answer
A fixed date makes the function deterministic, so tests give the same result on any day. Without it, you could not reliably check cases such as "today is not logged yet" or "two days missed".