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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file dataclassconfig-0.0.3.tar.gz.

File metadata

  • Download URL: dataclassconfig-0.0.3.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.13

File hashes

Hashes for dataclassconfig-0.0.3.tar.gz
Algorithm Hash digest
SHA256 09e67026e2aca3baca3f1aa176fc3b85c3f166a0d5e3f443b7aa02b097d41d0b
MD5 179d958ccc153b25055c3e9c868c9a31
BLAKE2b-256 cb3a36a3e47721fa837de9ebb489fe1cfa380a2771a22efc2c49359ec12cc701

See more details on using hashes here.

File details

Details for the file dataclassconfig-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for dataclassconfig-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1cd56e835f7bee6a788e1fb3589c8fd88ac7866d066f146ba13940dcfd9dd3b1
MD5 0d6911b960f08ab9635a863b8e143862
BLAKE2b-256 f15b25049849882682b596e04c12a8ba26ff65753eb7eb5f8e5cad8fb6c8d7db

See more details on using hashes here.

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