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.
Installation
pip install compote
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!")
return value_
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
Loosen Python version requirements
This library should work with pretty much any version of Python but I will need to add a test matrix and verify that.
Pluggable "fail" exceptions
It'd be a nice touch if users could provide their own exception class to use if a required environment variable is not found.
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
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file compote-0.0.2.tar.gz.
File metadata
- Download URL: compote-0.0.2.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4435ce70234d86f8894528c1204570996c84d190bffad2ce8fc76cdddae04c44
|
|
| MD5 |
5ddb57295b0df9657ac27adee006371d
|
|
| BLAKE2b-256 |
1b095a305a37cdd1b841eab3a743e660379e6c23241928b32b88843fefc91cd0
|
File details
Details for the file compote-0.0.2-py3-none-any.whl.
File metadata
- Download URL: compote-0.0.2-py3-none-any.whl
- Upload date:
- Size: 4.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5bd9b39f598eb536682c989a27d0fd76fb8bdcd4f500e56e9beafe0a101f9725
|
|
| MD5 |
281d0628712c9fd1c8a2fe4296a3f5af
|
|
| BLAKE2b-256 |
36057cb472d2de14bc12262c484c47a62c1e9174945c26e233b9124fab4092d5
|