Skip to content
Hands-on IT IT Drills

Day 1: Syntax in Brief – wc

Goal: practice strings, lists, dicts, loops and comprehensions with a wc of your own that counts lines, words, characters and the most frequent words · Duration: approx. 90 min · Prerequisite: Day 0

Briefing

  • Strings are immutable and Unicode: len("Prüfe") is 5, no matter how many bytes.
  • list, dict, set, tuple are the workhorses; collections.Counter does the counting for you.
  • Comprehensions ([x for x in ...]) replace most loops with append.
  • At the end you compare your tool with the macOS wc via diff – byte for byte.
file/stdin  ──► read_text() ──► count()      ──► "       5      31     209 datei"
                            └─► top_words()  ──► Counter.most_common(n)

Drill 1: Project and test text

cd ~/py-lab
uv init -q --package --name wc tag01-wc
cd tag01-wc
uv add -q --dev pytest ruff
mkdir -p lab
cat > lab/runbook.txt <<'EOF'
Runbook: Zertifikat erneuern

Prüfe das Zertifikat. Prüfe die Kette, dann prüfe den Ablauf.
Erneuere das Zertifikat rechtzeitig, sonst fällt der Dienst aus.
Der Dienst meldet Fehler; die Fehler landen im Log.
EOF
/usr/bin/wc lab/runbook.txt
/usr/bin/wc -lwm lab/runbook.txt

Expected:

       5      31     213 lab/runbook.txt
       5      31     209 lab/runbook.txt

Why two values? Without an option, wc counts bytes, with -m it counts characters. The four umlauts take two bytes each in UTF-8. Your tool will count characters, so later you compare with wc -lwm.

Tip: /usr/bin/wc with its full path: inside uv run, .venv/bin comes first in PATH, and that is where your own wc will soon live.

Save your progress? Sign up for free

Drill 2: Strings

uv run python - <<'EOF'
zeile = "  Prüfe das Zertifikat. Prüfe die Kette!  "
print(repr(zeile.strip()))
print(zeile.split())
print(zeile.lower().count("prüfe"))
wort = "Zertifikat."
print(wort.strip(".,;:!?"), wort[:4], wort[-1], len(wort))
print(f"{'Zeilen':<8}|{42:>8}|{3.14159:.2f}")
EOF

Expected:

'Prüfe das Zertifikat. Prüfe die Kette!'
['Prüfe', 'das', 'Zertifikat.', 'Prüfe', 'die', 'Kette!']
2
Zertifikat Zert . 11
Zeilen  |      42|3.14
  • split() without an argument splits on any whitespace and drops empty parts – exactly what wc means by “word”.
  • strip(".,;:!?") removes each of these characters at both ends, not the string as a whole.
  • Slicing [:4], [-1] works the same for strings, lists and tuples.
  • In an f-string, :<8 / :>8 controls the alignment, :.2f the decimal places.

Save your progress? Sign up for free

Drill 3: Lists and loops

uv run python - <<'EOF'
from pathlib import Path

text = Path("lab/runbook.txt").read_text(encoding="utf-8")
lines = text.splitlines()
print(type(lines), len(lines))
print(lines[0], "|", lines[-1])
words = 0
for nr, line in enumerate(lines, start=1):
    n = len(line.split())
    words += n
    print(f"{nr:>2}: {n:>2} Wörter")
print("Summe:", words, "Zeichen:", len(text), "Newlines:", text.count("\n"))
EOF

Expected:

<class 'list'> 5
Runbook: Zertifikat erneuern | Der Dienst meldet Fehler; die Fehler landen im Log.
 1:  3 Wörter
 2:  0 Wörter
 3: 10 Wörter
 4:  9 Wörter
 5:  9 Wörter
Summe: 31 Zeichen: 209 Newlines: 5

Why enumerate? It returns index and value at the same time; a C-style for i in range(len(lines)) is a style mistake in Python. Always specify encoding="utf-8" explicitly – otherwise the default depends on the platform.

Save your progress? Sign up for free

Drill 4: Dicts and Counter

