Mini Project: Config Manager
Exit

Mini Project: Config Manager

Build a config tool that reads, updates, and saves JSON settings

💻

Writing code and entering commands is only available on desktop. Open this page on a larger screen to complete this chapter.

What you will build

A config manager with three functions: load settings from a JSON file, update a value, and save the changes back to disk.

Example output

Current theme: light
Updated theme: dark
Updated max_retries: 5

Design

FunctionPurpose
load_config(filename)Read and return a JSON config file as a dict
update_config(config, key, value)Set config[key] to value
save_config(config, filename)Write the config dict back to the JSON file

Instructions

Build the config manager.

  1. Import json.
  2. Define a function named load_config that takes filename. Inside, open filename in read mode and return json.load(file).
  3. Define a function named update_config that takes config, key, and value. Inside, assign value to config[key].
  4. Define a function named save_config that takes config and filename. Inside, open filename in write mode and call json.dump(config, file, indent=2).
  5. Assign load_config("settings.json") to config. Call print(f"Current theme: {config['theme']}").
  6. Call update_config(config, "theme", "dark"). Call update_config(config, "max_retries", 5). Call save_config(config, "settings.json").
  7. Call print(f"Updated theme: {config['theme']}"). Call print(f"Updated max_retries: {config['max_retries']}").