Skip to main content

A configuration utility for Python progams

Project description

Compote

Rationale

Compote is a library which provides structure and utility for use when bootstrapping an application's common configuration class.

This is a pattern which consolidates application configuration in a single source of truth and prevents application components from having to reach out into the environment to read values at runtime, handle missing values, handle type casting of read values, etc. There are two core functions which read values from the environment: fetch_from_env_or_default and fetch_from_env_or_fail. These functions will be documented below but hopefully their names reveal their intentions.

This library also provides hooks for transformation functions (transform_env, transform_default and transform_value) to apply after reading value from the environment or setting defaults. These are useful for operations like casting value types and doing data validation.

A nice side effect of this consolidation is that it makes stubbing values into test environments simple. Instead of having to monkeypatch the environment, you can just provide a different Compote instance in your test setup.

Compote is a piece of code I've been carrying around and iterating on since around 2018. It was originally inspired by the Config class in Miguel Grinberg's infamous Flask tutorial.

Features

Reading from environment

Pull from env and fall back to default

class Config(Compote):
    GREETING = Compote.fetch_from_env_or_default("GREETING", "Hiya!")

Attempt to pull from env and raise a KeyError if not found

class Config(Compote):
    SOME_API_KEY = Compote.fetch_from_env_or_fail("SOME_API_KEY")

Value Transformation

transform_env

This function runs after the field has been set using an environment variable.

class Config(Compote):
    SOME_VALUE = Compote.fetch_from_env_or_default(
        "SOME_VALUE",
        111,
        transform_env=lambda x: int(x)
    )

transform_value

This function runs after the field has been set, regardless of whether the value came from the environment or the default.

def some_value_transformer(value):
    value_ = int(value)
    if value_ < 100:
        raise Exception(f"{value_} must be greater than 100!")

class Config(Compote):
    SOME_VALUE = Compote.fetch_from_env_or_default(
        "SOME_VALUE",
        111,
        transform_value=some_value_transformer
    )

transform_default

This function runs after the field has been set using a default variable. This may seem contrived but it can be useful if SOME_VALUE is computed using other Config fields and has proved useful in practice.

class Config(Compote):
    SOME_OTHER_VALUE = 111
    SOME_VALUE = Compote.fetch_from_env_or_default(
        "SOME_VALUE",
        "111",
        transform_default=lambda x: int(x) + Config.SOME_OTHER_VALUE
    )

Examples

Flask

See: examples/flask_app.py which can be run using:

GREETING=hola uv run --with-editable . examples/flask_app.py

Future Work

Logging

I find it helpful to know when default values are used and have historically used loguru to log a warning message when this happens. I don't want to add a dependency for anyone wishing to use this library, though, so I plan to add pluggable logging in a future release.

Types

I personally don't care for Python type hints (because they lie!) but I'll be a good steward and add them at some point.

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

compote-0.0.1.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

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

compote-0.0.1-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file compote-0.0.1.tar.gz.

File metadata

  • Download URL: compote-0.0.1.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.20

File hashes

Hashes for compote-0.0.1.tar.gz
Algorithm Hash digest
SHA256 02202861808b3bfe1bc2a3f65f7184131914153a2bffe0fc0e2647e0a2d5d128
MD5 96c6d359a77b044944c2fe67ec9cea98
BLAKE2b-256 8c4143866342de3073a6580153606adf6e37c1ab35fa57b0c388ce5cf0b8b07b

See more details on using hashes here.

File details

Details for the file compote-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: compote-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.20

File hashes

Hashes for compote-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ad1d641e7e600fc0bdec8d9103e5ba464d9279c6bb579c8f957a15e4c733065b
MD5 6a52dc718ac661071ca201f1aeca4db6
BLAKE2b-256 22fc1e74a5d546bde57f8260a2d29196575393bf46fba9e3036d21f1ae4d774b

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