Skip to main content

Python bindings for YAMLStar - YAML 1.2 loader

Project description

YAMLStar Python Bindings

Python bindings for YAMLStar - a pure YAML 1.2 loader implemented in Clojure.

Features

  • YAML 1.2 Spec Compliance: 100% compliant with YAML 1.2 core schema
  • Pure Implementation: No dependencies on SnakeYAML or other external parsers
  • Fast Native Performance: Uses GraalVM native-image shared library
  • Simple API: Load YAML documents with a single function call
  • Multi-Document Support: Load multiple YAML documents from a single string

Installation

Prerequisites

First, build and install the shared library:

cd ../libyamlstar
make native
sudo make install PREFIX=/usr/local

Or install to user-local directory:

cd ../libyamlstar
make native
make install PREFIX=~/.local

Install Python Package

pip install .

For development:

pip install -e .

Quick Start

import yamlstar

# Create a YAMLStar instance
ys = yamlstar.YAMLStar()

# Load a simple YAML string
data = ys.load("key: value")
print(data)  # {'key': 'value'}

Usage Examples

Basic Types

import yamlstar

ys = yamlstar.YAMLStar()

# Strings
ys.load("hello")  # 'hello'

# Integers
ys.load("42")  # 42

# Floats
ys.load("3.14")  # 3.14

# Booleans
ys.load("true")   # True
ys.load("false")  # False

# Null
ys.load("null")  # None

Collections

# Mappings (dictionaries)
data = ys.load("""
name: Alice
age: 30
city: Seattle
""")
# {'name': 'Alice', 'age': 30, 'city': 'Seattle'}

# Sequences (lists)
data = ys.load("""
- apple
- banana
- orange
""")
# ['apple', 'banana', 'orange']

# Flow style
data = ys.load("[a, b, c]")
# ['a', 'b', 'c']

Nested Structures

data = ys.load("""
person:
  name: Alice
  age: 30
  hobbies:
    - reading
    - coding
    - hiking
""")
# {
#   'person': {
#     'name': 'Alice',
#     'age': 30,
#     'hobbies': ['reading', 'coding', 'hiking']
#   }
# }

Multi-Document YAML

# Load all documents from a multi-document YAML string
docs = ys.load_all("""---
name: Document 1
---
name: Document 2
---
name: Document 3
""")
# [
#   {'name': 'Document 1'},
#   {'name': 'Document 2'},
#   {'name': 'Document 3'}
# ]

Type Coercion

YAMLStar follows YAML 1.2 core schema type inference:

data = ys.load("""
string: hello
integer: 42
float: 3.14
bool_true: true
bool_false: false
null_value: null
""")
# {
#   'string': 'hello',
#   'integer': 42,
#   'float': 3.14,
#   'bool_true': True,
#   'bool_false': False,
#   'null_value': None
# }

Error Handling

try:
    data = ys.load("invalid: yaml: syntax")
except Exception as e:
    print(f"Error loading YAML: {e}")

Version Information

# Get YAMLStar version
version = ys.version()
print(f"YAMLStar version: {version}")

API Reference

YAMLStar Class

__init__()

Create a new YAMLStar instance. Each instance maintains its own GraalVM isolate.

ys = yamlstar.YAMLStar()

load(yaml_input)

Load a single YAML document.

Parameters:

  • yaml_input (str): String containing YAML content

Returns:

  • Python object representing the YAML document (dict, list, str, int, float, bool, or None)

Raises:

  • Exception if the YAML is malformed

Example:

data = ys.load("key: value")

load_all(yaml_input)

Load all YAML documents from a multi-document string.

Parameters:

  • yaml_input (str): String containing one or more YAML documents

Returns:

  • List of Python objects, one per YAML document

Raises:

  • Exception if the YAML is malformed

Example:

docs = ys.load_all("---\ndoc1\n---\ndoc2")

version()

Get the YAMLStar version string.

Returns:

  • str: Version string

