Skip to main content

The simplest interface for working with configuration files

Project description

dataclassconfig

Installation

pip install dataclassconfig

Usage

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

For example:

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

For example:

# 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'

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.1.tar.gz (3.8 kB view hashes)

Uploaded Source

Built Distribution

dataclassconfig-0.0.1-py3-none-any.whl (4.4 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