forked from msgflux/msgspec-ext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_basic_usage.py
More file actions
67 lines (51 loc) · 1.67 KB
/
01_basic_usage.py
File metadata and controls
67 lines (51 loc) · 1.67 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
Basic usage of msgspec-ext for settings management.
This example shows the simplest use case: defining settings with defaults
and loading from environment variables.
"""
import os
from msgspec_ext import BaseSettings
class AppSettings(BaseSettings):
"""Application settings with sensible defaults."""
app_name: str = "my-app"
debug: bool = False
port: int = 8000
host: str = "0.0.0.0"
def main():
print("=" * 60)
print("Example 1: Basic Usage")
print("=" * 60)
print()
# Create settings with defaults
print("1. Using defaults:")
settings = AppSettings()
print(f" App Name: {settings.app_name}")
print(f" Debug: {settings.debug}")
print(f" Port: {settings.port}")
print(f" Host: {settings.host}")
print()
# Override with environment variables
print("2. Override with environment variables:")
os.environ["APP_NAME"] = "production-app"
os.environ["PORT"] = "9000"
os.environ["DEBUG"] = "true"
settings2 = AppSettings()
print(f" App Name: {settings2.app_name}")
print(f" Debug: {settings2.debug}")
print(f" Port: {settings2.port}")
print(f" Host: {settings2.host}")
print()
# Clean up
os.environ.pop("APP_NAME", None)
os.environ.pop("PORT", None)
os.environ.pop("DEBUG", None)
# Override with explicit values
print("3. Override with explicit values:")
settings3 = AppSettings(app_name="test-app", port=3000, debug=True)
print(f" App Name: {settings3.app_name}")
print(f" Debug: {settings3.debug}")
print(f" Port: {settings3.port}")
print()
print("✅ Basic usage complete!")
if __name__ == "__main__":
main()