Poyo, a Lightweight YAML Parser
Poyo is a lightweight YAML parser for Python (current version 0.5.0). It focuses on parsing a chosen subset of the YAML format, primarily designed for configuration files like those used by cookiecutter. It explicitly does not support deserialization of arbitrary Python objects, YAML serialization, or JSON compatibility. The project is in maintenance mode with infrequent updates, with the last release in July 2019, but it remains functional for its intended scope.
Common errors
-
poyo.PoyoException: Error parsing YAML: Line ... Column ...: Expected a mapping or sequence start
cause Attempting to parse YAML that uses features outside of Poyo's supported subset (e.g., YAML tags, anchors/aliases, or malformed syntax).fixReview the YAML content and simplify it to basic key-value pairs, lists, and scalars. If advanced YAML features are required, switch to a full-featured YAML parser like `PyYAML`. -
KeyError: 'some_key' or AttributeError: 'NoneType' object has no attribute 'get'
cause A YAML `null` value (represented by `~` or `null`) was not parsed as Python's `None`, but as a string, or a key was expected but not found due to a parsing error in older versions.fixThis often occurs in older versions of poyo. Ensure you are using `poyo>=0.3.0` to correctly handle `null` values. Also, verify that your YAML structure matches the expected Python dictionary access path. -
Error parsing YAML: Line ... Column ...: Could not parse scalar
cause This error can occur when Poyo encounters unexpected characters or structures, such as unquoted strings containing special characters or improper indentation, especially in older versions when list/comment handling was less robust.fixCheck for correct YAML indentation and ensure all strings with special characters or spaces are properly quoted. Consider upgrading to the latest Poyo version (`0.5.0`) as several parsing bugs were fixed in later releases.
Warnings
- gotcha Poyo only supports a *subset* of the YAML specification. It does not support arbitrary Python object deserialization, YAML serialization, or compatibility with JSON. Attempting to use advanced YAML features (e.g., anchors, tags, complex mappings) will likely result in parsing errors.
- gotcha The `~` character (YAML's representation for null) was not correctly recognized as `None` in Python before version 0.3.0, leading to unexpected string values.
- gotcha Earlier versions had bugs with parsing list items that included comments or blank lines, leading to incorrect or incomplete data structures.
- gotcha Multiline strings using the `>` or `|` block scalar indicators are only supported in version 0.5.0 and later.
Install
-
pip install poyo
Imports
- parse_string
from poyo import parse_string
- PoyoException
from poyo import PoyoException
Quickstart
from poyo import parse_string, PoyoException
yaml_string = """
default_context:
greeting: Hello
is_enabled: true
count: 123
items:
- apple
- banana
- null
long_description: >
This is a multiline string.
It demonstrates how to handle
text blocks introduced in 0.5.0.
"""
try:
config = parse_string(yaml_string)
print(config)
# Expected output similar to:
# {
# 'default_context': {
# 'greeting': 'Hello',
# 'is_enabled': True,
# 'count': 123,
# 'items': ['apple', 'banana', None]
# },
# 'long_description': 'This is a multiline string. It demonstrates how to handle text blocks introduced in 0.5.0.'
# }
except PoyoException as e:
print(f"Error parsing YAML: {e}")