Skip to main content

Write simple scripts leveraging omegaconf

Project description

AlphaConf

PyPI version

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 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))

if __name__ == '__main__':
    alphaconf.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 generated
  • PYTHON_ALPHACONF may contain a path to a configuration file
  • 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 where keys are converted to lower case and "_" to ".": "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

Omegaconf's resolvers may be used as configuration values. For example, ${oc.env:USER,me} would resolve to the environment variable USER with a default value "me". Similarly, ${oc.select:path} will resolve to another configuration value.

Additional resolvers are added to read file contents. These are the same as type casts: read_text, read_strip, read_bytes.

The 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 dataclasses to specify which values are enforced in the configuration. Alternatively, the get method can receive a data type or a function which will parse the value. By default, bool, str, Path, DateTime, etc. are supported.

Secrets

When showing the configuration, by default configuration keys which are secrets, keys or passwords will be masked. Another good practice is to have a file containing the password which you can retrieve using alphaconf.get('secret_file', 'read_strip').

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)

Interactive and manual usage

Use alphaconf.interactive.mount() or load manually create an alphaconf.Application, configure it and set it.

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

alphaconf-0.4.2.tar.gz (26.4 kB view details)

Uploaded Source

Built Distribution

alphaconf-0.4.2-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

Details for the file alphaconf-0.4.2.tar.gz.

File metadata

  • Download URL: alphaconf-0.4.2.tar.gz
  • Upload date:
  • Size: 26.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.1

File hashes

Hashes for alphaconf-0.4.2.tar.gz
Algorithm Hash digest
SHA256 4ff21e4b90d472cf45f9fc5cdc0ced48721903af1809a26c3fbd00ee82c456c3
MD5 678d7fcde926d8aa45ebe21f35c44b51
BLAKE2b-256 d3221a9179c080af9d0101b79ce2f36faa097e1b2874f8f137d03f6a358ce357

See more details on using hashes here.

File details

Details for the file alphaconf-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: alphaconf-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 19.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.1

File hashes

Hashes for alphaconf-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 66d18f7a37d9dc571dd806f7eddcb7cecf57fdab5812b8e64611a51d762f372d
MD5 6a716b799587db05c4da8856b538663d
BLAKE2b-256 d9dd4f99b8a02e648ea55f6d68b92c1b9782db9126676c12061b90dabef791e3

See more details on using hashes here.

Supported by

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