Skip to main content

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


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)

Uploaded Source

Built Distribution

envclasses-0.2.0-py3-none-any.whl (4.8 kB view hashes)

Uploaded Python 3

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