Skip to main content

YAML loader and dump for PyYAML allowing to keep keys order.

Project description

License Versions PyPi Code repo Code Health

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):

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')]))
    open('myfile.yml', 'w'),

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

yamlordereddictloader-0.4.0.tar.gz (3.3 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page