Skip to main content

Parses .env files against dataclass based schema validation

Project description

envdataclass

Parses .env files against dataclass based schema validation

pip install envdataclass

Supported schema types

  • int
  • str
  • List[int]
  • bool

Example usage (short and sweet)

from dataclasses import dataclass
from typing import List

from envdataclass import from_string


@dataclass
class my_cool_config_schema:
    test_list_int: List[int]


foo = from_string(my_cool_config_schema, 'test_list_int=1,2,3,5')
print(foo)
print(type(foo.test_list_int))
print(type(foo.test_list_int[1]))

Output:

my_cool_config_schema(test_list_int=[1, 2, 3, 5])
<class 'list'>
<class 'int'>

Example usage (longer version)

Define a data class for your config schema:

@dataclass
class my_cool_config_schema:
    test_list_int: List[int]

    test_int: int
    test_string: str = '123'
    test_bool: bool = False

    test_string_d: str = 'hello'
    test_int_d: int = 1234
    test_bool_d: bool = False

The variables without a default value assigned will be assumed to be required. This means an exception will be thrown when any number of required variables are not present or are invalid in the environment file.

Example: TypeError: __init__() missing 1 required positional argument: 'test_int'

Setup a .env file:

test_string_d=hellofromtheotherside
test_int=98766
test_bool_d=true
test_list_int=123,456,123

Load it:

foo = from_file(my_cool_config_schema, '/path/.env')

Result:

lol(test_list_int=[123, 456, 123], 
    test_int=98766, 
    test_string='123', 
    test_bool=False, 
    test_string_d='hellofromtheotherside', 
    test_int_d=1234, 
    test_bool_d=True)
  • test_list_int got parsed from a str equal to '123,456,123' to a List equal to [123, 456, 123]
  • test_bool_d got parsed from str equal to 'true' to True

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

envdataclass-0.4.tar.gz (2.5 kB view hashes)

Uploaded Source

Built Distribution

envdataclass-0.4-py3-none-any.whl (2.7 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