Skip to main content

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.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, and override=True, it will override the environment variables with the values in the .env.toml file;
    • if os.environ already has the environment variables, and override=False, it will not override the environment variables with the values in the .env.toml file;

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 is None, you should set nullable=True, otherwise it will raise TomlEnvError 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 is None, you should set nullable=True, otherwise it will raise TomlEnvError 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 is None, you should set nullable=True, otherwise it will raise TomlEnvError 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 is None, you should set nullable=True, otherwise it will raise TomlEnvError 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 is None, you should set nullable=True, otherwise it will raise TomlEnvError exception;

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

pytomlenv-0.2.0.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

pytomlenv-0.2.0-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

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

Hashes for pytomlenv-0.2.0.tar.gz
Algorithm Hash digest
SHA256 9c3968b432b33514048ab033ba0be4583c6e17c09f93ed6ba95a9be9fd8524ff
MD5 1f78b2f1e1c97777960329b67daa26f5
BLAKE2b-256 367e8ba285af0f56ce88625702cba67c82d1c8b73ec1bc6332b4608dce423fef

See more details on using hashes here.

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

Hashes for pytomlenv-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8932c5bd8d77897ba0bea2f43f83db58cc7dc3fb48398da8d3d980c9fb12947d
MD5 cda91aee61d09af6a83865a7105acfc3
BLAKE2b-256 3e322df6931cfb4991a1a2ac3b100465ff285bc6db472cf8658cbb4d5caab882

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page