Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
import json from pathlib import Path def load_config(path: str, defaults: dict) -> dict: cfg_path = Path(path) user_cfg = json.loads(cfg_path.read_text()) if cfg_path.exists() else {} return {**defaults, **user_cfg} # shallow merge defaults = {"debug": False, "port": 8000, "db": {"host": "localhost", "port": 5432}} config = load_config("config.json", defaults) print(config["port"])
Result
Open