uv run python - <<'EOF'
from collections import Counter
from pathlib import Path

words = Path("lab/runbook.txt").read_text(encoding="utf-8").lower().split()
counts: dict[str, int] = {}
for w in words:
    w = w.strip(".,;:!?")
    counts[w] = counts.get(w, 0) + 1
print(counts["prüfe"], counts.get("vault", 0))
top = sorted(counts.items(), key=lambda kv: kv[1], reverse=True)[:3]
print(top)
print(Counter(w.strip(".,;:!?") for w in words).most_common(3))
EOF

Expected:

3 0
[('zertifikat', 3), ('prüfe', 3), ('das', 2)]
[('zertifikat', 3), ('prüfe', 3), ('das', 2)]
  • counts["vault"] would abort with a KeyError; get(key, 0) returns the default.
  • items() returns (key, value) tuples; sorted(..., key=...) sorts by the second element.
  • Counter does counting and sorting in one line. Dicts keep insertion order, which is why the result is stable.

Save your progress? Sign up for free

Drill 5: Comprehensions

uv run python - <<'EOF'
from pathlib import Path

STOP = {"der", "die", "das", "dann", "sonst", "im"}
raw = Path("lab/runbook.txt").read_text(encoding="utf-8").lower().split()
words = [w.strip(".,;:!?") for w in raw]
print(words[:6])
meaningful = [w for w in words if len(w) > 2 and w not in STOP]
print(meaningful[:6])
unique = {w for w in meaningful}
print(len(words), len(meaningful), len(unique))
lengths = {w: len(w) for w in sorted(unique) if len(w) > 9}
print(lengths)
print(sum(1 for w in words if w.startswith("z")))
EOF

Expected:

['runbook', 'zertifikat', 'erneuern', 'prüfe', 'das', 'zertifikat']
['runbook', 'zertifikat', 'erneuern', 'prüfe', 'zertifikat', 'prüfe']
31 22 16
{'rechtzeitig': 11, 'zertifikat': 10}
3
Form Result Example
[...] List [w.lower() for w in words]
{...} Set {w for w in words}
{k: v ...} Dict {w: len(w) for w in words}
(...) Generator (lazy) sum(1 for w in words if ...)

Why a set for stop words? w not in STOP is O(1) for a set, O(n) for a list. The generator in sum(...) builds no intermediate list – with large files that saves memory.

Save your progress? Sign up for free

Drill 6: The tool

Now everything goes into the package. argparse provides options, help and error messages; from Day 3 on, typer takes over.

cat > src/wc/__init__.py <<'EOF'
"""wc – Zeilen, Wörter und Zeichen zählen, dazu die häufigsten Wörter."""

import argparse
import sys
from collections import Counter
from pathlib import Path

PUNCT = ".,;:!?()[]\"'"
STOPWORDS = {"der", "die", "das", "und", "oder", "dann", "sonst", "im", "the", "and"}


def count(text: str) -> tuple[int, int, int]:
    """Zeilen (Anzahl Newlines wie bei wc), Wörter, Zeichen."""
    return text.count("\n"), len(text.split()), len(text)


def top_words(text: str, n: int = 5) -> list[tuple[str, int]]:
    """Die n häufigsten Wörter, ohne Satzzeichen, Stoppwörter und Kurzwörter."""
    words = [w.strip(PUNCT) for w in text.lower().split()]
    kept = [w for w in words if len(w) > 2 and w not in STOPWORDS]
    return Counter(kept).most_common(n)


def fmt(values: tuple[int, ...], show: list[bool], name: str = "") -> str:
    """Spalten wie BSD-wc: je 8 Zeichen rechtsbündig, dann der Name."""
    cols = "".join(f"{v:>8}" for v, on in zip(values, show, strict=True) if on)
    return f"{cols} {name}".rstrip()


