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.0.tar.gz
(4.8 kB
view hashes)
Built Distribution
Close
Hashes for envclasses-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e4444ce3119c342c554af59e79b8ad5ff8379105151c215d2a6d3c65d66066c5 |
|
MD5 | cbf01fed7730a53892f8e0127db3b23e |
|
BLAKE2b-256 | 775f1847d40e322c20da9628d5728878c9b892eb8df84cf7417413d1034b1e7a |