Skip to main content

The simplest interface for working with configuration files

Project description

dataclassconfig

License Version Python versions Code style: black

The simplest interface for working with configuration files. Describe the structure of your configuration file as a dataclass. Use inheritance and composition to describe the complex hierarchical structure of a configuration file.

Installation

pip install dataclassconfig

Requirements

Minimum Python version supported by dataclassconfig is 3.6

Usage

Describe the structure of your config file like a dataclass and inherit it from Сonfig

from dataclassconfig import Config

class Socket(Config):
    host: str
    port: int

class DatabaseConnection(Socket):
    database: str
    username: str
    password: str

class ServerConfig(Config):
    root: str
    db: DatabaseConnection

Create a configuration file according to the described structure in YML or JSON format

# server-config.yml
root: ~/server
db:
  host: localhost
  port: 1234
  database: database
  username: demouser
  password: demopassword
// server-config.json
{
    "root": "~/server",
    "db": {
        "host": "localhost",
        "port": 1234,
        "database": "database",
        "username": "demouser",
        "password": "demopassword",
    },
}

Load the config file using your class

config = ServerConfig.load("server-config.yml")

or

config = ServerConfig.load("server-config.json")

The load method will check the completeness of the provided data in the configuration file and strictly match the data types. The result will be the same in both cases, the config object contains fields defined in the class

config :- ServerConfig(
    root='~/server',
    db=DatabaseConnection(
        host='localhost',
        port=1234,
        database='database',
        username='demouser',
        password='demopassword'
    )
)

config.db.username :- 'demouser'

Gratitude

This module works thanks to the implementation in the dacite project

Authors

Created by Vladislav A. Proskurov

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

dataclassconfig-0.0.3.tar.gz (4.2 kB view hashes)

Uploaded Source

Built Distribution

dataclassconfig-0.0.3-py3-none-any.whl (4.8 kB view hashes)

Uploaded Python 3

Supported by

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