Skip to main content

Enables self referential yaml entries

Project description

Build Status

dynamic-yaml

Dynamic YAML is a couple of classes and functions that add extra functionality to YAML that turns it into a great configuration language for Python. If you prefer JSON, then see dynamic-json.

YAML already provides:

  • A very readable and clean syntax
  • Infinitely nestable key:value pairs
  • Sequence types
  • A regulated portable syntax that conforms to strict standards

In addition, the PyYAML parser provides:

  • Automatic type identification (a result of implementing the YAML standard)

Finally, the classes introduced by Dynamic YAML enable:

  • Dynamic string resolution

Dynamic PyYAML requires PyYAML (https://bitbucket.org/xi/pyyaml).

Usage

The key feature that was introduced is the ability for a string scalar to reference other parts of the configuration tree. This is done using the Python string formatting syntax. The characters '{' and '}' enclose a reference to another entry in the configuration structure. The reference takes the form key1.key2 where key1 maps to another mapping object and can be found in the root mapping, and key2 can be found in key1's mapping object. Multiple levels of nesting can be used (eg. key1.key2.key3 etc...).

An example yaml configuration:

project_name: hello-world
dirs:
    home: /home/user
    venv: "{dirs.home}/venvs/{project_name}"
    bin: "{dirs.venv}/bin"
    data: "{dirs.venv}/data"
    errors: "{dirs.data}/errors"
    sessions: "{dirs.data}/sessions"
    databases: "{dirs.data}/databases"
    output: "{dirs.data}/output-{parameters.parameter1}-{parameters.parameter2}"
exes:
    main: "{dirs.bin}/main"
    test: tests
parameters:
    parameter1: a
    parameter2: b

Reading in a yaml file:

import dynamic_yaml

with open('/path/to/file.yaml') as fileobj:
    cfg = dynamic_yaml.load(fileobj)
    assert cfg.dirs.venv == '/home/user/venvs/hello-world'
    assert cfg.dirs.output == '/home/user/venvs/hello-world/data/output-a-b'

As the variables are dynamically resolved, it is also possible to combine this with argparse:

import dynamic_yaml

from argparse import ArgumentParser

with open('/path/to/file.yaml') as fileobj:
    cfg = dynamic_yaml.load(fileobj)
    parser = ArgumentParser()
    parser.add_argument('--parameter1')
    parser.add_argument('--parameter2')
    parser.parse_args('--parameter1 c --parameter2 d'.split(), namespace=cfg.parameters)
    assert cfg.dirs.output == '/home/user/venvs/hello-world/data/output-c-d'

Writing yaml will resolve all references:

import dynamic_yaml
import yaml

with open('/path/to/file.yaml') as fileobj:
    cfg = dynamic_yaml.load(fileobj)
    assert yaml.safe_load(dynamic_yaml.dump(cfg)) == yaml.safe_load('''
project_name: hello-world
dirs:
    home: /home/user
    venv: /home/user/venvs/hello-world
    bin: /home/user/venvs/hello-world/bin
    data: /home/user/venvs/hello-world/data
    errors: /home/user/venvs/hello-world/data/errors
    sessions: /home/user/venvs/hello-world/data/sessions
    databases: /home/user/venvs/hello-world/data/databases
    output: /home/user/venvs/hello-world/data/output-a-b}
exes:
    main: /home/user/venvs/hello-world/bin/main
    test: tests
parameters:
  - 0.5
  - 0.1
''')

Installation

To install, simply run:

pip install dynamic-yaml

Restrictions

Due to the short amount of time I was willing to spend on working upon this, there are a few restrictions that I could not overcome.

  • Wild card strings must be surrounded by quotes. Braces ('{' and '}') in a YAML file usually enclose a mapping object. However, braces are also used by the Python string formatting syntax to enclose a reference. As there is no way to change either of these easily, strings that look like a yaml mapping must be explicitly declared using single or double quotes to enclose them. For example:
    quotes_needed: '{variable}'
    
  • Certain keys can only be used via __getitem__ and not __getattr__. Because dict comes with it's own set of attributes that are always resolved first, the values for the following keys must be gotten using the item getter rather than the attribute getter (eg. config['items'] vs. config.items):
    • append
    • extend
    • insert
    • remove
    • pop
    • clear
    • index
    • count
    • sort
    • reverse
    • copy

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

dynamic-yaml-1.3.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dynamic_yaml-1.3.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file dynamic-yaml-1.3.0.tar.gz.

File metadata

  • Download URL: dynamic-yaml-1.3.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for dynamic-yaml-1.3.0.tar.gz
Algorithm Hash digest
SHA256 c94cbd4cbb37a5905d0414aa343564f48547df004d3c27c54b6f2a0430c7480b
MD5 46019ada9fccf300950d1fef25c8b498
BLAKE2b-256 3869209a0e2999b843f820b61015f769a44ff1efd69993afc6d847fdc8b56a4c

See more details on using hashes here.

File details

Details for the file dynamic_yaml-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: dynamic_yaml-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for dynamic_yaml-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9788190611b790889ab93a1def820b1f342d445c1579459e9299cd3f412ec180
MD5 5123125bf69c86924cd4d24d8fcd522a
BLAKE2b-256 61fa3a230401b262fe4d52292fbdd50dc8a47fb088eed058875ad8a3b0ca1b23

See more details on using hashes here.

Supported by

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