A library to map dataclass and environmental variables
Project description
| |
___ _ ____ _____| | __ _ ___ ___ ___ ___
/ _ \ '_ \ \ / / __| |/ _` / __/ __|/ _ \/ __|
| __/ | | \ V / (__| | (_| \__ \__ \ __/\__ \
\___|_| |_|\_/ \___|_|\__,_|___/___/\___||___/
envclass is a small library which maps environment variables dataclass's fields This is very useful in a case like you want to override the configurations read from file.
Installation
pip install envclasses
Usage
# foo.py
from typing import List, Dict
from dataclasses import dataclass, field
from envclasses import envclass, load_env
@envclass
@dataclass
class Foo:
i: int
s: str
f: float
b: bool
lst: List[int] = field(default_factory=list)
dct: Dict[str, float] = field(default_factory=dict)
foo = Foo(i=10, s='foo', f=0.1, b=False)
# Map environment variables to fields.
load_env(foo, prefix='FOO')
print(foo)
run
$ python foo.py
Foo(i=10, s='foo', f=0.1, b=False, lst=[], dct={})
Run with environment variables
$ FOO_I=20 FOO_S=foofoo FOO_F=0.2FOO_B=true FOO_DCT="{key: 100.0}" FOO_LST="[1, 2, 3]" python foo.py
Foo(i=20, s='foofoo', f=0.2, b=True, lst=[1, 2, 3], dct={'key': 100.0})
Notes:
- If
prefix
is not defined or set toNone
, the default prefix (ENV_
) will be expected. - If
prefix
is set to the empty string''
, no prefix will be expected in the environment variables.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
envclasses-0.2.4.tar.gz
(4.7 kB
view hashes)
Built Distribution
Close
Hashes for envclasses-0.2.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 64e58ed9d2a09deee14a1ed3bd71e0b418649a1813b3c968a48f89a80e35d358 |
|
MD5 | 9a4d5a6d613d94ac71942cf8cceb7c96 |
|
BLAKE2b-256 | 4f4151aa0e1673acc44d0244f54ad6766bd5b459b992c1cc097823f9b5ab29ba |