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

To run an application, you need...

import alphaconf
# each module or application can declare the default configuration they need
# it will always be loaded before application startup
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.get().application.name)
    # shortcut to get an option as a dict, str, etc.
    log.info('server.user:', alphaconf.get('server.user'))
    log.info('has server.user:', alphaconf.get('server.user', bool))

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

During an interactive session, you can set the application in the current context.

# import other modules
import alphaconf
app = alphaconf.Application()
app.setup_configuration(configuration_paths=[])  # load other files
alphaconf.set_application(app)

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 configuration directories (base on 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.

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.

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

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

Uploaded Source

Built Distribution

alphaconf-0.3.1-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for alphaconf-0.3.1.tar.gz
Algorithm Hash digest
SHA256 32c20c0076ab02cc2a9b36795a5b7bbd2c304638761af14e579bba92778b4458
MD5 f396086ca405518f593869e733211cd1
BLAKE2b-256 d3d2c2ec823383b579e0423ef2b71b30b4e38ded9b81be9a0efe9881cb558e40

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for alphaconf-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9b993a7e1bce2e4d86fcfd150bbf876799830674d052cc23a6ad5471a4015001
MD5 dbfd65975345e6cd75c0c9628904af89
BLAKE2b-256 f649948a9cf65eeeb5429de4cbf29ba677f409855065ec5d02e772c75832dd56

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