YAML loader and dumper for PyYAML allowing to keep keys order.
Project description
DEPRECATED: the Phynix/yamlloader project provide an improved version of this library with unit tests, performance improvements (by providing access to the C implementation of PyYAML) and is more actively developed. You should use it!
This module provide a loader and a dumper for PyYAML allowing to keep items order when loading a file (by putting them in OrderedDict objects) and to manage OrderedDict objects when dumping to a file.
The loader is based on stackoverflow topic (thanks to Eric Naeseth): http://stackoverflow.com/questions/5121931/in-python-how-can-you-load-yaml-mappings-as-ordereddicts#answer-5121963
Self promotion: I use it a lot with clg, which allows to generate command-line definition from a configuration file, for keeping order of subcommands, options and arguments in the help message!
To install it
$ pip install yamlordereddictloader
Loader usage
import yaml
import yamlordereddictloader
data = yaml.load(open('myfile.yml'), Loader=yamlordereddictloader.Loader)
Note: For using the safe loader (which want standard YAML tags and does not construct arbitrary Python objects), replace yamlorderdictloader.Loader by yamlorderedictloader.SafeLoader.
Dumper usage
import yaml
import yamlordereddictloader
from collections import OrderedDict
data = OrderedDict([
('key1', 'val1'),
('key2', OrderedDict([('key21', 'val21'), ('key22', 'val22')]))
])
yaml.dump(
data,
open('myfile.yml', 'w'),
Dumper=yamlordereddictloader.Dumper,
default_flow_style=False)
Note: For using the safe dumper (which produce standard YAML tags and does not represent arbitrary Python objects), replace yamlorderdictloader.Dumper by yamlorderedictloader.SafeDumper.
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
Built Distribution
Hashes for yamlordereddictloader-0.4.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 36af2f6210fcff5da4fc4c12e1d815f973dceb41044e795e1f06115d634bca13 |
|
MD5 | c782a0fa8302a19651c2c3f12f92fff2 |
|
BLAKE2b-256 | 2378f853b0db6d8f3ea0ae4c648e4504ba376d024c139ba1292a256ce6180dd0 |
Hashes for yamlordereddictloader-0.4.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dc048adb67026786cd24119bd71241f35bc8b0fd37d24b415c37bbc8049f9cd7 |
|
MD5 | 37246c001984680615c07d702ff06824 |
|
BLAKE2b-256 | d3b664e84e26c52343dc48e9ffefd7d5e82b986f3bc2bd6da753420f41645718 |