1
Step 4 Trainer
Tirsvad edited this page 2026-11-20 06:44:39 +01:00

English · Dansk · Bahasa Malaysia

Step 4: the trainer

The program now has six sample texts. One is chosen at random at the start, and a chooser lets you pick another until you type the first character. When a test ends, the result goes into a list of the ten best results, best first. The list is kept in a file in your home folder, so it is still there the next time you start the program.

The chooser before a test starts

A finished test with the high score list

Run it

python -m typing_speed_test

What step 4 taught

  • Persistence behind a port. SaveHighScore talks to the HighScoreStore port. The JSON file is an adapter in the infrastructure layer, so the rules are tested with a fake store and the file code is tested with a temporary folder.
  • Tolerant file reading. A missing, unreadable or corrupt file gives an empty list and no error, because a lost high score list is a small harm and a crash at start is a large one.
  • Ranking is a domain rule. HighScoreList.add keeps the ten best by words per minute, then accuracy, then the earlier date. A new list is returned each time.
  • Injecting randomness. The text source takes a seeded random.Random in tests, so the random start can be tested without luck.

Files of this step: high_score_list.py, save_high_score.py, json_high_score_store.py, built_in_texts.py, typing_view_model.py.

Home · Setup