Checkpoint: The Standard Library

Published by Brayan K

A checkpoint is a hands-on review where you combine several standard-library modules — functools, enums, namedtuple, hashlib, and more — into one small working tool.

Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

Build a duplicate-content report with an explicit contract

A small publishing tool receives labelled text drafts. It must report each draft’s UTF-8 byte length and whether the same bytes appeared earlier. A duplicate is about content, not a repeated label. The first occurrence keeps the status NEW; later occurrences are REPEAT. Preserve input order so the report explains which occurrence was first.

This checkpoint combines tools you already met: namedtuple & NamedTuple for a compact result record, Enums (the enum module) for status choices, and Hashing with hashlib for byte fingerprints. Their lessons own the individual APIs. Here, concentrate on how their contracts fit together.

from enum import Enum
from hashlib import sha256
from typing import NamedTuple

class Status(Enum):
    NEW = "new"
    REPEAT = "repeat"

class Report(NamedTuple):
    label: str
    byte_count: int
    status: Status

def report(drafts):
    seen = set()
    rows = []
    for label, text in drafts:
        data = text.encode("utf-8")
        digest = sha256(data).hexdigest()
        status = Status.REPEAT if digest in seen else Status.NEW
        seen.add(digest)
        rows.append(Report(label, len(data), status))
    return rows

drafts = [("A", "café"), ("B", "cafe"), ("C", "café")]
for row in report(drafts):
    print(row.label, row.byte_count, row.status.value)

# ✅ Expected output:
# A 5 new
# B 4 new
# C 5 repeat

Encoding happens before both hashing and counting, so both operations describe the same representation. The set belongs inside report: each report begins with no prior drafts. Testing membership before adding the digest is essential; adding first would label every row as a repeat.

A digest is a practical fingerprint, not a mathematical proof of byte equality and not proof of who wrote a draft. If collisions must be handled, retain and compare the bytes within each digest group. Authentication needs a different contract. This exercise does not use a checksum as a password hash or a signature.

Decide what counts as the same content before normalizing it

from hashlib import sha256

first = "café"
second = "cafe"
third = "café "

print(len(first), len(first.encode("utf-8")))
print(sha256(first.encode("utf-8")).digest() == sha256(second.encode("utf-8")).digest())
print(sha256(first.encode("utf-8")).digest() == sha256(third.encode("utf-8")).digest())

# ✅ Expected output:
# 4 5
# False
# False

Stripping whitespace would change this tool’s definition of a duplicate. Do not insert strip() merely to make two test fixtures match. If a product later wants comparison that ignores trailing spaces, specify that policy and test it separately. Similarly, character count is not byte count when text contains non-ASCII characters.

from enum import Enum

class Status(Enum):
    NEW = "new"
    REPEAT = "repeat"

Status("duplicate")

# ✅ Expected error:
# ValueError: 'duplicate' is not a valid Status

Enum construction by value rejects unknown values. The solution keeps enum members inside the result and uses .value only when rendering the report, avoiding a mixture of strings and members in the core logic.

Keep cache lifetime separate from report lifetime

Hashing the same immutable bytes can reuse a cached result. Whether those bytes have occurred in this report cannot be cached globally: it depends on the report’s history. Cache the pure calculation, and keep the seen-set local. The cache policy itself is taught in functools: lru_cache, partial, reduce.

from functools import lru_cache
from hashlib import sha256

calls = []

@lru_cache(maxsize=32)
def fingerprint(data):
    calls.append("computed")
    return sha256(data).hexdigest()

def statuses(texts):
    seen = set()
    result = []
    for text in texts:
        digest = fingerprint(text.encode("utf-8"))
        result.append("repeat" if digest in seen else "new")
        seen.add(digest)
    return result

print(statuses(["tea", "tea"]))
print(statuses(["tea"]))
print(calls)

# ✅ Expected output:
# ['new', 'repeat']
# ['new']
# ['computed']

The cache may outlive one call, while the seen-set must not. A module-level seen-set or a mutable default argument would make the second report depend on the first. The cached function takes bytes, which are immutable and hashable; a list of drafts is neither an appropriate cache key nor a stable unit of this computation.

Fill the two decisions that define a repeat

Complete the encoding and membership test. The report must compare fingerprints of encoded content, then add each fingerprint after making its decision. The labels remain independent of content.

from hashlib import sha256

drafts = [("one", "tea"), ("two", "coffee"), ("three", "tea")]
seen = set()
for label, text in drafts:
    data = text.encode(___)
    digest = sha256(data).hexdigest()
    repeated = digest ___ seen
    seen.add(digest)
    print(label, repeated)

# 👉 replace ___ with "utf-8"
# 👉 replace ___ with in

# ✅ Expected output:
# one False
# two False
# three True

Test behavior that would expose a plausible bad implementation

A useful checkpoint test is chosen because a specific mistake would make it fail. Include identical text under different labels, different text under one repeated label, non-ASCII bytes, empty input, and a second report. Merely checking that a report has the right number of rows would miss incorrect duplicate detection.

import unittest
from hashlib import sha256

def repeats(drafts):
    seen = set()
    flags = []
    for label, text in drafts:
        digest = sha256(text.encode("utf-8")).hexdigest()
        flags.append(digest in seen)
        seen.add(digest)
    return flags

