A config loader
Why it matters Config loading is where small teams build frameworks. It feels like infrastructure, so it attracts infrastructure-sized code.
A small internal service reads its settings from a JSON file. There are four settings, with defaults:
DEFAULTS = {"port": 8000, "debug": False, "workers": 2, "log_level": "info"}Last month a typo ("prot": 9000) was silently ignored and the service started on the wrong port, so the team wants unknown keys and wrong types rejected. One engineer proposes a small schema system with field objects and validators, "so the next service can reuse it". Nobody has a next service planned.
Write load_config(text). text is the file's contents. Return a new dict: the defaults above with the file's values on top. Raise ValueError if the JSON is broken or not an object, if a key is not one of the four (name the key in the message), or if a value's type is not exactly the default's type (name the key; note true is not a whole number here). Rule: at most 170 syntax nodes (code_size).
Spotted a problem in this lesson? Report it · Ask about this lesson
This exercise opens soon
Finish the lesson before it and this one unlocks. The whole course is free, no account needed, and you can read this lesson right now.
This browser is blocking cookies for learn.floati.life, so lessons you finished here cannot open in order. Allow cookies for this site and reload.