Skip to main content

JSON Data Class Loader

Project description

JSDC Loader (JSON Data Class Loader)

JSDC Loader is a Python utility for loading JSON configuration files into dataclass objects. It provides a simple and type-safe way to manage configuration data in your Python applications by forcing the use of dataclass and type hinting.

Features

  • Load JSON configuration files into dataclass objects
  • Support for nested dataclass structures
  • Type checking and conversion for configuration values
  • Easy updating of configuration from different files
  • Ability to dump modified configurations back to JSON

Installation

To install JSDC Loader, you can use pip:

pip install jsdc_loader

Usage

Here's an example of how to use JSDC Loader:

Example 1

from dataclasses import dataclass
from jsdc_loader import JSDC_Loader

@dataclass
class DatabaseConfig:
    host: str = 'localhost'  # default value must be provided
    port: int = 3306
    user: str = 'root'
    password: str = 'password'

# Create an instance of JSDC_Loader to load configuration from 'config.json'
config_loader = JSDC_Loader('config.json', DatabaseConfig)
# Access the host attribute from the loaded configuration
print(config_loader.data.host)  # Accessing the host attribute from the loaded data

Example 2

from dataclasses import dataclass, field
from jsdc_loader import JSDC_Loader

@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())

# Create an instance of JSDC_Loader to load configuration from 'config.json'
app_config_loader = JSDC_Loader('config.json', AppConfig)
# Access the name attribute from the user configuration
print(app_config_loader.data.user.name)  # Accessing the name attribute from the loaded data

Example 3

from dataclasses import dataclass, field
from enum import Enum, auto
from jsdc_loader import JSDC_Loader

@dataclass
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())

# Create an instance of JSDC_Loader to load configuration from 'config.json'
app_config_loader = JSDC_Loader('config.json', AppConfig)
# Access the user type attribute from the user configuration
print(app_config_loader.data.user.user_type)  # Accessing the user type attribute from the loaded data

Example 4

from dataclasses import dataclass, field
from jsdc_loader import JSDC_Loader

@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())

# Create an instance of JSDC_Loader to load configuration from 'config.json'
controller_config_loader = JSDC_Loader('config.json', ControllerConfig)
# Access the controller_id attribute from the loaded configuration
print(controller_config_loader.data.controller_id)  # Accessing the controller_id attribute from the loaded data

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jsdc_loader-0.0.6.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jsdc_loader-0.0.6-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file jsdc_loader-0.0.6.tar.gz.

File metadata

  • Download URL: jsdc_loader-0.0.6.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.4

File hashes

Hashes for jsdc_loader-0.0.6.tar.gz
Algorithm Hash digest
SHA256 cd0afb510d480c1dc83688a70350239195259e3eaa8ff30b0a4c563c45dc1a30
MD5 7824807b03eb6fb93be7a10635f8661e
BLAKE2b-256 b80105c216181ede6f1f15262076390b9137b42393dcafc892b8c51906711755

See more details on using hashes here.

File details

Details for the file jsdc_loader-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: jsdc_loader-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.4

File hashes

Hashes for jsdc_loader-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 f05fc189de9a3ed85e6b093828b2e0cb59fe56e176b2bf3dd24823236b25ea3a
MD5 6b6bf5b49c4589e4925f3b64818f806c
BLAKE2b-256 d79cafff5394a653530ac5b39fc4802a6a31b67b11deaf01f3ad37143e806662

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page