Example:

version = ys.version()

Development

Running Tests

# Run all tests
make test

# Run only pytest tests
make test-pytest

# Run only FFI tests
make test-ffi

Building Distribution

# Build source distribution
make dist

# Build and install in development mode
make install

Requirements

  • Python: 3.6 or higher
  • libyamlstar: Shared library (installed separately)
  • System: Linux or macOS

Library Search Path

The package searches for libyamlstar.so (or .dylib on macOS) in:

  1. Development path (relative to package)
  2. Directories in LD_LIBRARY_PATH environment variable
  3. /usr/local/lib (default install location)
  4. ~/.local/lib (user-local install location)

Comparison to PyYAML

Feature YAMLStar PyYAML
YAML Version 1.2 1.1
Implementation Pure Clojure C + Python
Type Inference YAML 1.2 core schema YAML 1.1 + custom
Native Performance Yes (GraalVM) Yes (C extension)
Dependencies libyamlstar.so None

License

MIT License - See License file

Credits

Created by Ingy döt Net, inventor of YAML.

YAMLStar is built on the YAML Reference Parser (pure Clojure implementation).

Links

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

yamlstar-0.1.15.tar.gz (7.2 kB view details)

Uploaded Source

Built Distributions

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

yamlstar-0.1.15-py3-none-win_amd64.whl (10.6 MB view details)

Uploaded Python 3Windows x86-64

yamlstar-0.1.15-py3-none-manylinux_2_31_x86_64.whl (11.0 MB view details)

Uploaded Python 3manylinux: glibc 2.31+ x86-64

yamlstar-0.1.15-py3-none-macosx_14_0_arm64.whl (10.2 MB view details)

Uploaded Python 3macOS 14.0+ ARM64

File details

Details for the file yamlstar-0.1.15.tar.gz.

File metadata

  • Download URL: yamlstar-0.1.15.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for yamlstar-0.1.15.tar.gz
Algorithm Hash digest
SHA256 d700da5e52e4fc55d7c0126fe506d700952393f575a7264c3f9e1e2dd8848da1
MD5 4fd9558b3b193ed8e104c6a810f1c226
BLAKE2b-256 48daa48473a571bb4d26bc92366e1b1a69d38c2e141fb53896ae7447770da6e9

See more details on using hashes here.

File details

Details for the file yamlstar-0.1.15-py3-none-win_amd64.whl.

File metadata

  • Download URL: yamlstar-0.1.15-py3-none-win_amd64.whl
  • Upload date:
  • Size: 10.6 MB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for yamlstar-0.1.15-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 b7f5f74277063786b50d73b8ecae721286f23f6e9399543e247c3f7be0e2487f
MD5 5a8bf9971569096ecb30947dfbdb6b9e
BLAKE2b-256 ea668a33080a44ef5d5b224bafbf52336ab557623d97cee76be65356f5e92a4f

See more details on using hashes here.

File details

Details for the file yamlstar-0.1.15-py3-none-manylinux_2_31_x86_64.whl.

File metadata

File hashes

Hashes for yamlstar-0.1.15-py3-none-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 6777730b5d5034aa0cbee4730e8ed3c83f2f3734fd07a216067ae00f6d5c5379
MD5 17bea657aebb51190f5f92d3467c123a
BLAKE2b-256 143e1cb0c8bc225fb89a65048fbd3fd01ec17cbd64ed72e3e611fab4b74d3e10

See more details on using hashes here.

File details

Details for the file yamlstar-0.1.15-py3-none-macosx_14_0_arm64.whl.

File metadata

File hashes

Hashes for yamlstar-0.1.15-py3-none-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 f227a7021900a401ee953f56e690672e491c4df16e5dc4359052ebf8234a7152
MD5 8daca7f8ee691a90da52e3510e5eb56a
BLAKE2b-256 ab02f13c1e8cdbe91ee3ac90b218d990aaa545142c9352c0a6d98b466dcb2f87

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