Skip to main content

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

Metadata

Release files for xdantic 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xdantic 0.0.2
File Size Uploaded Provenance Actions
xdantic-0.0.2.tar.gz 97.5 kB Without provenance Details for xdantic-0.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for xdantic 0.0.2
File Interpreter ABI Platform Actions
xdantic-0.0.2-py3-none-any.whl Python 3 none any Details for xdantic-0.0.2-py3-none-any.whl

Total release size: 102.8 kB

Release files / xdantic-0.0.2.tar.gz

Download URL xdantic-0.0.2.tar.gz
Size 97.5 kB
Tags Source
SHA-256 checksum
How to use checksums
f85c687a291d7a9ffb98772faa2ad5e2cf196efe3740dc20c39e0e47830331ab
BLAKE2b-256 checksum
How to use checksums
959b8f805f7d455778e427f52665e8bc4ad52b8909350c856870c19c3cbfd403
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / xdantic-0.0.2-py3-none-any.whl

Download URL xdantic-0.0.2-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b6fb2f9dfad5bdf4cad71fa398c872a78717995968b9c2497d16e0174493f9d4
BLAKE2b-256 checksum
How to use checksums
37a3640d19de6fd4e0fec1e759532f9b13e6136d1d6a2de7a2eaa635ed5528bc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Security for xdantic 0.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page