Type-safe environment variable loading with casting and defaults.
Project description
philiprehberger-dotenv-cast
Type-safe environment variable loading with casting and defaults.
Installation
pip install philiprehberger-dotenv-cast
Usage
from philiprehberger_dotenv_cast import env, load_dotenv
# Load a .env file into os.environ
load_dotenv()
# Read variables with type casting
host = env.str("HOST", "localhost")
port = env.int("PORT", 8080)
debug = env.bool("DEBUG", False)
tags = env.list("TAGS") # "a,b,c" -> ["a", "b", "c"]
Boolean Casting
Boolean values are case-insensitive:
- Truthy:
"true","1","yes","on" - Falsy:
"false","0","no","off"
List Splitting
# Default separator is ","
tags = env.list("TAGS") # "a, b, c" -> ["a", "b", "c"]
# Custom separator
paths = env.list("PATHS", separator=":")
JSON Parsing
config = env.json("CONFIG") # '{"key": "value"}' -> {"key": "value"}
Missing Variables
Variables without a default raise MissingEnvError:
from philiprehberger_dotenv_cast import env, MissingEnvError
try:
secret = env.str("SECRET_KEY") # raises if not set
except MissingEnvError:
print("SECRET_KEY is required")
Loading .env Files
from philiprehberger_dotenv_cast import load_dotenv
# Load default .env
load_dotenv()
# Load a specific file
load_dotenv("config/.env.production")
API
| Method / Function | Description |
|---|---|
env.str(key, default?) |
Get variable as string |
env.int(key, default?) |
Get variable cast to int |
env.float(key, default?) |
Get variable cast to float |
env.bool(key, default?) |
Get variable cast to bool |
env.list(key, separator?, default?) |
Get variable split into a list |
env.json(key, default?) |
Get variable parsed as JSON |
load_dotenv(path?) |
Load a .env file into os.environ |
Env |
Class for creating custom instances |
MissingEnvError |
Raised when a required variable is missing |
Development
pip install -e .
python -m pytest tests/ -v
Support
If you find this project useful:
License
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file philiprehberger_dotenv_cast-0.2.0.tar.gz.
File metadata
- Download URL: philiprehberger_dotenv_cast-0.2.0.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
78b90cc0cc623e4dc28ac088a1d87c3365a37564566378dfa227aec0ecb6ed8d
|
|
| MD5 |
6d28bea64cc13e5a988af76ee1c7c8d2
|
|
| BLAKE2b-256 |
ec740d786913e39f7b39e3cc0cd92692f340bdaae25e60e59d7e9276b201fb5d
|
File details
Details for the file philiprehberger_dotenv_cast-0.2.0-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_dotenv_cast-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f9ac4b3ae2e53a05fdc3a81c97ff93699ad7abb54363e1e8db06f290535f37a2
|
|
| MD5 |
ea8e6c0d5ea42279d8bbfb9f2a80932f
|
|
| BLAKE2b-256 |
1ead87bfec6783126c8c0efc0c235279b9597caa6d367da963bd8fdf4215b628
|