Skip to main content

Pit-Viper is a Python package that offers configuration management capabilities like the Viper package in Golang.

Project description

pit-viper

Pit-Viper is a Python package that offers configuration management capabilities like the Viper package in Golang.

Table of Contents

  1. Installation

  2. Usage

  3. Development

Installation

You can install pit-viper using pip:

pip install pit-viper

Usage

Environment Variables

The pit-viper package provides the auto_env function that loads environment variables from a .env file. The function returns a dict of all existing environment variables.

from pit import viper

env = viper.auto_env()

We recommend accessing your environment variables via viper.get.

from pit import viper

# Bind environment variables
viper.auto_env()

bar = viper.get("foo")

With this, you could also useviper.set to specify default parameters up front.

Note: Changes to the environment following the first import of pit-viper will not be reflected in the package's record of environment variables.

Prefixes are commonly being used to prevent name collisions in environment variables. You can specify a prefix as follows:

from pit import viper

viper.set_env_prefix("pf")

# Bind environment variables 
viper.auto_env() # PF_FOO = "bar" 

bar = viper.get("foo") # "bar"

We provide you with the option to overwrite env keys when querying environment variables. For example, you might want to overwrite a . used for retrieving parameters from a config with a _ when accessing environment variables. This can be useful for creating a mapping between env keys and config keys.

from pit import viper

# Bind environment variables 
viper.auto_env() # MY_FOO = "bar" 

viper.set_env_key_replacer({".": "_"})

bar = viper.get("my.foo") # "bar"

Config Files

Config files are another config source for pit-viper. The package supports loading, accessing, and setting defaults for a config. Supported file formats are JSON, TOML, and YAML.

from pathlib import Path
from pit import viper

MY_CONFIG_DIR = Path() / "config"

viper.set_config_path(MY_CONFIG_DIR)
viper.set_config_name("my_config")
viper.set_config_type("toml")

viper.set("foo", "default-value")

viper.load_config()

bar = viper.get("foo")
nested_parameter = viper.get("my.nested.parameter")

Note: After loading the environment varibles with viper.auto_env, every call of viper.get will try to retrieve the parameter from the environment variables before performing a lookup in the parameters of the config file.

Development

We use Poetry for packaging and dependency management. We recommend creating your virtual environment in a directory named .venv in the project's root directory. This ensures that all scripts and git hooks relying on the Python environment will work properly. Therefore, after installing Poetry, consider running the command below to configure Poetry for the present project.

poetry config virtualenvs.in-project true --local 

Afterward, you can run the install command to install the dependencies (including development requirements) specified in the pyproject.toml or poetry.lock file.

poetry install

For a more comprehensive description of the Poetry setup and commands, see their documentation.

The pit-viper package is intended to be PEP 561 compatible. For this reason, we use a pre-commit hook that validates the code on every commit. The code validation runs tests and performs checks using Ruff, Mypy, and Pyright. Execute scripts/setup to add the validation as your pre-commit hook for this project.

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

pit_viper-1.1.0.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

pit_viper-1.1.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file pit_viper-1.1.0.tar.gz.

File metadata

  • Download URL: pit_viper-1.1.0.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.9 Linux/6.5.0-1025-azure

File hashes

Hashes for pit_viper-1.1.0.tar.gz
Algorithm Hash digest
SHA256 446c7e9953a5ec64990fc31a290722cd394970ab1878347c533c88b8cbb1a91b
MD5 16e5928355bb4669b177cce964495280
BLAKE2b-256 99838fb312104f9fd55232330eda3f0452578e9b63623cee5c35ce62837b8997

See more details on using hashes here.

File details

Details for the file pit_viper-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: pit_viper-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.11.9 Linux/6.5.0-1025-azure

File hashes

Hashes for pit_viper-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2677433a570ca0f307fde5767a736909ca81f0647155eb2b2167d1911004a26f
MD5 931f1a6ef62b5be59d47538e3bef542b
BLAKE2b-256 052ecb611d52121a12b66d5716a472d69c6f60ec6116e92b36e06aae08838405

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