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")
project = env.get("PROJECT")
db_name = env.get_str("DB_NAME")
db_user = env.get_str("DB_USER", "root")
db_pass = env.get_str("DB_PASS", "password")
db_host = env.get_str("DB_HOST", "localhost")
db_port = env.get_int("DB_PORT", 3306)
debug = env.get_bool("DEBUG", False)
sentry_dsn = 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.toml
file;override
:- if
os.environ
does not have the environment variables, it will load the environment variables from the.env.toml
file; - if
os.environ
already has the environment variables, andoverride=True
, it will override the environment variables with the values in the.env.toml
file; - if
os.environ
already has the environment variables, andoverride=False
, it will not override the environment variables with the values in the.env.toml
file;
- if
get
method
get(key: str, default=None, nullable=False) -> Any
key
: the key in the.env.toml
file;default
: the default value if the key is not found in the.env.toml
file;nullable
: if the key is not found in the.env.toml
file, and the default value isNone
, you should setnullable=True
, otherwise it will raiseTomlEnvError
exception;
get_str
method
get_str(key: str, default=None, nullable=False) -> str
key
: the key in the.env.toml
file;default
: the default value if the key is not found in the.env.toml
file;nullable
: if the key is not found in the.env.toml
file, and the default value isNone
, you should setnullable=True
, otherwise it will raiseTomlEnvError
exception;
get_int
method
get_int(key: str, default=None, nullable=False) -> int
key
: the key in the.env.toml
file;default
: the default value if the key is not found in the.env.toml
file;nullable
: if the key is not found in the.env.toml
file, and the default value isNone
, you should setnullable=True
, otherwise it will raiseTomlEnvError
exception;
get_float
method
get_float(key: str, default=None, nullable=False) -> float
key
: the key in the.env.toml
file;default
: the default value if the key is not found in the.env.toml
file;nullable
: if the key is not found in the.env.toml
file, and the default value isNone
, you should setnullable=True
, otherwise it will raiseTomlEnvError
exception;
get_bool
method
get_bool(key: str, default=None, nullable=False) -> bool
key
: the key in the.env.toml
file;default
: the default value if the key is not found in the.env.toml
file;nullable
: if the key is not found in the.env.toml
file, and the default value isNone
, you should setnullable=True
, otherwise it will raiseTomlEnvError
exception;
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.1.0.tar.gz
(3.3 kB
view details)
Built Distribution
File details
Details for the file pytomlenv-0.1.0.tar.gz
.
File metadata
- Download URL: pytomlenv-0.1.0.tar.gz
- Upload date:
- Size: 3.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.10.6 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e752f3ce86009d860b92133df19727926a131abb3680420de6a65b67984419d6 |
|
MD5 | 29eb2d00e2ea89a4345076e763c8df6b |
|
BLAKE2b-256 | cb76ac396bda2115ca1e70ff5887d8f1848a36535999b885c46575727132942b |
File details
Details for the file pytomlenv-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: pytomlenv-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.10.6 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 34294bcf53c40c89f4e52f25009ed49dca489b343cb2c4c89645509dc7e81a3b |
|
MD5 | 638b2caf11334819eac78c1001aaca91 |
|
BLAKE2b-256 | 79ac7f11c59b7d704a0aa1f479b3cb9fbaf82775a6e787bf361d74f4b59d7c84 |