Write simple scripts leveraging omegaconf
Project description
AlphaConf
A small library to ease writing parameterized scripts. The goal is to execute a single script and be able to overwrite the parameters easily. The configuration is based on OmegaConf. Optionally, loading from toml or using pydantic is possible.
To run multiple related tasks, there is an integration with invoke. If you need something more complex, like running multiple instances of the script, take a look at hydra-core or use another script to launch multiple instances.
Demo and application
To run an application, you need...
# myapp.py
import alphaconf
import logging
# define the default values and helpers
alphaconf.setup_configuration({
"server.url": "http://default",
}, {
"server.url": "The URL to show here",
})
def main():
log = logging.getLogger()
log.info('server.url:', alphaconf.get('server.url'))
log.info('has server.user:', alphaconf.get('server.user', bool, default=False))
if __name__ == '__main__':
alphaconf.cli.run(main)
Invoking:
python myapp.py server.url=http://github.com
During an interactive session, you can set the application in the current context.
# import other modules
import alphaconf.interactive
alphaconf.interactive.mount()
alphaconf.interactive.load_configuration_file('path')
Check the DEMO for more examples.
How the configuration is loaded
When running a program, first dotenv is used to load environment variables
from a .env
file - this is optional.
Then configuration is built from:
- default configurations defined using (
alphaconf.setup_configuration
) application
key is generatedPYTHON_ALPHACONF
environment variable may contain a path to load- configuration files from configuration directories (using application name)
- environment variables based on key prefixes,
except "BASE" and "PYTHON";
if you have a configuration key "abc", all environment variables starting with "ABC_" will be loaded, for example "ABC_HELLO=a" would set "abc.hello=a" - key-values from the program arguments
Finally, the configuration is fully resolved and logging is configured.
Configuration templates and resolvers
Configuration values are resolved by OmegaConf. Some of the resolvers (standard and custom):
${oc.env:USER,me}
: resolve the environment variable USER with a default value "me"${oc.select:config_path}
: resolve to another configuration value${read_text:file_path}
: read text contents of a file asstr
${read_bytes:file_path}
: read contents of a file asbytes
${read_strip:file_path}
: read text contents of a file as strip spaces
The oc.select is used to build multiple templates for configurations
by providing base configurations.
An argument --select key=template
is a shortcut for
key=${oc.select:base.key.template}
.
So, logging: ${oc.select:base.logging.default}
resolves to the configuration
dict defined in base.logging.default and you can select it using
--select logging=default
.
Configuration values and integrations
Typed-configuration
You can use OmegaConf with pydantic to get typed values.
class MyConf(pydantic.BaseModel):
value: int = 0
def build(self):
# use as a factory pattern to create more complex objects
# for example, a connection to the database
return self.value * 2
# setup the configuration
alphaconf.setup_configuration(MyConf, prefix='a')
# read the value
alphaconf.get('a', MyConf)
v = alphaconf.get(MyConf) # because it's registered as a type
Secrets
When showing the configuration, by default configuration keys which are
secrets, keys or passwords will be masked.
You can read values or passwords from files, by using the template
${read_strip:/path_to_file}
or, more securely, read the file in the code
alphaconf.get('secret_file', Path).read_text().strip()
.
Inject parameters
We can inject default values to functions from the configuration. Either one by one, where we can map a factory function or a configuration key. Or inject all automatically base on the parameter name.
from alphaconf.inject import inject, inject_auto
@inject('name', 'application.name')
@inject_auto(ignore={'name'})
def main(name: str, example=None):
pass
# similar to
def main(name: str=None, example=None):
if name is None:
name = alphaconf.get('application.name', str)
if example is None:
example = alphaconf.get('example', default=example)
...
Invoke integration
Just add the lines below to parameterize invoke. Note that the argument parsing to overwrite configuration will work only when the script is directly called.
import alphaconf.invoke
ns = alphaconf.invoke.collection(globals())
alphaconf.setup_configuration({'backup': 'all'})
alphaconf.invoke.run(__name__, ns)
Way to 1.0
- Run a specific function
alphaconf my.module.main
: find functions and inject args - Install completions for bash
alphaconf --install-autocompletion
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
File details
Details for the file alphaconf-0.5.0.tar.gz
.
File metadata
- Download URL: alphaconf-0.5.0.tar.gz
- Upload date:
- Size: 31.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 153b57289355601d34eb5569ed5031b711204524c5598cf071ed49e0c20e8ce9 |
|
MD5 | ffa8226ceb7db770eb405636f5e5e55c |
|
BLAKE2b-256 | 65634d4244599ef94fb1f7bf35f55d2304067f3b742315ec8ebd9d1a9ce577b8 |
File details
Details for the file alphaconf-0.5.0-py3-none-any.whl
.
File metadata
- Download URL: alphaconf-0.5.0-py3-none-any.whl
- Upload date:
- Size: 25.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b61cc075ccfe07a7aaf22090d13cca5d546dfb571b47406d9e8809f0e45719b2 |
|
MD5 | 5ee560f98648690fbe2517009f4d7938 |
|
BLAKE2b-256 | ed56568a060f7a8da17a4cdbac3e291396ee13d422dd16e8de77a0cf1cbad1f1 |