simple utility to parse and validate environment variables
Project description
env_var
A simple utility for working with environment variables. The main goal was to provide proper type check.
Installation
pip install env-var
Usage
from env_var import env
minio_port = env('MINIO_PORT').as_port_number().default(9000).required() # port type hint is `int`
minio_host = env('MINIO_HOST').as_hostname().optional() # minio_host type hint is `str | None`
minio_secure = env('MINIO_SECURE').as_bool().required() # minio_host type hint is `bool`
Setting default
will result in always returning a value, so it makes little sense to use it with optional
.
It might be useful to gather all environment variables that are used in an application to a separate file.
from enum import Enum
from env_var import env
class SomeImportantOption(Enum):
option_a = "a"
option_b = "b"
option_c = "c"
PG_HOST = env('PG_HOST').as_hostname().required()
PG_PORT = env('PG_PORT').as_port_number().default(5432).required()
PG_DB = env('PG_DB').as_string().required()
PG_USER = env('PG_USER').as_string().required()
PG_PASSWORD = env('PG_PASSWORD').as_string().required()
IMPORTANT_OPTION = env('IMPORTANT_OPTION').as_enum(SomeImportantOption).required()
Sometimes it might happen that some variables will be required only in specific circumstances, in such cases calling required
can be postponed until the variable is actually needed.
UPDATE_URL = env('UPDATE_URL').as_url()
"""required only when some condition is met"""
# elsewhere in the code
def send_update(status: str):
url = UPDATE_URL.required()
requests.post(url, dict(status=status))
It's also possible to pass custom transformers/validators.
@dataclass
class MyOwnClass:
initial: str
initial_class = env("INITIAL").custom_transformer(MyOwnClass).required() # intial_class is of type MyOwnClass
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file env_var-1.0.1.tar.gz
.
File metadata
- Download URL: env_var-1.0.1.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.8.12 Linux/5.13.0-1021-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | de6e349902528086bb5642921b5795d752f43e20d047b27aca638fd6d030bfbf |
|
MD5 | 16b6b2df9fcea3ad3a19510801a0bfbc |
|
BLAKE2b-256 | 66c861855f4ec9ec249a39a01eb34e9682234090ac20f834c0e172fe2a10855e |
File details
Details for the file env_var-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: env_var-1.0.1-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.13 CPython/3.8.12 Linux/5.13.0-1021-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8e0062677de7f40ccb442f9f9531f7cccd23eece1fefeeca3c666365e496c87b |
|
MD5 | 7d5588f28da4c5a609222c7521dc287b |
|
BLAKE2b-256 | b6585da7eb65c4cdaa2679d2b3b7cc4262b5050e8c4167a526f679973a3d9ddd |