docs
evals
scenario-1
scenario-10
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
Build a configuration loader that converts JSON data into Python objects for a web application settings system.
Your task is to implement a configuration loader that can parse JSON configuration data and convert it into strongly-typed Python objects. The system should handle multiple configuration sections with different data types.
The configuration will contain three sections:
Database Configuration - Connection details for the database
host (string): Database server hostnameport (integer): Database server portusername (string): Database usernamemax_connections (integer): Maximum number of connectionsAPI Settings - External API configuration
base_url (string): Base URL for API callstimeout (integer): Request timeout in secondsretry_count (integer): Number of retries for failed requestsenabled (boolean): Whether API integration is enabledFeature Flags - Application feature toggles
dark_mode (boolean): Enable dark mode UIbeta_features (boolean): Enable beta featuresanalytics_enabled (boolean): Enable analytics trackingCreate Python classes to represent each configuration section using dataclasses with type hints. Implement a load_config function that takes JSON strings for each section and returns the corresponding Python objects.
from dataclasses import dataclass
@dataclass
class DatabaseConfig:
host: str
port: int
username: str
max_connections: int
@dataclass
class APISettings:
base_url: str
timeout: int
retry_count: int
enabled: bool
@dataclass
class FeatureFlags:
dark_mode: bool
beta_features: bool
analytics_enabled: bool
def load_config(json_string: str, config_type: type):
"""
Load configuration from a JSON string and convert to the specified type.
Args:
json_string: JSON string containing configuration data
config_type: The target class type to deserialize into
Returns:
An instance of the specified config_type
"""
passProvides JSON serialization and deserialization support for Python objects.