-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path05_json_config_manager.py
More file actions
35 lines (28 loc) · 884 Bytes
/
Copy path05_json_config_manager.py
File metadata and controls
35 lines (28 loc) · 884 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"""
Day 10: JSON Config Manager
Concept: Loading and saving configuration dictionaries using the 'json' module.
"""
import json
config_file = "settings.json"
# 1. Dictionary to be saved as JSON
app_settings = {
"app_name": "Python Daily Tracker",
"version": "1.0.0",
"theme": "Dark",
"notifications": True,
"user_prefs": {
"auto_save": True,
"backup_interval": 3600
}
}
# 2. Writing JSON to a file (Serialization)
with open(config_file, "w") as f:
json.dump(app_settings, f, indent=4)
print(f"Configuration saved to {config_file}")
# 3. Reading JSON from a file (Deserialization)
with open(config_file, "r") as f:
loaded_config = json.load(f)
print("\nLoaded Settings:")
print(f"App Name: {loaded_config['app_name']}")
print(f"Theme: {loaded_config.get('theme')}")
print(f"Auto-Save: {loaded_config['user_prefs']['auto_save']}")