Skip to main content

A library to map dataclass and environmental variables

Project description

                    | |
  ___ _ ____   _____| | __ _ ___ ___  ___  ___
 / _ \ '_ \ \ / / __| |/ _` / __/ __|/ _ \/ __|
|  __/ | | \ V / (__| | (_| \__ \__ \  __/\__ \
 \___|_| |_|\_/ \___|_|\__,_|___/___/\___||___/

image image CircleCI

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})

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

envclasses-0.2.2.tar.gz (4.3 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page