Registry / serialization / donfig

donfig

JSON →
library0.8.1.post1pypypi✓ verified 31d ago

Donfig is a Python library designed to simplify package and script configuration, drawing inspiration from the configuration logic originally found in the Dask library. It allows configuration through programmatic settings, environment variables, and YAML files located in standard paths. The library is actively maintained, with the current version being 0.8.1.post1, and releases occurring periodically to add features and fix bugs.

pip install donfig
INSTALL
IMPORT
SIG · DONFIG
D
donfig
serializationpythonv0.8.1.post1
Install
1.7s avg
Import
197ms
Disk
18MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v0.8.1.post1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.198s · 20.1MB
glibc
py 3.10–3.95 runs
installs and imports cleanly · install 1.7s · import 0.196s · 21MB
18MB installed
● package 18MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Config
✓ from donfig import Config

Initialize a `Config` object with a unique name, which it uses to locate environment variables and YAML files. Access settings using `config.get()` and update them with `config.set()`. The `set()` method can also be used as a context manager for temporary changes. Environment variables take precedence over YAML files, which in turn take precedence over programmatic defaults.

import os from donfig import Config # Simulate environment variable for demonstration os.environ['MYAPP_SETTING_ONE'] = 'env_value' # Create a configuration object for your application/package # The name ('myapp' here) is used for environment variable prefixing (e.g., MYAPP_) # and YAML file searching (e.g., ~/.config/myapp/) config = Config('myapp', defaults={'setting_one': 'default_value', 'setting_two': 123}) # Access configuration values value_one = config.get('setting_one') value_two = config.get('setting_two') print(f"Setting One (from env): {value_one}") print(f"Setting Two (from default): {value_two}") # Update configuration programmatically config.set(setting_two=456) print(f"Setting Two (updated): {config.get('setting_two')}") # Use as a context manager to temporarily change configuration with config.set(setting_one='context_value'): print(f"Setting One (in context): {config.get('setting_one')}") print(f"Setting One (after context): {config.get('setting_one')}") # Clean up environment variable (optional, for isolated testing) del os.environ['MYAPP_SETTING_ONE']
Debug
Known issues
breakingThe `update_defaults` method in `Config` objects changed behavior in v0.8.0. Previously, it might not have consistently overridden existing default values. As of v0.8.0, `update_defaults` will reliably override old default values, which might change behavior if your code relied on the previous 'buggy' non-overriding behavior.
fix
Review calls to `config.update_defaults()` and ensure the intended merging/overriding logic matches the new behavior. If preserving old defaults is crucial, manually check for existence before updating.
affects: >=0.8.0
gotchaWhen loading configuration from environment variables, Donfig uses `ast.literal_eval` to parse values. This means that environment variable values are interpreted as Python literals (e.g., 'True' becomes `True` boolean, '123' becomes `123` integer, '[1, 2]' becomes a list). This can lead to unexpected type conversions if not accounted for.
fix
Be aware of the `ast.literal_eval` parsing when setting environment variables. Ensure values are formatted correctly as Python literals if specific types (like booleans, numbers, lists, dictionaries) are expected. For string values that should not be evaluated, ensure they are quoted or otherwise treated as plain strings by your application after retrieval if `ast.literal_eval` causes issues.
affects: All versions
gotchaWhen setting configuration values using `config.set()`, underscores (`_`) and hyphens (`-`) in key names are treated as identical. For example, `config.set({'my-key': True})` is equivalent to `config.set({'my_key': True})`.
fix
Maintain consistency in your key naming convention (either all hyphens or all underscores) to avoid confusion. If you use both, remember they will resolve to the same internal key.
affects: All versions
deprecatedVersion 0.8.0 introduced support for key deprecation. While not a direct breaking change for existing configurations, this means that future releases of packages using donfig might mark certain configuration keys as deprecated. It's advisable to check documentation for specific applications using donfig to see if any keys you rely on have been deprecated.
fix
Monitor application-specific documentation for deprecation warnings related to configuration keys. Update your configurations as recommended to use newer, non-deprecated keys.
affects: >=0.8.0
breakingWhen initializing `Config` objects or refreshing configurations, an internal `AttributeError: 'str' object has no attribute 'items'` can occur if a configuration source provides a string instead of a dictionary-like object. This happens during the internal `update` process, which expects to iterate over dictionary items. This indicates a breaking change in how configuration sources are handled or what they are expected to contain.
fix
Ensure all configuration sources (e.g., values passed to `Config` constructor, loaded from files, or environment variables) are dictionary-like objects. Review how configurations are loaded and processed to prevent strings from being mistakenly treated as dictionaries in the internal update logic. If string values are intended as configuration, they might need to be wrapped in a dictionary or processed differently before being passed to `donfig`.
affects: >=0.8.0
breakingAn `AttributeError: 'str' object has no attribute 'items'` occurs during `Config` object initialization within the `refresh` method. This happens when the internal `update` function, which expects a dictionary to merge, receives a string instead. This indicates a configuration source (e.g., an environment variable, a file's content, or a manually provided source) is being interpreted or passed as a string where a dictionary is expected, leading to a type mismatch during configuration merging.
fix
Ensure all configuration sources (defaults, files, environment variables, etc.) that are intended to be merged by `donfig` are correctly formatted and parsed as dictionary-like objects before or during their loading. If a source unexpectedly yields a string, investigate how it's being produced and ensure it's converted to a dictionary or handled appropriately by Donfig's parsing mechanisms (e.g., by ensuring JSON strings are parsed as JSON objects).
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'donfig'
The 'donfig' package is not installed in the Python environment where the code is being run, or the environment is not correctly activated.
fix
Install the package using pip: `pip install donfig`
AttributeError: 'Config' object has no attribute 'my_setting'
You are attempting to access a configuration key using attribute-style access (e.g., `config.my_setting`) that has not been defined in any of the loaded configuration sources (YAML files, environment variables, or programmatic defaults).
fix
Ensure the 'my_setting' key is defined in a YAML file, as an environment variable (e.g., `MYPKG_MY__SETTING`), or explicitly set in the `Config` object, for example: `config = Config('mypkg', defaults={'my_setting': 'default_value'})`
yaml.scanner.ScannerError: while scanning a simple key
A YAML configuration file used by donfig has a syntax error, such as incorrect indentation, a missing colon, or an invalid structure, preventing it from being parsed correctly.
fix
Carefully review the YAML file for syntax errors, paying close attention to indentation and proper key-value pair formatting. A YAML linter can help identify issues.
KeyError: 'some_other_setting'
When accessing configuration values using dictionary-style lookup (e.g., `config['some_other_setting']`), this error occurs if the specified key does not exist in the loaded configuration.
fix
Define the key in your configuration sources, or use the `.get()` method for safe access with a default value, e.g., `value = config.get('some_other_setting', 'default_value')`.
Upgrade
Version history
0.8.1.post1latest on PyPI · released May 23, 2024
Audit
Dependencies
pyyamlrequiredRequired for parsing configuration from YAML files.
Agent activity
14 hits · last 30 days
node
14
Resources
donfig — pip install donfig · libregistry