Skip to main content

An extended and convenient os.environ interface

Project description


env exTENDED


dotenv .env aware environment variable handling with typing features

Overview

This is a refactoring of django-settings-env with Django specific functionality stripped out, and so implements all of the smart environment handling suitable for use outside of Django.

This module provides a convenient interface for handling the environment, and therefore configuration of any application using 12factor.net principals removing many environment specific variables and security sensitive information from application code.

This module provides some features not supported by other dotenv handlers (python-dotenv, etc.) including expansion of template variables which is very useful for DRY.

An Env instance delivers a lot of functionality by providing a type-smart front-end to os.environ, with support for most - if not all - os.environ functionality.

from envex import env

assert env['HOME'] ==  '/Users/davidn'
env['TESTING'] = 'This is a test'
assert env['TESTING'] == 'This is a test'

import os
assert os.environ['TESTING'] == 'This is a test'

assert env.get('UNSET_VAR') is None
env.set('UNSET_VAR', 'this is now set')
assert env.get('UNSET_VAR') is not None
env.setdefault('UNSET_VAR', 'and this is a default value but only if not set')
assert env.get('UNSET_VAR') == 'this is now set'
del env['UNSET_VAR']
assert env.get('UNSET_VAR') is None

An Env instance can also read a .env (default name) file and update the application environment accordingly.

It can read this either from __init__ or via the method read_env().

  • Override the base name of the dot env file, use the DOTENV environment variable.

  • Other kwargs that can be passed to Env.__init__

    • env_file (str): base name of the env file, os.environ["DOTENV"] by default, or .env
    • search_path (str or list): a single path or list of paths to search for the env file
    • overwrite (bool): overwrite already set values read from .env, default is to only set if not currently set
    • parents (bool): search (or not) parents of dirs in the search_path
    • update (bool): update os.environ if true (default) otherwise pool changes internally
    • environ (env): pass the environment to update, default is os.environ
  • Env() also takes an additional kwarg, readenv (default False) which instructs it to read dotenv files

Some type-smart functions act as an alternative to Env.get and having to parse the result:

from envex import env

env['AN_INTEGER_VALUE'] = 2875083
assert env.get('AN_INTEGER_VALUE') == '2875083'
assert env.int('AN_INTEGER_VALUE') == 2875083

env['A_TRUE_VALUE'] = True
assert env.get('A_TRUE_VALUE') == 'True'
assert env.bool('A_TRUE_VALUE') is True

env['A_FALSE_VALUE'] = 0
assert env.get('A_FALSE_VALUE') == '0'
assert env.int('A_FALSE_VALUE') == 0
assert env.bool('A_FALSE_VALUE') is False

env['A_FLOAT_VALUE'] = 287.5083
assert env.get('A_FLOAT_VALUE') == '287.5083'
assert env.float('A_FLOAT_VALUE') == 287.5083

env['A_LIST_VALUE'] = '1,"two",3,"four"'
assert env.get('A_LIST_VALUE') == '1,"two",3,"four"'
assert env.list('A_LIST_VALUE') == ['1', 'two', '3', 'four']

Note that environment variables are always stored as strings. This is enforced by the underlying os.environ, but also true of any provided environment, using the MutableMapping[str, str] contract.

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

envex-1.4.0.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

envex-1.4.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file envex-1.4.0.tar.gz.

File metadata

  • Download URL: envex-1.4.0.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.10.8 Darwin/22.1.0

File hashes

Hashes for envex-1.4.0.tar.gz
Algorithm Hash digest
SHA256 91c73d66f6bff9c713ae64f5c242bc59358faa3bdb1382fb4478cc852a26ddbf
MD5 84bd0d880cece870a6d1882dce756b0f
BLAKE2b-256 296fb6412a51700c221a197656e617bf588566401f4c738fb1c02097f58bc1b6

See more details on using hashes here.

File details

Details for the file envex-1.4.0-py3-none-any.whl.

File metadata

  • Download URL: envex-1.4.0-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.10.8 Darwin/22.1.0

File hashes

Hashes for envex-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a19cfcded7b5cd3a193f097b4df7504ff2fba333d0e9ee3291604c5920892ea5
MD5 bd6c29ba3b3bff36e9e753229a91286b
BLAKE2b-256 1b00afe9906a129b93fba1d460dd2be12433d7800070940b9ac15d56fc7b463b

See more details on using hashes here.

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