项目作者: adeharo9

项目描述 :
python-dotenv wrapper with attribute-like access to environment variables
高级语言: Python
项目地址: git://github.com/adeharo9/dotenvpy.git
创建时间: 2020-07-13T09:36:21Z
项目社区:https://github.com/adeharo9/dotenvpy

开源协议:Other

下载


dotenvpy, a python-dotenv wrapper

version 0.1.0

This module is a simple wrapper around the well-known python-dotenv module for Python. It provides attribute-like read access to environment variables.

Table of contents

  1. Installation
  2. Usage
    1. Typed environment variables
  3. Features

Installation

INSTALLATION THROUGH pip COMING SOON!

  • pip install dotenvpy

python-dotenv already comes as a requirement for dotenvpy; no need to worry about that.

Usage

Use it as you would use python-dotenv:

  1. import dotenvpy as env
  2. env.load_dotenv()
  3. print(env.HOME)
  4. print(env.PATH)
  5. # This is equivalent to:
  6. # print(os.getenv('HOME'))
  7. # print(os.getenv('PATH'))

Typed environment variables

dotenvpy uses Python’s ast module to evaluate each environment variable as a literal. This allows for automatic conversion to the right types at access time, reducing the number of explicit type conversions users have to make in order to operate with their variables.

Asume a .env file as follows:

  1. NON_VAR=None
  2. BOL_VAR=True
  3. INT_VAR=4
  4. FLT_VAR=5.18
  5. CPX_VAR=(2+3j)
  6. LST_VAR=[1,2]
  7. TPL_VAR=('1',2)
  8. DCT_VAR={'a':1,'b':2}
  9. STR_VAR='Just a string'

The following code would be valid with dotenvpy:

  1. import dotenvpy as env
  2. env.load_dotenv()
  3. # Without dotenvpy, this code would be quite larger
  4. #
  5. # At least a whole section with explicit type conversions and variable
  6. # assignations would have been necessary
  7. if env.NON_VAR is None and env.BOL_VAR:
  8. a = 9 % env.INT_VAR
  9. b = pow(env.FLT_VAR, 2)
  10. if env.CPX_VAR.real + 1 == env.CPX_VAR.imag:
  11. print(env.LST_VAR[1])
  12. print(env.TPL_VAR[0])
  13. if env.DCT_VAR['a'] + 1 == env.DCT_VAR['a']:
  14. print(env.STR_VAR)

Since ast is the module Python internally uses for its own interpretation, it has the same capabilities as the language literals themselves:

  • None
  • Booleans
  • Numbers: integers, floating-point, complex
  • Lists (of any nested literal types)
  • Tuples (of any nested literal types)
  • Dictionaries (of any nested literal types)
  • Strings

Features

  • All the capabilities of python-dotenv.
  • Attribute-like read access to environment variables.
  • Automatic conversion of the environment variables to their most suitable type.