Skip to main content

YAML document splitter, with iterator that returns document, start linenumber and optionally data

Project description

https://sourceforge.net/p/ruamel-yaml-split/code/ci/default/tree/_doc/_static/license.svg?format=raw https://sourceforge.net/p/ruamel-yaml-split/code/ci/default/tree/_doc/_static/pypi.svg?format=raw https://sourceforge.net/p/oitnb/code/ci/default/tree/_doc/_static/oitnb.svg?format=raw

This package provides a YAML document splitter, that allows you to iterate over a UTF-8 encoded file with YAML documents, and that returns each document, start linenumber and optionally loaded data.

This has the advantage over using load_all(), that you can skip individual documents that you know that don’t load or that you test for proper loading. E.g. R markdown files have a YAML header followed by a non-YAML body (of course it would have been much better if the header had been followed by --- | instead of only a directory-end-marker (---), so you could use any compliant YAML parser to load both)

You can use the following to get to each document:

from pathlib import Path
from ruamel.yaml.split import split

for doc, line_nr in split(Path('input.yaml')):
    print(doc.decode('utf-8'), line_nr)

The line_nr indicates the starting line of the document and can be used as an offset for e.g. errors that are generated when parsing the document.

You can also provide a YAML() instance to get the document loaded for you:

import ruamel.yaml

for doc, data, line_nr in split(Path('input.yaml'), yaml=ruamel.yaml.YAML()):
    print(doc.decode('utf-8'), data, line_nr)

By default split() splits on the line-ending after the document-end-marker (...), so that any comment on the line of the document-end-marker is part of the document before it. Using some constants imported from ruamel.yaml.split that you provide to the the cmnt parameter of split(), you can influence where the comments “between” documents are split. C_PRE adds any such comments to the preceding document, C_POST to the following document. C_SPLIT_ON_FIRST_BLANK, splits after the first blank line and assigns to both.

from ruamel.yaml.split import C_SPLIT_ON_FIRST_BLANK

for doc, data, line_nr in split(Path('input.yaml'), cmnt=C_SPLIT_ON_FIRST_BLANK):
    print(doc.decode('utf-8'), line_nr)

Comments at the end of the document stream are all attached to the last document, independent of the cmnt parameter.

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

ruamel.yaml.split-0.2.1.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

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

ruamel.yaml.split-0.2.1-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file ruamel.yaml.split-0.2.1.tar.gz.

File metadata

  • Download URL: ruamel.yaml.split-0.2.1.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.3.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.4

File hashes

Hashes for ruamel.yaml.split-0.2.1.tar.gz
Algorithm Hash digest
SHA256 17bfd054f1236418e3a3861514b384c205b17afb6aceaac11a43cf4c88801ef1
MD5 1079b87b47bd41930879d2b4c32c1531
BLAKE2b-256 fc19739676c95a3433cf0205c7a4dcde7132e02d6f33949864450de09151507c

See more details on using hashes here.

File details

Details for the file ruamel.yaml.split-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: ruamel.yaml.split-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.3.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.4

File hashes

Hashes for ruamel.yaml.split-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6dcba759fc470897a77c874a1ac33c3d430772d86cb8d83de182c598aa5d4ec7
MD5 e26330fbef594df93c2d5c6674d7186e
BLAKE2b-256 ee037f0b1c7aaa8fa726c9c648b1975fdac3b7d8078607e202ef23abef7a9118

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