Skip to content
Exercise · Modules, files and generators · lesson 12 of 12

Exercise: settings from JSON

Write a function load_settings(text). It parses the JSON object in text and returns a new dict: the settings from DEFAULTS overwritten by those from text. The dict DEFAULTS is already in the stub.

  • load_settings('{"theme": "dark"}') → {'theme': 'dark', 'font_size': 14, 'autosave': True}
  • load_settings('{"lang": "ru"}') → {'theme': 'light', 'font_size': 14, 'autosave': True, 'lang': 'ru'}
  • load_settings("{theme: dark}") raises ValueError

Constraints: text is either a JSON object or a string that is not valid JSON; on invalid JSON the function raises ValueError. Keys missing from DEFAULTS go into the result. DEFAULTS itself does not change between calls.

Do not add extra imports unless the task says so explicitly.

load_settings.py — python
python · 8 lines
Ctrl+Enter run · Esc leave editor

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way