Environment in TOML format.
Project description
pytomlenv
Environment in TOML for Python.
Install
pip install pytomlenv
Usage
.env.toml
[production]
DEBUG = false
DB_ENGINE = "django.db.backends.postgresql"
DB_NAME = "ragx"
DB_USER = "postgres"
DB_PASSWORD = "123456"
DB_HOST = "10.10.0.100"
DB_PORT = 5432
[development]
DEBUG = true
DB_ENGINE = "django.db.backends.sqlite3"
DB_NAME = "ragx.db"
DB_USER = ""
DB_PASSWORD = ""
DB_HOST = ""
DB_PORT = 0
[test]
DEBUG = true
DB_ENGINE = "django.db.backends.mysql"
DB_NAME = "ragx"
DB_USER = "root"
DB_PASSWORD = "123456"
DB_HOST = "127.0.0.1"
DB_PORT = 3306
your python code:
from toml_env import load_env
env = load_env(".env.toml")
# `os.environ.get()` also works, but it always return string
# you have better choice with the following methods:
project: str = env.get("PROJECT")
db_name: str = env.get_str("DB_NAME")
db_user: str = env.get_str("DB_USER", "root")
db_pass: str = env.get_str("DB_PASS", "password")
db_host: str = env.get_str("DB_HOST", "localhost")
db_port: int = env.get_int("DB_PORT", 3306)
debug: bool = env.get_bool("DEBUG", False)
sentry_dsn: str = env.get_str("SENTRY_DSN", None, nullable=True)
print(debug, db_name, db_user, db_pass, db_host, db_port, sentry_dsn)
run your python code:
$ export ENV=development
$ python your_code.py
Methods
load_env function
def load_env(path: str = ".env.toml", override: bool = False)
path: the path to the.env.tomlfile;override:- if
os.environdoes not have the environment variables, it will load the environment variables from the.env.tomlfile; - if
os.environalready has the environment variables, andoverride=True, it will override the environment variables with the values in the.env.tomlfile; - if
os.environalready has the environment variables, andoverride=False, it will not override the environment variables with the values in the.env.tomlfile;
- if
get method
get(key: str, default=None, nullable=False) -> Anykey: the key in the.env.tomlfile;default: the default value if the key is not found in the.env.tomlfile;nullable: if the key is not found in the.env.tomlfile, and the default value isNone, you should setnullable=True, otherwise it will raiseTomlEnvErrorexception;
get_str method
get_str(key: str, default=None, nullable=False) -> strkey: the key in the.env.tomlfile;default: the default value if the key is not found in the.env.tomlfile;nullable: if the key is not found in the.env.tomlfile, and the default value isNone, you should setnullable=True, otherwise it will raiseTomlEnvErrorexception;
get_int method
get_int(key: str, default=None, nullable=False) -> intkey: the key in the.env.tomlfile;default: the default value if the key is not found in the.env.tomlfile;nullable: if the key is not found in the.env.tomlfile, and the default value isNone, you should setnullable=True, otherwise it will raiseTomlEnvErrorexception;
get_float method
get_float(key: str, default=None, nullable=False) -> floatkey: the key in the.env.tomlfile;default: the default value if the key is not found in the.env.tomlfile;nullable: if the key is not found in the.env.tomlfile, and the default value isNone, you should setnullable=True, otherwise it will raiseTomlEnvErrorexception;
get_bool method
get_bool(key: str, default=None, nullable=False) -> boolkey: the key in the.env.tomlfile;default: the default value if the key is not found in the.env.tomlfile;nullable: if the key is not found in the.env.tomlfile, and the default value isNone, you should setnullable=True, otherwise it will raiseTomlEnvErrorexception;
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
pytomlenv-0.2.0.tar.gz
(3.1 kB
view details)
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 pytomlenv-0.2.0.tar.gz.
File metadata
- Download URL: pytomlenv-0.2.0.tar.gz
- Upload date:
- Size: 3.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.12.1 Linux/6.5.0-1025-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9c3968b432b33514048ab033ba0be4583c6e17c09f93ed6ba95a9be9fd8524ff
|
|
| MD5 |
1f78b2f1e1c97777960329b67daa26f5
|
|
| BLAKE2b-256 |
367e8ba285af0f56ce88625702cba67c82d1c8b73ec1bc6332b4608dce423fef
|
File details
Details for the file pytomlenv-0.2.0-py3-none-any.whl.
File metadata
- Download URL: pytomlenv-0.2.0-py3-none-any.whl
- Upload date:
- Size: 3.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.12.1 Linux/6.5.0-1025-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8932c5bd8d77897ba0bea2f43f83db58cc7dc3fb48398da8d3d980c9fb12947d
|
|
| MD5 |
cda91aee61d09af6a83865a7105acfc3
|
|
| BLAKE2b-256 |
3e322df6931cfb4991a1a2ac3b100465ff285bc6db472cf8658cbb4d5caab882
|