Mental Model: TUI Word Processor in Python

This tool is just a text editor with richer operations and document stats. Don’t let the name “word processor” scare you.

Layered Mental Model

Split the app into layers:

  1. Document layer: line editing and file persistence.
  2. Analysis layer: derived stats like word/line counts.
  3. CLI layer: command parsing and user prompts.

Bugs get easier when you can place them in one layer.

Pythonic Document Core

A dataclass Document keeps state and behavior together.

  • lines is canonical text data.
  • edit methods mutate lines safely.
  • stats() derives data without mutating state.

This “data + methods” bundle is a strong beginner pattern.

Command Loop Rule

The loop should answer only: “Which method should run?”

  • parse command
  • validate line number
  • delegate to Document
  • report result

Keep the loop boring and your app stays maintainable.

Python-Specific Insight

Python makes text manipulation cheap and clear.

  • split() for word counting
  • list insert/pop for line edits
  • Path for file operations

You get a lot of power with basic built-ins.

Build Sequence

  1. append + show
  2. insert/edit/delete with bounds checks
  3. stats
  4. save/load + dirty guard

That order keeps the feedback loop tight.

How You Know It Clicked

  • You can explain where text lives.
  • You can add a command without editing half the file.
  • You can trace one command from input to state change.

That’s the mental model you want for larger apps later.