Skip to content

The simplest interface for working with configuration files

License

Notifications You must be signed in to change notification settings

rilshok/dataclassconfig

Repository files navigation

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

About

The simplest interface for working with configuration files

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published