A library to map dataclass and environmental variables
Project description
| |
___ _ ____ _____| | __ _ ___ ___ ___ ___
/ _ \ '_ \ \ / / __| |/ _` / __/ __|/ _ \/ __|
| __/ | | \ V / (__| | (_| \__ \__ \ __/\__ \
\___|_| |_|\_/ \___|_|\__,_|___/___/\___||___/
envclass is a meta programming library on top of dataclass. Once envclass decorator is specified in a dataclass, It will generate dunder function which loads values from environment variables. This functionality is very useful in a case like you want to override the exisiting configurations for an application by the ones defined in environment variables.
Requirements
python >= 3.6.0
Installation
-
Install from PyPI
pip install envclasses
-
Install development version from Github
pip install git+https://github.com/yukinarit/WebSocketSample.git
Usage
-
Create Hoge instance
from typing import List, Dict from dataclasses import dataclass, field from envclasses import envclass, load_env @envclass @dataclass class Hoge: i: int s: str f: float b: bool lst: List[int] = field(default_factory=list) dct: Dict[str, float] = field(default_factory=dict) # Create an instance. hoge = Hoge(i=10, s='hoge', f=0.1, b=False) # Load values from environment variables. load_env(hoge, prefix='HOGE') print(hoge)
-
Run
$ python hoge.py
Hoge(i=10, s='hoge', f=0.1, b=False, lst=[], dct={})
-
Override Hoge values by environment variables
$ export HOGE_I=20 $ export HOGE_S=hogehoge $ export HOGE_F=0.2 $ export HOGE_B=true $ export HOGE_DCT="{key: 100.0}" $ export HOGE_LST="[1, 2, 3]" $ python hoge.py
Hoge(i=20, s='hogehoge', f=0.2, b=True, lst=[1, 2, 3], dct={'key': 100.0})
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.1.tar.gz
(4.3 kB
view hashes)
Built Distribution
Close
Hashes for envclasses-0.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 656bda9663b178bf50f06d932c5f5b5a8a71a85106ca2095af2f6b4967070d73 |
|
MD5 | 6448ba855b6bd7ba65657f6f7e443665 |
|
BLAKE2b-256 | f32c0a1f3df3a1088a3815a0ff00192f41e8062ab17d1e2b45ccd2c382edadb4 |