Package: dynamic-yaml
dynamic-yamlDynamic 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 seedynamic-json.YAML already provides:A very readable and clean syntaxInfinitely nestable key:value pairsSequence typesA regulated portable syntax that conforms to strict standardsIn 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 resolutionDynamic PyYAML requires PyYAML (https://bitbucket.org/xi/pyyaml).UsageThe 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...).
If you need brace literals, they can be escaped by doubling them up, as described by the Python format stringdocumentation.An example yaml configuration:project_name:hello-worlddirs:home:/home/uservenv:"{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:testsparameters:parameter1:aparameter2:bReading in a yaml file:importdynamic_yamlwithopen('/path/to/file.yaml')asfileobj:cfg=dynamic_yaml.load(fileobj)assertcfg.dirs.venv=='/home/user/venvs/hello-world'assertcfg.dirs.output=='/home/user/venvs/hello-world/data/output-a-b'As the variables are dynamically resolved, it is also possible to combine this withargparse:importdynamic_yamlfromargparseimportArgumentParserwithopen('/path/to/file.yaml')asfileobj: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)assertcfg.dirs.output=='/home/user/venvs/hello-world/data/output-c-d'Writing yaml will resolve all references:importdynamic_yamlimportyamlwithopen('/path/to/file.yaml')asfileobj:cfg=dynamic_yaml.load(fileobj)assertyaml.safe_load(dynamic_yaml.dump(cfg))==yaml.safe_load('''project_name: hello-worlddirs:home: /home/uservenv: /home/user/venvs/hello-worldbin: /home/user/venvs/hello-world/bindata: /home/user/venvs/hello-world/dataerrors: /home/user/venvs/hello-world/data/errorssessions: /home/user/venvs/hello-world/data/sessionsdatabases: /home/user/venvs/hello-world/data/databasesoutput: /home/user/venvs/hello-world/data/output-a-b}exes:main: /home/user/venvs/hello-world/bin/maintest: testsparameters:- 0.5- 0.1''')InstallationTo install, simply run:pipinstalldynamic-yamlRestrictionsDue 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__.Becausedictcomes 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):appendextendinsertremovepopclearindexcountsortreversecopy