Skip to main content

Yet Another Python Envvar Config Object

Project description

yapeco

yapeco (Yet Another Python Envvar Config Object) is a tiny utility module to access .env (and otherwise environment-defined) config values in a structured way through a Python object. Created for the dual purpose of easily requiring/defaulting/casting environment variables at startup time for typical Python microservices & being able to autocomplete envvars in my editor.

Features & Limitations

  • Case-insensitive + snake-case (i.e. SNAKE_case) field names
  • Primitives such as str, bool, int and float are supported (no guarantees with float though, because, well... floating point)
  • Assuming use of the above primitives and enums, supports Optional[*] types (and by extension Union[*,None]), but no others from typing
  • Default values through class variable assignment; assumed to be None for optional types
  • Will (intentionally) raise a RuntimeError if there is no value set and no default value
  • Common boolean config formats (i.e. VAR=0/1/true/false/True/False) work as expected
  • Enums as well as str/int-literal unions are checked and parsed out
  • Unchecked JSON objects can be used too if you really want that for some reason lmao

Usage

Installation (PyPI):

pip3 install yapeco

Simple example:

# -- [mock environment] ---------------------------
API_KEY=abc123
DELAY_MSEC=18
FEATURE_A_ENABLED=false
FEATURE_B_ENABLED=1
VALUE=thing2
VALUE2=8
# --- contrived_config.py -------------------------

from enum import Enum, unique
from yapeco import BaseEnvironment as Env

@unique
class MyValue(Enum):
    THING1 = "thing1"
    THING2 = "thing2"

class Config(Env):
    api_key: str
    delay_msec: int
    feature_a_enabled: bool
    feature_a_flags: str = "-a -b -c"
    feature_b_enabled: bool
    feature_b_flags: Optional[str]
    value: MyValue
    value2: Literal["abc", "def", 7, 8]


# --- main.py -------------------------------------

from config import Config

Config.api_key # "abc123"
Config.delay_msec # 18
Config.feature_a_enabled # False
Config.feature_a_flags # "-a -b -c"
Config.feature_b_enabled # True
Config.feature_b_flags # None
Config.value # MyValue.THING1
Config.value2 # 7

# ...
# API_KEY=def456
# FEATURE_B_ENABLED=false

Config.refresh() # update environment

Config.api_key # "def456"
Config.feature_b_enabled # False

Development

Requires poetry.

poetry install  # install dependencies
poetry build  # build package
poetry run pytest .  # run tests
poetry run pyright .  # run type checks

Extra

Pedantic note:

As in Smalltalk, classes themselves are objects.

The Literal Python Documentation

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

yapeco-0.1.5.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

yapeco-0.1.5-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file yapeco-0.1.5.tar.gz.

File metadata

  • Download URL: yapeco-0.1.5.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.13.5 Darwin/24.1.0

File hashes

Hashes for yapeco-0.1.5.tar.gz
Algorithm Hash digest
SHA256 261aeab98e9daf49313d43bcf470b70c96c67cb208b549458df2e69b8c350b4a
MD5 00d18033453d7a5b0804d2bee21b10f3
BLAKE2b-256 370cf607c77c7b0aea520e21b3e6da6cf3ba43742eafe8570b92a8eef0be9e3b

See more details on using hashes here.

File details

Details for the file yapeco-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: yapeco-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.2 CPython/3.13.5 Darwin/24.1.0

File hashes

Hashes for yapeco-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 75eff14439c159fb7f50effd6f61fbf70f71f1fe4bb5b329f56cf9771c4eb674
MD5 52c68e68553db6ab3cb2c0a0708cdec0
BLAKE2b-256 9a65ef1ec374c4aa32a6c756aec2506988f3d498fae00020faf317ee41b4e4c1

See more details on using hashes here.

Supported by

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