Skip to main content

A lightweight Python config library on top of Pydantic.

Project description

xdantic

Lightweight configuration library on top of Pydantic

Getting started

This notebook shows some example usages of xdantic.

It explains how to provide values to the configuration object, how to access values and how nested configurations work.

The configuration management is simple, including the config object itself with all the fields that you implement

from typing import Literal
from pydantic import HttpUrl, Field

from xdantic import XConfig

class DemoConfig(XConfig):
    """
    The object `DemoConfig` is the central config object that
    will hold your values and can be extended to your needs with new values.
    Here, you can leverage the power of Pydantic and Python typehints to create a configuration
    that is safe, documented and easy to use.
    
    Check https://pydantic-docs.helpmanual.io/usage/types/ to learn more about field types in Pydantic.
    """
    DB_HOST: HttpUrl  # will be validate to be a httpUrl 
    DB_PORT: int = Field(default=3333, le=64000, ge=1024)  # will be validated to be in the given range
    DB_TYPE: Literal["postgres", "mysql"] = "mysql"  # can only take on values present in the `Literal` definition

Create a config

To create a config object with parsed values you can simply call the initializer method and XConfig will automatically create the object using values from e.g. environment variables.

Now that we have the DemoConfig we are ready to use our config in the code. The next cell provides an example of parsing values to the config from environment variables and accessing them:

import os 

os.environ['DB_PORT'] = '8888'  # note, how this value will take presedence over the default
os.environ['DB_HOST'] = 'https://postgres-develop.cluster.svc.local'
os.environ['DB_TYPE'] = 'postgres'

print(DemoConfig())

Nested configuration

Sometime, it is desired to nest configurations to have a structure like config.db.usernamne. This can be easily achieved by creating a separate class that inherits from pydantic.BaseModel and add it as an attribute to the config object (DemoConfig). In the cell below, we overwrite the definitions from above to create the nested config object:

from pydantic import BaseModel, PositiveFloat, HttpUrl


# for objects that will be nested into the root config object, inherit from `BaseModel`
class ModelConfig(BaseModel):
    lr: PositiveFloat = 1e-3
        

# overwrite the `XConfig` definition from above to include the sub-config object.
class DemoConfig(XConfig):
    MODEL_CFG: ModelConfig = ModelConfig()
    
    DB_HOST: HttpUrl  # will be validate to be a httpUrl 
    DB_PORT: int = Field(default=3333, le=64000, ge=1024)  # will be validated to be in the given range
    DB_TYPE: Literal["postgres", "mysql"] = "mysql"  # can only take on values present in the `Literal` definition
os.environ['MODEL_CFG__LR'] = '0.1'

print(DemoConfig())

Reading from .env file {#reading-from-env-file}

In the notebooks folder there is a file called '.env' containing some config values. XConfig objects will also parse .env files automatically if they exist.

os.environ.pop('MODEL_CFG__LR', None)
print(ModelConfig())

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

xdantic-0.0.3.tar.gz (97.8 kB view details)

Uploaded Source

Built Distribution

xdantic-0.0.3-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: xdantic-0.0.3.tar.gz
  • Upload date:
  • Size: 97.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for xdantic-0.0.3.tar.gz
Algorithm Hash digest
SHA256 8ebaa96bcd9a0bc40ff209adbbc981f6269bd3b0849c6a42b7e4c4b062bd009c
MD5 1327da6c506a07211858afffdad0c7f1
BLAKE2b-256 bfa18a16b9c1d89f195ba0589fc2ce6333bd71219d2b72528281b28eeb4e5bf9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: xdantic-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.17

File hashes

Hashes for xdantic-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 13c2b2728122339e4da524b0879b9ff957b9247c5b1322fbb9f98eb98f14e85e
MD5 1553113d52d7035f0dfa36f7cefb7a49
BLAKE2b-256 8ebc3c046c7f6b2dd64a5db4d8fccae5cf3c7a993b836ecc301af96d43efd508

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