Level 5 / Project 04 - Config Layer Priority¶
Home: README
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| — | This project | — | — | Flashcards | — | Browser |
Estimated time: 65 minutes
Focus¶
- defaults, env, and cli precedence
Why this project exists¶
This project gives you level-appropriate practice in a realistic operations context. Goal: run the baseline, alter behavior, break one assumption, recover safely, and explain the fix.
Run (copy/paste)¶
Use <repo-root> as the folder containing this repository's README.md.
cd <repo-root>/projects/level-5/04-config-layer-priority
python project.py --config data/config.json --output data/resolved_config.json
# Try with env override:
APP_LOG_LEVEL=DEBUG python project.py --config data/config.json --output data/resolved_config.json
pytest -q
Expected terminal output¶
Expected artifacts¶
data/resolved_config.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
--setflag that allows setting individual keys from the CLI (e.g.--set log_level=TRACE). - Add a
--dump-sourcesmode that prints which layer each final value came from. - Support nested config keys using dot notation (e.g.
database.host). - Re-run script and tests.
Break it (required) — Core¶
- Set an environment variable with a non-numeric value for a key that expects an integer (e.g.
APP_MAX_CONNECTIONS=abc). - Reference a config file that does not exist.
- Capture the first failing test or visible bad output.
Fix it (required) — Core¶
- Add type coercion with error handling that logs a warning and falls back to the default.
- Handle missing config file gracefully (use defaults only).
- Add tests for bad env var types and missing files.
- Re-run until output and tests are deterministic.
Checkpoint: All modifications done, tests still pass. Good time to review your changes.
Explain it (teach-back)¶
- Why is env > file > defaults the standard precedence order?
- What happens when
coerce_typesencounters a value it cannot convert? - How does
os.environ.getwith a prefix prevent collisions with other apps? - Where do you see layered configuration in production systems (12-factor apps, Kubernetes)?
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¶
- Classes and Objects
- Errors and Debugging
- Functions Explained
- The Terminal Deeper
- Quiz: Classes and Objects
Stuck? Ask AI¶
If you are stuck after trying for 20 minutes, use one of these prompts:
- "I am working on Config Layer Priority. I got this error: [paste error]. Can you explain what this error means without giving me the fix?"
- "I am trying to merge configuration from defaults, environment variables, and CLI arguments with proper precedence. Can you explain config layering with a simple example?"
- "Can you explain how
os.environ.get()works and when to use it vsos.environ[]?"
| ← Prev | Home | Next → |
|---|---|---|