def main(argv: list[str] | None = None) -> int:
    parser = argparse.ArgumentParser(prog="wc", description="Zeilen/Wörter/Zeichen")
    parser.add_argument("files", nargs="*", type=Path, help="Dateien (ohne: stdin)")
    parser.add_argument("-l", "--lines", action="store_true", help="Zeilen zählen")
    parser.add_argument("-w", "--words", action="store_true", help="Wörter zählen")
    parser.add_argument("-m", "--chars", action="store_true", help="Zeichen zählen")
    parser.add_argument("--top", type=int, default=0, metavar="N", help="Top-N-Wörter")
    args = parser.parse_args(argv)

    show = [args.lines, args.words, args.chars]
    if not any(show):
        show = [True, True, True]

    texts: list[tuple[str, str]] = []
    if not args.files:
        texts.append(("", sys.stdin.read()))
    for path in args.files:
        try:
            texts.append((str(path), path.read_text(encoding="utf-8")))
        except OSError as err:
            print(f"wc: {path}: {err.strerror}", file=sys.stderr)
            return 1

    results = [count(text) for _, text in texts]
    for (name, _), values in zip(texts, results, strict=True):
        print(fmt(values, show, name))
    if len(results) > 1:
        totals = tuple(sum(col) for col in zip(*results, strict=True))
        print(fmt(totals, show, "total"))

    if args.top:
        print(f"--- Top {args.top} ---")
        for word, n in top_words(" ".join(t for _, t in texts), args.top):
            print(f"{word:<16}{n:>4}")
    return 0
EOF
cat > src/wc/__main__.py <<'EOF'
from wc import main

raise SystemExit(main())
EOF
uv run wc lab/runbook.txt
uv run wc -lw lab/runbook.txt src/wc/__main__.py
printf 'a b\nc' | uv run wc

Expected:

       5      31     209 lab/runbook.txt
       5      31 lab/runbook.txt
       3       6 src/wc/__main__.py
       8      37 total
       1       3       5

The most important tricks:

  • return a, b, c returns a tuple; for (name, _), values in zip(...) unpacks it again, nested. _ means “I don’t need this”.
  • zip(*results) transposes the list of rows into columns – which makes the totals line a one-liner. strict=True reports inputs of unequal length instead of silently truncating.
  • main(argv=None) returns an exit code. The console script calls sys.exit(main()), tests call main([...]) directly.
  • Error messages go to stderr (file=sys.stderr), results to stdout.

Save your progress? Sign up for free

Drill 7: Tests, ruff and the comparison with the original

From today on, every project gets a small ruff configuration: line length (E501), typical bugs (B), modern syntax (UP) and simplifications (SIM).

cat >> pyproject.toml <<'EOF'

[tool.ruff.lint]
extend-select = ["E501", "B", "UP", "SIM"]
EOF
mkdir -p tests
cat > tests/test_wc.py <<'EOF'
from wc import count, fmt, main, top_words


def test_count():
    assert count("a b\nc") == (1, 3, 5)
    assert count("") == (0, 0, 0)


def test_umlaute_sind_ein_zeichen():
    assert count("Prüfe\n") == (1, 1, 6)


def test_top_words_ignoriert_stoppwoerter():
    text = "Das Log. Das LOG! der Dienst"
    assert top_words(text, 2) == [("log", 2), ("dienst", 1)]


def test_fmt_nur_zeilen():
    assert fmt((5, 31, 209), [True, False, False], "x.txt") == "       5 x.txt"


def test_main_fehlende_datei(capsys):
    assert main(["gibt-es-nicht.txt"]) == 1
    assert "gibt-es-nicht.txt" in capsys.readouterr().err
EOF
uv run pytest -q
uv run ruff check && uv run ruff format --check

Expected:

.....                                                                    [100%]
5 passed in 0.01s
All checks passed!
4 files already formatted

capsys is a pytest fixture that captures stdout/stderr (more on Day 4). Now the honest test – the same output as the original:

for f in lab/runbook.txt pyproject.toml src/wc/__init__.py; do
  diff <(uv run wc "$f") <(/usr/bin/wc -lwm "$f") && echo "gleich: $f"
