Skip to main content

No project description provided

Project description

envcon: easy environment variables parsing

Envcon - easy environment variables parsing. Envcon allows you to store configuration separated from your code, like The Twelve-Factor App suggests.
Envcon heavily leaned on python type-hints and makes configuration simple and declerative.

Contents

Features

  • Simple usage
  • Type-casting
  • Parse .env files as well as environment variables (os.environ) (useful for development)
  • Default values
  • Prefix

Install

pip install envcon

Usage

Assuming these environment variables are set (or written in .env file)

export MONGO_USER=myMongoUser
export MONGO_PASSWORD=shh_its_a_secret
export SECRET_NUMBER=0.42
export ONE_TO_TEN=1,2,3,4,5,6,7,8,9,10
export IS_ENABLED=true
export URL=http://www.google.com

Basic usage

from envcon import environment_configuration

@environment_configuration
class Configs:
    SECRET_NUMBER: float
    IS_ENABLED: bool
    ONE_TO_TEN: list[int] # on python 3.8 use List[int] (from typing import List) 

print(Configs.SECRET_NUMBER) # 0,42
print(type(Configs.SECRET_NUMBER)) # <class 'float'>
print(Configs.IS_ENABLED) # True
print(type(Configs.IS_ENABLED)) # <class 'bool'>
print(type(Configs.ONE_TO_TEN[0])) # <class 'int'> 

Prefix

from envcon import environment_configuration

@environment_configuration(prefix="MONGO_")
class MongoConfiguration:
    USER: str
    PASSWORD: str

print(MongoConfiguration.USER) # myMongoUser
    

Supported types

The following types hints are supported

Builtins and from typing:

  • str
  • bool
  • int
  • float
  • list
  • list[T] # >= python 3.9
  • dict
  • List
  • List[T]
  • Dict
  • Optional[T]

Reading .env files

By default, envcon will parse your .env file. This feature is useful for local development.
notice that .env will not override your environment variables.

You can turn this feature off:

@environment_configuration(include_dot_env_file=False)
class MyConfigClass:
    ...

Why...?

Why environment variables?

See The 12-factor App section on configuration.

Why not os.environ?

Basically, because this:

class Config:
    MAX_CONNECTION = int(os.environ.get("MAX_CONNECTION", "42"))
    TIMEOUT = float(os.environ.get("TIMEOUT", "4.2"))
    MY_PASSWORD = os.environ["MY_PASSWORD"] #required w/o default value
    OPTIONAL_URL = os.environ.get("OPTIONAL_URL", None)
    OPTIONAL_NUMBER = int(os.environ.get("OPTIONAL_NUMBER", "0")) or None
    NUMS_LIST = [int(i) for i in os.environ["NUMS_LIST"].splite(",")]
    NUMS_LIST_WITH_DEFAULT = [int(i) for i in os.environ.get("NUMS_LIST", "1,2,3").splite(",")]

will simply turn into this:

from typing import Optional, List

@environment_configuration
class Config:
    MAX_CONNECTION: int = 42
    TIMEOUT: float = 4.2
    MY_PASSWORD: str
    OPTIONAL_URL: Optional[str]
    OPTIONAL_NUMBER: Optional[int]
    NUMS_LIST: list[int] # in python 3.8 use List[int]
    NUMS_LIST_WITH_DEFAULT: list[int] = [1, 2, 3]

envcon will help you

  • cast environment variables to the correct type
  • specify required environment variables
  • define default values
  • parse list and dict

License

MIT licensed.

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

envcon-0.1.6.tar.gz (6.1 kB view hashes)

Uploaded Source

Built Distribution

envcon-0.1.6-py3-none-any.whl (6.6 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