Skip to main content

Extra small env config system (like pydantic-settings, but simpler)

Project description

Nano settings

Creates simple python config from environment variables. Smaller analog of pydantic-settings.

In comparison with pydantic-settings

Pros:

  • Easier to understand and support.
  • Relies only on standard library.
  • Uses dataclasses, not polluting your code with external types (if you're using approaches similar to clean architecture).
  • Almost no configuration.

Cons:

  • No validation, you have to do it yourself.
  • Almost no configuration.

Installation

pip install nano-settings

Basic usage

from dataclasses import dataclass

import nano_settings as ns


@dataclass
class DbSetup(ns.BaseConfig):
    max_sessions: int
    autocommit: bool = True


@dataclass
class Database(ns.BaseConfig):
    url: str
    timeout: int
    setup: DbSetup


# export MY_VAR__URL=https://site.com
# export MY_VAR__TIMEOUT=10
# export MY_VAR__SETUP__MAX_SESSIONS=2
config = ns.from_env(Database, env_prefix='my_var')
print(config)
# Database(timeout=10, url='https://site.com', setup=DbSetup(max_sessions=2, autocommit=True))

Supported variants

Straightforward - when your annotations are callable

from dataclasses import dataclass

import nano_settings as ns


@dataclass
class Config(ns.BaseConfig):
    variable: int
    # equivalent of
    # variable = int(os.getenv('VARIABLE'))

Annotated - when your annotations are complex and cannot be called

from dataclasses import dataclass
from typing import Annotated
import json

import nano_settings as ns


@dataclass
class Config(ns.BaseConfig):
    variable: Annotated[list[int], lambda x: x['key'], json.loads]
    # equivalent of
    # variable = json.loads(os.getenv('VARIABLE'))['key']

Nested - when your annotations are models

Example is listed in Basic usage

Aliases - when you want to get value by different name

Normal - try default and then alternatives

from dataclasses import dataclass
from typing import Annotated

import nano_settings as ns


@dataclass
class Config(ns.BaseConfig):
    variable: Annotated[str, ns.EnvAlias('OTHER')]
    # will try to get `VARIABLE` and then `OTHER`

Strict - try only alternatives

from dataclasses import dataclass
from typing import Annotated

import nano_settings as ns


@dataclass
class Config(ns.BaseConfig):
    variable: Annotated[str, ns.EnvAliasStrict('OTHER')]
    # will only try to get `OTHER`

Nullable - if null is also a valid value

from dataclasses import dataclass
from typing import Annotated

import nano_settings as ns


@dataclass
class Config(ns.BaseConfig):
    variable: Annotated[int | None, ns.Nullable(int)]

Choices - if you have a set of valid variants

from dataclasses import dataclass
from typing import Annotated

import nano_settings as ns


@dataclass
class Config(ns.BaseConfig):
  variable: Annotated[str, ns.Choices('one', 'two')]

Interval - when you have minimum and maximum (including)

from dataclasses import dataclass
from typing import Annotated

import nano_settings as ns


@dataclass
class Config(ns.BaseConfig):
  variable_1: Annotated[int, ns.Interval(1, 15)]
  variable_2: Annotated[float, ns.Interval(0.0, 0.6, cast=float)]

Boolean - when you're using true or false

from dataclasses import dataclass
from typing import Annotated

import nano_settings as ns


@dataclass
class Config(ns.BaseConfig):
  variable_1: Annotated[bool, ns.Boolean()]

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

nano_settings-0.1.2.tar.gz (10.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nano_settings-0.1.2-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file nano_settings-0.1.2.tar.gz.

File metadata

  • Download URL: nano_settings-0.1.2.tar.gz
  • Upload date:
  • Size: 10.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.9

File hashes

Hashes for nano_settings-0.1.2.tar.gz
Algorithm Hash digest
SHA256 430564cd3eaf6fb934b079273d311cbe1c3091b9c208ea2f293ae2b8a377757a
MD5 ca49971f1f3d4b3238042bc4b7bcaf12
BLAKE2b-256 ebdcd8887fa155cea104e42de1a3c53cadb113c0a33b424c0d9ee6f2ff932dc0

See more details on using hashes here.

File details

Details for the file nano_settings-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for nano_settings-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 10205c5a9b81aeb358749aa478ce1bf26dbd00c7814788e7d2f6074307076b2b
MD5 e17aae5882d93897db3a37e5cf347edc
BLAKE2b-256 d20403cdafc9f3cba47e78e07a806a76e0925005433c43ec401900bee3ac71cc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page