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.

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

(DEMO)[./demo.ipynb]

To run an application, you need...

import alphaconf
# each module or application can declare the default configuration they need
# it will always be loaded first
alphaconf.setup_configuration("""
server:
  url: http://default
  user: ${oc.env:USER}
""")

def main():
    log = logging.getLogger()
    # get the DictConfig from the current application
    log.info('app name:', alphaconf.configuration().application.name)
    # shortcut to get an option as a dict, str, etc.
    log.info('server.user:', alphaconf.get('server.user'))

if __name__ == '__main__':
    # run the application
    alphaconf.Application(
        name='example',
        version='0.1',
    ).run(main)

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.

ns = Collection()  # define the invoke configuration
import alphaconf.invoke
alphaconf.setup_configuration({'backup': 'all'})
alphaconf.invoke.invoke_application(__name__, ns)

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
  • configuration files from the system (from your HOME and configuration directories)
  • 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 once 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.

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.

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.1.1.tar.gz (17.1 kB view details)

Uploaded Source

Built Distribution

alphaconf-0.1.1-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: alphaconf-0.1.1.tar.gz
  • Upload date:
  • Size: 17.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.11

File hashes

Hashes for alphaconf-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7c7542380c72219da044a27aef5c5fcff4106127fc4555a0845a844937e370f3
MD5 0906d29dddc2cfcf79a6190a03823ec4
BLAKE2b-256 49ad3fad9e4f648d4b61e92bb43bfe03a4d2551c400f05b6c4dbc6c474f9434b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: alphaconf-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.11

File hashes

Hashes for alphaconf-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 97605b29d747c086e4a0af8f7c8be5600663383a4d7ea0a9fb0452d5de665266
MD5 aaf36db6e905a7d10dcfbe2f2cd00002
BLAKE2b-256 fe9ffbbdcf4e35579e174922021ea530ba43309d512352d3e004f8ec4239be89

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