Skip to main content

environ composition

The library for parsing environ configuration and creating nested configuration classes through composition.

  • EnvConfigParser is designed to parse environment variables and a .env file to create a structured configuration.
  • EnvConfig takes the parsed environment variables (a dictionary) and creates an object with attributes matching the keys.

Installation

pip install environ-composition

Import

from environ_composition import EnvConfigParser

Usage

config = EnvConfigParser(dotenv_path='path_to/.env', separator='__').parse()

In EnvConfigParser each environment variable name goes into a lowercase attribute of EnvConfig instance.

Adding some separator (i.g. double underscores "__") to the variable name adds a level of nesting to the config.

VAR1=value1
VAR2__NESTED_VAR1=nested_value1
config.var1 = "value1" 
config.var2.nested_var1 = "nested_value1"

When specifying a template, the parser updates the values in the template.

Without a template, the parser returns the config in EnvConfig instance.

class NestedTemplate:
    def __init__(self):
        self.nested_var1 = None

class Template:
    def __init__(self):
        self.var1 = None
        self.var2 = NestedTemplate()

template = Template()

parser = EnvConfigParser('path_to/.env')

config = parser.parse(template)

The structure and nesting levels of the template must match the names in the environment variable.

Mismatched names of attributes and private attributes in the template are not updated.

Parsing values

The ast library and the literal_eval function are used for parsing string values.

Standard data types are available: str, int, float, bool, list, tuple, set, dict, etc.

VAR1=['string', 1, 1.01, True]
VAR2__NESTED_VAR1={"string": 1}

For variables with null string values ('', 'None', 'null'), None is assigned.

VAR1=
VAR2=None
VAR3=null

The use_literal_eval flag in the parse method determines to use or not parsing.

Type validation

If there is a type annotation for class attributes, the parser validates the assigned values.

If there is a type annotation for instance attributes, the parser does not validate. If an annotation is specified in the class attributes, the parser will validate the instance attribute.

Validation is not applied to nested config instances.

class NestedTemplate:
    nested_var1: str = None
    
    def __init__(self):
        self.nested_var2 = None # it will not be validated

class Template:
    var1: str = None
    var2: int = None
    var3: NestedTemplate = NestedTemplate() # it will not be validated
    var4: bool
    
    def __init__(self):
        self.var4 = None  
        self.var5 = NestedTemplate() # it will not be validated

The use_type_validation flag in the parse method determines to use or not type validation. Validation only works if use_literal_eval = True.

Release files for environ-composition 1.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for environ-composition 1.1.1
File Size Uploaded
environ_composition-1.1.1.tar.gz 6.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for environ-composition 1.1.1
File Interpreter ABI Platform
environ_composition-1.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 12.3 kB

Release files / environ_composition-1.1.1.tar.gz

Download URL environ_composition-1.1.1.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
3f0e9c69c41bd6130be21e646fae10ee24d49329d406976eca9fc567365fcd26
BLAKE2b-256 checksum
How to use checksums
be6450f45f9751e0ba404cb7460b5e386d74699bd9ef05409c02cba4439c7646
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.1

Release files / environ_composition-1.1.1-py3-none-any.whl

Download URL environ_composition-1.1.1-py3-none-any.whl
Size 5.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bb9d70a72996a7e2d5c476fda14d57eca20a47b45ef78ddb9cd00bf4c122ae5e
BLAKE2b-256 checksum
How to use checksums
2eb8add1d5f56454d66f78ff20ad316fa185b7bbce63b11ff0c087fe4e7db414
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.1

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page