Skip to main content

Ordered YAML loader and dumper for PyYAML.

Project description

https://github.com/Phynix/yamlloader/actions/workflows/ci.yml/badge.svg https://img.shields.io/pypi/pyversions/yamlloader.svg https://badge.fury.io/py/yamlloader.svg https://coveralls.io/repos/github/Phynix/yamlloader/badge.svg

yamlloader

This module provides loaders and dumpers for PyYAML. Currently, an OrderedDict loader/dumper is implemented, allowing to keep items order when loading resp. dumping a file from/to an OrderedDict (Python 3.7+: Also regular dicts are supported and are the default items to be loaded to. As of Python 3.7 preservation of insertion order is a language feature of regular dicts.)

This project was originally mirrored from yamlordereddict Many thanks to the original author François Ménabé! The library contains several improvements including automated testing and the much faster C-versions of the Loaders/Dumpers.

API Documentation

Install

There is a pip and a conda version available

$ pip install yamlloader

or

$ conda install yamlloader -c conda-forge

But does [your special case here] also work?

Tests are run continuously using randomly generated yaml files. Also, there are no fails to be expected.

Still, if you are concerned that your special case may breaks in the future, please add your own tests as test_ext_anyname.py under tests/ or let us know about your needs. This guarantees that no code will be added that breaks your case.

C vs non-C version

A significant speedup can be reached by replacing the Loader* and Dumper* classes by CLoader* and CDumper*. The package hereby relies on the implementations from PyYAML. If they have not been compiled, yamlloader automatically falls back to the non-C versions.

Therefore using the C-version is safe: if it is not available, the pure Python version is automatically used.

Usage examples

Loader usage

import yaml
import yamlloader

with open('myfile.yml') as yaml_file:
    data = yaml.load(yaml_file,
                     Loader=yamlloader.ordereddict.CLoader)
                     # CLoader is faster than Loader

Note: For using the safe loader (which takes standard YAML tags and does not construct arbitrary Python objects), replace yamlloader.ordereddict.CLoader by yamlloader.ordereddict.CSafeLoader.

Dumper usage

import yaml
import yamlloader
from collections import OrderedDict

data = OrderedDict([('key1', 'val1'),
                    ('key2', OrderedDict([('key21', 'val21'),
                                          ('key22', 'val22')]))])

with open('myfile.yaml', 'w') as yaml_file:
    yaml.dump(data, yaml_file,
              Dumper=yamlloader.ordereddict.CDumper)

Note: For using the safe dumper (which produce standard YAML tags and does not represent arbitrary Python objects), replace yamlloader.ordereddict.CDumper by yamlloader.ordereddict.CSafeDumper.

FAQ

C version not working

If the C version is not working (it falls back by default to a non-C version), check if yaml.cyaml exists. If not, the cyaml module was not compiled during the installation of yaml (pyyaml). Make sure that cython is installed (pip install Cython) and the yaml.h file is there (apt: libyaml-dev).

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

yamlloader-1.5.1.tar.gz (77.2 kB view details)

Uploaded Source

Built Distribution

yamlloader-1.5.1-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file yamlloader-1.5.1.tar.gz.

File metadata

  • Download URL: yamlloader-1.5.1.tar.gz
  • Upload date:
  • Size: 77.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for yamlloader-1.5.1.tar.gz
Algorithm Hash digest
SHA256 8dece19b050acb1c6a8ca14aa30793388f9be154f734b826541f9a1828d41cec
MD5 a666496037c2b816a5009c1f36c4c769
BLAKE2b-256 6f6b7f094b4d9009794cb69dea18c99bdee4e62b4e0ee69bb19128178191f18a

See more details on using hashes here.

Provenance

The following attestation bundles were made for yamlloader-1.5.1.tar.gz:

Publisher: cd.yml on Phynix/yamlloader

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file yamlloader-1.5.1-py3-none-any.whl.

File metadata

  • Download URL: yamlloader-1.5.1-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for yamlloader-1.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 610014b14e25d7328d69f6526524d4616a552561f4c1b919f1282695bc1f4a11
MD5 1a004e138ce7050679089271593111c0
BLAKE2b-256 680f28323a9c418403cd4080d1736873c354bf058ad34306be5ff58639dcaedd

See more details on using hashes here.

Provenance

The following attestation bundles were made for yamlloader-1.5.1-py3-none-any.whl:

Publisher: cd.yml on Phynix/yamlloader

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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