class ReportTests(unittest.TestCase):
    def test_same_content_different_labels(self):
        self.assertEqual(repeats([("A", "tea"), ("B", "tea")]), [False, True])

    def test_same_label_different_content(self):
        self.assertEqual(repeats([("A", "tea"), ("A", "coffee")]), [False, False])

    def test_new_report_resets_history(self):
        repeats([("A", "tea")])
        self.assertEqual(repeats([("B", "tea")]), [False])

    def test_empty(self):
        self.assertEqual(repeats([]), [])

result = unittest.TestResult()
unittest.defaultTestLoader.loadTestsFromTestCase(ReportTests).run(result)
print(result.testsRun, result.wasSuccessful())

# ✅ Expected output:
# 4 True

These assertions cover repeat detection. The independent task adds a separate byte-count assertion. Testing with unittest owns test discovery and assertion behavior; this checkpoint uses those tools to challenge its own design.

Extend the report without mixing computation and display

Build the complete report again, then add summary(rows), returning the number of new drafts and total bytes across all rows, including repeats. Use the three drafts A/café, B/tea, and C/café. Print the labelled rows and the summary. Add tests for the byte count of café, a repeat under a different label, and fresh history on a second call. Keep formatting outside the report function so tests can inspect structured records.

from enum import Enum
from hashlib import sha256
from typing import NamedTuple
import unittest

class Status(Enum):
    NEW = "new"
    REPEAT = "repeat"

class Report(NamedTuple):
    label: str
    byte_count: int
    status: Status

def report(drafts):
    seen = set()
    rows = []
    for label, text in drafts:
        data = text.encode("utf-8")
        digest = sha256(data).hexdigest()
        state = Status.REPEAT if digest in seen else Status.NEW
        seen.add(digest)
        rows.append(Report(label, len(data), state))
    return rows

def summary(rows):
    return sum(row.status is Status.NEW for row in rows), sum(row.byte_count for row in rows)

class ReportTests(unittest.TestCase):
    def test_utf8_bytes(self):
        self.assertEqual(report([("A", "café")])[0].byte_count, 5)

    def test_repeat(self):
        rows = report([("A", "tea"), ("B", "tea")])
        self.assertEqual([row.status for row in rows], [Status.NEW, Status.REPEAT])
        self.assertEqual(summary(rows), (1, 6))

    def test_fresh_history(self):
        report([("A", "tea")])
        self.assertIs(report([("B", "tea")])[0].status, Status.NEW)

rows = report([("A", "café"), ("B", "tea"), ("C", "café")])
for row in rows:
    print(row.label, row.byte_count, row.status.value)
print("summary:", summary(rows))
result = unittest.TestResult()
unittest.defaultTestLoader.loadTestsFromTestCase(ReportTests).run(result)
print("tests:", result.testsRun, result.wasSuccessful())

# ✅ Expected output:
# A 5 new
# B 3 new
# C 5 repeat
# summary: (2, 13)
# tests: 3 True

A follow-on project could read real files or accept command-line arguments. Keep that outside this exercise’s contract: Modern File Paths with pathlib owns file paths, and Building Command-Line Tools with argparse & Typer owns argument parsing. A focused tool with explicit assumptions is easier to test than a checkpoint that quietly adds networking, credentials, and platform-specific paths.

Practice quiz

Two different labels contain identical encoded text. Under this report’s contract, how should the second row be classified?

  • New
  • Invalid label
  • Unknown
  • Repeat

Answer: Repeat. Content, not labels, determines repeats, and the first occurrence is new.

What does this program print? text = "café" print(len(text), len(text.encode("utf-8")))

  • 4 4
  • 4 5
  • 5 5
  • 5 4

Answer: 4 5. The accented character occupies more than one UTF-8 byte.

Where should seen = set() live if every report has independent history?

  • As a mutable default argument
  • At module scope beside the cache
  • Inside the report function
  • Inside the loop for each row

Answer: Inside the report function. Per-call state resets between reports but persists through all rows in one report.

Which operation is safe to cache by its immutable bytes argument?

  • Compute the SHA-256 fingerprint
  • Decide whether this report saw it earlier
  • Append a row to the current report
  • Print the next report line

Answer: Compute the SHA-256 fingerprint. A fingerprint is determined by its bytes. History, mutations and printing depend on context or have side effects.

What does this program print? from hashlib import sha256 a = "tea".encode("utf-8") b = "tea ".encode("utf-8") print(sha256(a).digest() == sha256(b).digest())

  • True
  • None
  • False
  • TypeError

Answer: False. Trailing whitespace is part of the input bytes under this contract.

Which test catches a detector that mistakenly compares labels rather than content?

  • One draft with a nonempty label
  • Two labels with different text
  • An empty list of drafts
  • Different labels with identical text

Answer: Different labels with identical text. Different labels must still produce a repeat when the content matches.

Why return NamedTuple rows instead of printing from inside report?

  • Tests and other callers can inspect structured results
  • NamedTuple validates every value at runtime
  • It makes digest collisions impossible
  • It automatically sorts the report

Answer: Tests and other callers can inspect structured results. Keeping display separate lets callers choose formatting and lets tests assert individual fields.

What does matching an untrusted draft to a supplied SHA-256 digest establish about its author?

  • The named author created it
  • It does not authenticate the author
  • The draft cannot contain malware
  • The content is legally reusable

Answer: It does not authenticate the author. An attacker can replace both content and an untrusted checksum. A digest alone supplies no authorship proof.

Continue this course

Related lessons