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 DbSetup(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 DbSetup(ns.BaseConfig):
    variable: Annotated[str, ns.EnvAliasStrict('OTHER')]
    # will only try to get `OTHER`

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.1.tar.gz (9.2 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.1-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for nano_settings-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ef2c98f19da6bcf048b25512ffa7273e45712975a2478b910727051c0f3ac173
MD5 a66216d336cd49890007dbde6f0e27a4
BLAKE2b-256 095e39a89a1ed81349ae3d724fd4c02fe1b79c99069670910e4f2d30e2dd76fc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for nano_settings-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4b0320bc148e3d25dc7f61426bbc751a0f85614ad5e875d99225a380de423354
MD5 f9ad0435ac6b8829734831d00411c019
BLAKE2b-256 9de012d62787af55c7b63b1f40fb275db7765d7f81d3b5275bc8ca52ed4f73d6

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