### Installing JSDC Loader (Bash) Source: https://github.com/unkinseong/jsdc_loader/blob/main/src/jsdc_loader/README.md This snippet provides the command line instruction to install the JSDC Loader library using pip, the standard package installer for Python. ```Bash pip install jsdc-loader ``` -------------------------------- ### Install JSDC Loader Source: https://github.com/unkinseong/jsdc_loader/blob/main/README.md This command installs the JSDC Loader library using pip, the standard package installer for Python. ```bash pip install jsdc_loader ``` -------------------------------- ### Basic Dataclass Loading and Dumping with JSDC Loader Source: https://github.com/unkinseong/jsdc_loader/blob/main/README.md This example demonstrates how to define a simple dataclass, create an instance, dump it to a JSON file using `jsdc_dump`, and then load it back into a dataclass instance using `jsdc_load`. ```python from dataclasses import dataclass from jsdc_loader import jsdc_load, jsdc_dump @dataclass class DatabaseConfig: host: str = 'localhost' # default value must be provided port: int = 3306 user: str = 'root' password: str = 'password' # Dump configuration to 'config.json' db_config = DatabaseConfig() jsdc_dump(db_config, 'config.json') # Load configuration from 'config.json' loaded_db_config = jsdc_load('config.json', DatabaseConfig) print(loaded_db_config.host) # Accessing the host attribute from the loaded data ``` -------------------------------- ### Working with Pydantic Models (Python) Source: https://github.com/unkinseong/jsdc_loader/blob/main/src/jsdc_loader/README.md Provides an example of using JSDC Loader with Pydantic BaseModel classes, including nested models and lists of models, showcasing serialization and deserialization. ```Python from pydantic import BaseModel from typing import List, Dict from jsdc_loader import jsdc_load, jsdc_dump class ServerConfig(BaseModel): name: str = "main" port: int = 8080 ssl: bool = True headers: Dict[str, str] = {"Content-Type": "application/json"} class ApiConfig(BaseModel): servers: List[ServerConfig] = [] timeout: int = 30 retries: int = 3 # Create and serialize api_config = ApiConfig() api_config.servers.append(ServerConfig(name="backup", port=8081)) api_config.servers.append(ServerConfig(name="dev", port=8082, ssl=False)) jsdc_dump(api_config, "api_config.json") loaded_api = jsdc_load("api_config.json", ApiConfig) ``` -------------------------------- ### Handling Nested Dataclasses (Python) Source: https://github.com/unkinseong/jsdc_loader/blob/main/src/jsdc_loader/README.md Illustrates how JSDC Loader manages nested dataclasses and dictionary fields during serialization and deserialization, showing examples of dumping to a file and loading from a JSON string. ```Python from dataclasses import dataclass, field from typing import List, Dict from jsdc_loader import jsdc_load, jsdc_loads, jsdc_dump @dataclass class DatabaseConfig: host: str = "localhost" port: int = 3306 user: str = "root" password: str = "password" @dataclass class AppConfig: name: str = "myapp" database: DatabaseConfig = field(default_factory=DatabaseConfig) debug: bool = False settings: Dict[str, str] = field(default_factory=dict) # Create a config object config = AppConfig( name="production-app", database=DatabaseConfig(host="db.example.com", port=5432), settings={"theme": "dark", "language": "en"} ) # Serialize to JSON jsdc_dump(config, "app_config.json") # Deserialize from JSON string json_str = '{"name": "test-app", "database": {"host": "localhost", "port": 3306, "user": "tester", "password": "test123"}, "debug": true, "settings": {"env": "test"}}' test_config = jsdc_loads(json_str, AppConfig) ``` -------------------------------- ### Nested Dataclass Loading and Dumping with JSDC Loader Source: https://github.com/unkinseong/jsdc_loader/blob/main/README.md This example shows how to handle nested dataclass structures. It defines two dataclasses, `UserConfig` and `AppConfig`, where `AppConfig` contains instances of `UserConfig` and `DatabaseConfig`. It demonstrates dumping and loading this nested structure. ```python from dataclasses import dataclass, field from jsdc_loader import jsdc_load, jsdc_dump @dataclass class UserConfig: name: str = 'John Doe' age: int = 30 @dataclass class AppConfig: user: UserConfig = field(default_factory=lambda: UserConfig()) database: DatabaseConfig = field(default_factory=lambda: DatabaseConfig()) # Dump configuration to 'config.json' app_config = AppConfig() jsdc_dump(app_config, 'config.json') # Load configuration from 'config.json' loaded_app_config = jsdc_load('config.json', AppConfig) print(loaded_app_config.user.name) # Accessing the name attribute from the loaded data ``` -------------------------------- ### Deeper Nested Dataclass Loading and Dumping Source: https://github.com/unkinseong/jsdc_loader/blob/main/README.md This example demonstrates handling a deeper level of nesting by defining a `ControllerConfig` dataclass that contains an `AppConfig` instance, which in turn contains `UserConfig` and `DatabaseConfig`. It shows dumping and loading this multi-level nested structure. ```python from dataclasses import dataclass, field from jsdc_loader import jsdc_load, jsdc_dump @dataclass class ControllerConfig: controller_id: str = 'controller_01' controller_type: str = 'controller_type_01' controller_version: str = 'controller_version_01' utc_offset: float = 0.0 app: AppConfig = field(default_factory=lambda: AppConfig()) # Dump configuration to 'config.json' controller_config = ControllerConfig() jsdc_dump(controller_config, 'config.json') # Load configuration from 'config.json' loaded_controller_config = jsdc_load('config.json', ControllerConfig) print(loaded_controller_config.controller_id) # Accessing the controller_id attribute from the loaded data ``` -------------------------------- ### Dataclass with Enum and Nested Structures Loading and Dumping Source: https://github.com/unkinseong/jsdc_loader/blob/main/README.md This example extends the nested dataclass concept by including an `Enum` type within a dataclass field. It defines a `UserType` enum and includes it in `UserConfig`, demonstrating how JSDC Loader handles enums during dumping and loading. ```python from dataclasses import dataclass, field from enum import Enum, auto from jsdc_loader import jsdc_load, jsdc_dump class UserType(Enum): ADMIN = auto() USER = auto() @dataclass class UserConfig: name: str = 'John Doe' age: int = 30 married: bool = False user_type: UserType = field(default_factory=lambda: UserType.USER) @dataclass class AppConfig: user: UserConfig = field(default_factory=lambda: UserConfig()) database: DatabaseConfig = field(default_factory=lambda: DatabaseConfig()) # Dump configuration to 'config.json' app_config = AppConfig() jsdc_dump(app_config, 'config.json') # Load configuration from 'config.json' loaded_app_config = jsdc_load('config.json', AppConfig) print(loaded_app_config.user.user_type) # Accessing the user type attribute from the loaded data ``` -------------------------------- ### Basic Dataclass Serialization/Deserialization (Python) Source: https://github.com/unkinseong/jsdc_loader/blob/main/src/jsdc_loader/README.md Demonstrates the fundamental usage of jsdc_dump and jsdc_load to serialize a simple Python dataclass instance to a JSON file and then deserialize it back into a dataclass object. ```Python from dataclasses import dataclass, field from jsdc_loader import jsdc_load, jsdc_dump @dataclass class Config: name: str = "default" port: int = 8080 debug: bool = False # Serialize to JSON file config = Config(name="myapp", port=5000) jsdc_dump(config, "config.json") # Deserialize from JSON file loaded_config = jsdc_load("config.json", Config) print(loaded_config.name) # "myapp" ``` -------------------------------- ### Generating Random Indentation in Python Source: https://github.com/unkinseong/jsdc_loader/blob/main/test.ipynb This Python script uses the 'random' module to generate a random integer between 20 and 39 for each of 50 iterations. It then prints a string composed solely of spaces, with the number of spaces determined by the randomly generated integer, effectively creating lines with random indentation. ```Python import random for i in range(50): n = random.randint(20, 39) print(''.join(' ' for _ in range(n))) ``` -------------------------------- ### Serializing/Deserializing Enums in Dataclasses (Python) Source: https://github.com/unkinseong/jsdc_loader/blob/main/src/jsdc_loader/README.md Shows how to define and use Python Enum types within dataclasses and demonstrates JSDC Loader's ability to correctly serialize and deserialize these enum values. ```Python from dataclasses import dataclass, field from enum import Enum, auto from jsdc_loader import jsdc_load, jsdc_dump class UserRole(Enum): ADMIN = auto() USER = auto() GUEST = auto() @dataclass class User: name: str role: UserRole = UserRole.USER user = User(name="John", role=UserRole.ADMIN) jsdc_dump(user, "user.json") loaded_user = jsdc_load("user.json", User) assert loaded_user.role == UserRole.ADMIN ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.