Sub topic 1
What makes a habit trackable
Prerequisites
This chapter assumes you know basic Python: functions, dictionaries, sets and loops. It also assumes you have used date and timedelta from the datetime module and can read and write text files. The chapter itself needs little code. Its job is to settle what the tracker will record, because every later chapter depends on that decision.
Why the definition comes first
A habit tracker is only as good as the question it asks. If the question is vague, you cannot tell whether a day counts. The streak, the longest streak and the completion rate are all calculated from check-ins, and a check-in is only meaningful if the habit behind it is defined clearly.
Compare two goals:
- "Be healthier."
- "Walk for at least 20 minutes."
You cannot answer the first with yes or no. You can answer the second at the end of the day. The tracker in this tutorial stores only that yes or no answer, so your habit must be written in a form that has one.
The yes/no test
A habit is trackable when you can answer "did I do it today?" with yes or no and no judgment call. Check a candidate against these four properties:
- Specific. It names one action, such as "write one page" and not "work on the book".
- Binary. The action either happened or did not. There is no partial credit.
- Bounded in time. It is tied to a calendar day, so each day has exactly one answer.
- Within your control. You decide whether it happens. "Lose 2 kg" depends on many things. "Log my meals" does not.
Rewriting vague goals
Most goals need a small rewrite before they can be tracked. The table shows some typical ones.
| Vague goal | Trackable habit | Why it works |
|---|---|---|
| Exercise more | Do 10 minutes of stretching | One action, clear threshold |
| Read more | Read at least one page | Small enough to do every day |
| Eat well | Eat one serving of vegetables at lunch | Specific and observable |
| Sleep better | Be in bed by 23:00 | A time you can check |
Notice that the trackable versions are small. A small threshold is a design choice. If the minimum is easy, a bad day can still count, and the data then reflects whether you showed up and not how energetic you felt.
One check-in per habit per day
The tracker records at most one check-in for each habit on each calendar day. Doing the habit three times in one day still counts as one check-in. This limit keeps the data model small: each habit becomes a set of dates, and a date is either in the set or it is not.
Here is a first sketch of that idea. You will build it properly in the next two chapters.
from datetime import date
log = {} # habit name -> set of dates on which it was done
def check_in(habit, day=None):
day = day or date.today()
log.setdefault(habit, set()).add(day)
check_in("read")
check_in("read") # a duplicate check-in on the same day changes nothing
print(len(log["read"])) # 1A set ignores a value that is already present, so the second call changes nothing. The yes/no definition and the set of dates match each other: both have exactly one answer per day.
How the definition shapes the plan
Each property of a trackable habit supports a later part of the tracker.
If the habit is clear, each check-in is reliable. Reliable check-ins give a set of dates you can trust. Metrics computed from that set then describe your behavior accurately.
Common errors
- Bundling several actions. "Exercise, read and meditate" is three habits. Track each one under its own name so a missed day for one does not hide the others.
- Defining the habit by outcome. Outcomes such as weight or sales figures depend on factors outside your control. Track the action that leads to them.
- Changing the rule mid-way. If you change what counts, earlier check-ins mean something different. Start a new habit name when the definition changes.
- Checking in on a feeling. "I basically did it" is a judgment call. Count it only if it meets the written threshold.
Choose your habit
Before the next chapter, pick one habit and write it as one sentence that passes the yes/no test. Use it as the running example. A good template is: "I will [action] for at least [amount] each day."
Exercise
Which of these is trackable with a yes/no check-in, and how would you fix the other? (a) "Get fit" (b) "Do 15 push-ups"
Show answer
(b) is trackable. It is one specific action with a clear threshold, and you can answer it at the end of the day. (a) is not, because "fit" has no yes/no answer. You can rewrite it as an action such as "Do 20 minutes of exercise", which is specific, binary, tied to a day and within your control.
Exercise
You do your habit three times on Tuesday. How many check-ins does the tracker record for Tuesday, and which Python feature enforces this?
Show answer
One. Each habit's check-ins are stored in a set of dates, and adding a date that is already in a set does nothing.