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.3.tar.gz
(10.6 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nano_settings-0.1.3.tar.gz.
File metadata
- Download URL: nano_settings-0.1.3.tar.gz
- Upload date:
- Size: 10.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6a0cb4ef98fd423bc4b52c1f05d6ab50be2ee9ddbb36975ac3026cb5aa7f723f
|
|
| MD5 |
9760fc0c804d1bf40e7b653b25bbc76c
|
|
| BLAKE2b-256 |
afb8bb3049dec2ed65c0cd034e7ca8671d468987ece1d18e3fe9645f45cbe820
|
File details
Details for the file nano_settings-0.1.3-py3-none-any.whl.
File metadata
- Download URL: nano_settings-0.1.3-py3-none-any.whl
- Upload date:
- Size: 9.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f99123f63bc59ee5f940ca979be3813c80e19f60fe66e8e8b5252d4b7dfb7d02
|
|
| MD5 |
624c7116e538ce5ba1513b59dc03b342
|
|
| BLAKE2b-256 |
0904b5a1a3615f214a3730fb73e6093e93beae27a19785a6c6c6e9b8567aed7e
|