done
diff <(uv run wc -lw lab/*.txt pyproject.toml) \
     <(/usr/bin/wc -lw lab/*.txt pyproject.toml) && echo "gleich: mehrere"
diff <(cat src/wc/*.py | uv run wc) \
     <(cat src/wc/*.py | /usr/bin/wc -lwm) && echo "gleich: stdin"

Expected:

gleich: lab/runbook.txt
gleich: pyproject.toml
gleich: src/wc/__init__.py
gleich: mehrere
gleich: stdin

Why <(...)? Process substitution turns the output of a command into a file that diff can read. No diff output means: identical.

Save your progress? Sign up for free

Drill 8: The tool is done

uv run wc --top 3 lab/runbook.txt
uv run wc gibt-es-nicht.txt; echo "exit=$?"
uv run wc -x; echo "exit=$?"
uv run wc -h

Expected:

       5      31     209 lab/runbook.txt
--- Top 3 ---
zertifikat         3
prüfe              3
dienst             2
wc: gibt-es-nicht.txt: No such file or directory
exit=1
usage: wc [-h] [-l] [-w] [-m] [--top N] [files ...]
wc: error: unrecognized arguments: -x
exit=2
usage: wc [-h] [-l] [-w] [-m] [--top N] [files ...]

Zeilen/Wörter/Zeichen

positional arguments:
  files        Dateien (ohne: stdin)

options:
  -h, --help   show this help message and exit
  -l, --lines  Zeilen zählen
  -w, --words  Wörter zählen
  -m, --chars  Zeichen zählen
  --top N      Top-N-Wörter

Exit codes by Unix convention: 0 ok, 1 runtime error, 2 wrong usage (argparse handles that). This makes wc usable in Bash pipelines and CI.

Save your progress? Sign up for free

Checkpoint

#!/usr/bin/env bash
check() { if eval "$2" >/dev/null 2>&1; then echo "✔ $1"; else echo "✘ $1"; fi; }
cd ~/py-lab/tag01-wc
check "wc zählt wie das Original" \
  "diff <(uv run wc lab/runbook.txt) <(/usr/bin/wc -lwm lab/runbook.txt)"
check "stdin funktioniert" "printf 'a b\nc' | uv run wc | grep -q '1       3       5'"
check "Top-Wort ist zertifikat" \
  "uv run wc --top 1 lab/runbook.txt | grep -q '^zertifikat'"
check "fehlende Datei gibt Exit 1" "! uv run wc gibt-es-nicht.txt"
check "Tests grün" "uv run pytest -q"
check "ruff sauber" "uv run ruff check && uv run ruff format --check"

Expected:

✔ wc zählt wie das Original
✔ stdin funktioniert
✔ Top-Wort ist zertifikat
✔ fehlende Datei gibt Exit 1
✔ Tests grün
✔ ruff sauber

Challenge

Add --longest: for every input, wc additionally prints the longest line along with its length.

Hint: Like sorted(), max() takes a key= argument, and default="" protects against empty files.

Solution

In src/wc/__init__.py after the --top option:

    parser.add_argument("--longest", action="store_true", help="längste Zeile")

and before the if args.top: block:

    if args.longest:
        for _, text in texts:
            line = max(text.splitlines(), key=len, default="")
            print(f"{len(line):>8} {line}")
uv run wc --longest -l lab/runbook.txt src/wc/__main__.py
uv run pytest -q && uv run ruff check

Expected:

       5 lab/runbook.txt
       3 src/wc/__main__.py
       8 total
      64 Erneuere das Zertifikat rechtzeitig, sonst fällt der Dienst aus.
      24 raise SystemExit(main())
5 passed in 0.01s
All checks passed!

Save your progress? Sign up for free

Enterprise Note

  • read_text() loads the whole file into memory. For logs in the GB range, iterate line by line (for line in f:) – more on Day 14.
  • In mixed environments, encodings are not always UTF-8; errors="replace" prevents aborts on broken bytes, but distorts the counts.
  • Never name your own tools like system commands if they get installed globally; a prefix (pywc, acme-wc) avoids mix-ups in PATH.
  • Output formats that other scripts parse are an interface: keep them stable or offer --json.