Level 1 / Project 11 - Command Dispatcher¶
Home: README
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| Concept | This project | — | Quiz | Flashcards | — | Browser |
Estimated time: 35 minutes
Focus¶
- map commands to handler functions
Why this project exists¶
Map text commands (upper, lower, reverse, etc.) to handler functions using a dictionary dispatcher. You will learn the function-as-value pattern, where commands are looked up in a dict and called dynamically.
Run (copy/paste)¶
Use <repo-root> as the folder containing this repository's README.md.
cd <repo-root>/projects/level-1/11-command-dispatcher
python project.py --input data/sample_input.txt
pytest -q
Expected terminal output¶
=== Command Dispatcher ===
upper "hello world" => HELLO WORLD
lower "THIS SHOULD BE..." => this should be lowercase
reverse "Python is fun" => nuf si nohtyP
3 commands processed. Output written to data/output.json
5 passed
Expected artifacts¶
data/output.json- Passing tests
- Updated
notes.md
Checkpoint: Baseline code runs and all tests pass. Commit your work before continuing.
Alter it (required) — Extension¶
- Add a new command:
replace old new textthat replaces all occurrences ofoldwithnewin the text. - Add a
--listflag that prints all available commands and exits. - Re-run script and tests.
Break it (required) — Core¶
- Send a command with no arguments like just
upper(no text) -- doesdispatch()handle it? - Send an unknown command like
fly to the moon-- does the error dict include the command name? - Send an empty line -- does the dispatcher skip it or crash?
Fix it (required) — Core¶
- Ensure
dispatch()returns an error dict when no arguments are provided. - Include the attempted command name in the "Unknown command" error message.
- Add a test for the no-arguments case.
Checkpoint: All modifications done, tests still pass. Good time to review your changes.
Explain it (teach-back)¶
- Why does
COMMANDSmap command names to functions instead of using if/elif? - What does
dispatch()returning a dict (not raising an exception) mean for error handling? - Why does
list_commands()return sorted keys from theCOMMANDSdict? - Where would command dispatchers appear in real software (chatbots, CLI frameworks, REST API routers)?
Mastery check¶
You can move on when you can: - run baseline without docs, - explain one core function line-by-line, - break and recover in one session, - keep tests passing after your change.
Related Concepts¶
- Errors and Debugging
- Functions Explained
- How Imports Work
- The Terminal Deeper
- Quiz: Errors and Debugging
| ← Prev | Home | Next → |
|---|---|---|