Skip to main content

Working with YAML format

Project description

Coverage Status Total alerts Language grade: Python

vutils-yaml: Working with YAML Format

This package provides tools for loading data from and saving data to the YAML format. Features:

  • It annotates data while loading them from the YAML format. Every object returned by vutils.yaml.load.load_yaml has information about its origin. This is useful when reporting errors.

Limitations:

  • Due to the annotating data, this packages is not suitable for loading YAML formatted data containing thousands and hundreds of thousands items. For such amounts of data use PyYAML or other YAML parser since it is supposed that large data sets are usually machine-generated and consistent so further verification is not required.
  • When used for the first time, vutils.yaml.utils.keyloc function builds a mapping between dict keys and their locations. This can be a bottle-neck for large dictionaries.

Due to the limitations, this package is suitable for parsing human-written configurations in YAML format containing at most hundreds of items.

Installation

pip install vutils-yaml

How to Use

Topic covered in this short guide is:

  • loading annotated YAML data
  • API reference

Loading Annotated YAML Data

To load YAML data with annotations, use the load_yaml function from vutils.yaml.load. Example:

from vutils.yaml.load import load_yaml
from vutils.yaml.utils import is_null, keyloc

stream = """
---
food:
  fruit:
    - apple
    - banana
    - orange
  vegetable:
    - potato
    - tomato
    - carrot
  meat: null
"""

data = load_yaml(stream)
food = data["food"]
# `if food["meat"] is None` will not work, `null` is converted to `NullType`
# object to be annotated
if is_null(food["meat"]):
    # `keyloc` retrieves the annotated key object from `food` and return its
    # location
    print(f"{keyloc(food, 'meat')}: At least one kind of meat is required.")

Observe the testing if the value is null and the retrieving the meat key location to inform a user where the problem with his/her data is.

API Reference

Module vutils.yaml.load provide these functions:

  • load_yaml(stream) loads YAML data from stream and annotates them. stream can be str, bytes, or a file-like object supporting read.

Module vutils.yaml.utils provide these functions:

  • getloc(obj) retrieves the location of obj
  • keyloc(obj, kobj) retrieves the location of kobj, which is a key of dict-like object obj.
  • is_null(obj) tests whether obj is null.
  • is_bool(obj) tests whether obj is of the Boolean type.

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

vutils-yaml-0.1.1.tar.gz (16.0 kB view details)

Uploaded Source

Built Distribution

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

vutils_yaml-0.1.1-py2.py3-none-any.whl (9.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file vutils-yaml-0.1.1.tar.gz.

File metadata

  • Download URL: vutils-yaml-0.1.1.tar.gz
  • Upload date:
  • Size: 16.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for vutils-yaml-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ce52bc93b1bede9f41057a856c981f4894da0dcea1baf2804f3391e9c2a76d32
MD5 e914b1e080204796b13e6e7ab12c2b39
BLAKE2b-256 978e5d6bc75dbb003dee28060249ab48965c6756f96f3d0ae38caa2387f904ff

See more details on using hashes here.

File details

Details for the file vutils_yaml-0.1.1-py2.py3-none-any.whl.

File metadata

  • Download URL: vutils_yaml-0.1.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for vutils_yaml-0.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 db270f250869c5368d29338fb2f5d5e3b71522da27e5a1fd92ab60f4c828c3f8
MD5 1f87ee343c1a01e7999f4805a8ef5639
BLAKE2b-256 431e3fdef0e0c4b38c5bcbec3858387b58a5f448ff35543fd635f255b9c88034

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