Skip to main content

Fast, safe YAML parser for Python (Rust-powered)

Project description

RustyYAML ๐Ÿฆ€

Fast, safe YAML parser for Python โ€“ A drop-in replacement for PyYAML with 10-100x performance improvement.

PyPI version CI License

Features

  • โšก 10-100x faster than pure Python PyYAML
  • ๐Ÿ”’ 100% safe by default โ€“ No code execution vulnerabilities
  • ๐ŸŽฏ Drop-in replacement โ€“ Compatible with PyYAML API
  • ๐Ÿš€ Always fast โ€“ No C extension installation required
  • ๐Ÿงต Parallel loading โ€“ Parse multiple files simultaneously
  • ๐Ÿ Pure Python API โ€“ Pythonic and easy to use

Installation

pip install rustyyaml

That's it! No C compiler, no build tools needed.

Quick Start

import rustyyaml as yaml

# Parse YAML string
data = yaml.safe_load("key: value")
print(data)  # {'key': 'value'}

# Load from file
with open('config.yaml') as f:
    config = yaml.safe_load(f)

# Load from Path
from pathlib import Path
config = yaml.safe_load(Path('config.yaml'))

# Parse multiple documents
docs = yaml.load_all("""
doc: 1
---
doc: 2
---
doc: 3
""")
print(len(docs))  # 3

# Batch loading (parallel) - 5-10x faster for multiple files
results = yaml.safe_load_many([yaml1, yaml2, yaml3])

# Load entire directory
configs = yaml.load_directory('./configs', recursive=True)
for filename, data in configs:
    print(f"{filename}: {data}")

Migration from PyYAML

Option 1: Replace import

# Before
import yaml

# After
import rustyyaml as yaml

# Rest of your code works unchanged!
data = yaml.safe_load("key: value")

Option 2: Use compatibility layer

# Add this line at the top of your main file
import rustyyaml.compat

# All existing code works unchanged
import yaml
config = yaml.safe_load(open('config.yaml'))

Benchmarks

Tested on Apple M2, Python 3.11, parsing a Kubernetes manifest 5000 times:

Library Time Speedup
PyYAML (pure Python) 12.3s 1x (baseline)
PyYAML (with LibYAML) 1.8s 6.8x
RustyYAML 0.4s 30x

Batch Loading Performance

Operation Sequential Parallel Speedup
100 files 0.8s 0.2s 4x
1000 files 8.2s 1.1s 7.5x

Why RustyYAML?

The Problem

PyYAML is slow because:

  1. It's written in pure Python (interpreted, not compiled)
  2. It loops through every element sequentially
  3. It creates intermediate Python objects during parsing
  4. The C extension (LibYAML) is often missing or hard to install

The Solution

RustyYAML:

  1. Written in Rust (compiled to native code)
  2. Uses parallel processing for batch operations
  3. Zero-copy parsing where possible
  4. Always includes the fast parser (no C extension needed)
  5. Pre-built wheels for all major platforms

Safety

Unlike PyYAML, RustyYAML is secure by default:

# This would execute code in PyYAML's unsafe mode
dangerous = "!!python/object/apply:os.system ['rm -rf /']"

# RustyYAML blocks it by default
yaml.safe_load(dangerous)  # Raises YAMLError: Unsafe tag detected

# Only use unsafe_load() if you completely trust the source
yaml.unsafe_load(trusted_yaml)  # Use with caution!

API Reference

Core Functions

Function Description
safe_load(stream) Parse YAML safely (recommended)
unsafe_load(stream) Parse without safety checks
load(stream) Alias for safe_load()
load_all(stream) Parse multiple documents

File Operations

Function Description
safe_load_file(path) Load YAML from file path
load_all_file(path) Load multiple documents from file

Batch Operations

Function Description
safe_load_many(yamls) Parse list of YAML strings in parallel
unsafe_load_many(yamls) Parallel parsing without safety checks
load_directory(path, recursive=False) Load all YAML files from directory

Input Types

All loading functions accept:

  • str - YAML content as string
  • bytes - YAML content as bytes (UTF-8)
  • Path - Path to YAML file
  • File objects - Open file handles

Compatibility

โœ… Fully Supported

  • yaml.safe_load() - Drop-in replacement
  • yaml.load() - Defaults to safe mode (unlike PyYAML!)
  • yaml.load_all() - Multiple document support
  • yaml.YAMLError - Exception handling

โš ๏ธ Not Yet Supported

  • yaml.dump() / yaml.safe_dump() - Coming in v2.0
  • yaml.YAMLObject - Custom object serialization
  • Custom constructors/representers

Error Handling

from rustyyaml import YAMLError

try:
    data = yaml.safe_load(invalid_yaml)
except YAMLError as e:
    print(f"Parse error: {e}")

Development

Building from source

# Clone the repository
git clone https://github.com/yourusername/rustyaml
cd rustyaml

# Install development dependencies
pip install maturin pytest

# Build and install in development mode
maturin develop

# Run tests
pytest tests/ -v

# Run Rust tests
cargo test

# Run benchmarks
cargo bench

Project Structure

rustyaml/
โ”œโ”€โ”€ Cargo.toml              # Rust dependencies
โ”œโ”€โ”€ pyproject.toml          # Python packaging
โ”œโ”€โ”€ src/                    # Rust source code
โ”‚   โ”œโ”€โ”€ lib.rs              # PyO3 module entry point
โ”‚   โ”œโ”€โ”€ parser.rs           # YAML parsing logic
โ”‚   โ”œโ”€โ”€ types.rs            # Type conversion
โ”‚   โ”œโ”€โ”€ error.rs            # Error handling
โ”‚   โ”œโ”€โ”€ safe.rs             # Safety filters
โ”‚   โ””โ”€โ”€ batch.rs            # Parallel batch loading
โ”œโ”€โ”€ python/rustyaml/        # Python wrapper
โ”‚   โ”œโ”€โ”€ __init__.py         # Public API
โ”‚   โ””โ”€โ”€ compat.py           # PyYAML compatibility
โ”œโ”€โ”€ tests/                  # Test suites
โ””โ”€โ”€ benches/                # Criterion benchmarks

Contributing

Contributions welcome! Please read our contributing guidelines before submitting a PR.

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

Licensed under either of:

at your option.

Acknowledgments

Changelog

v0.1.0

  • Initial release
  • Core YAML parsing functionality
  • PyYAML API compatibility
  • Parallel batch loading
  • Safety filters for dangerous tags

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

rustyyaml-0.1.5-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (484.8 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ x86-64

rustyyaml-0.1.5-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (487.9 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARMv7l

rustyyaml-0.1.5-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (471.9 kB view details)

Uploaded CPython 3.9+manylinux: glibc 2.17+ ARM64

rustyyaml-0.1.5-cp39-abi3-macosx_11_0_arm64.whl (421.4 kB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

rustyyaml-0.1.5-cp39-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (838.4 kB view details)

Uploaded CPython 3.9+macOS 10.12+ universal2 (ARM64, x86-64)macOS 10.12+ x86-64macOS 11.0+ ARM64

File details

Details for the file rustyyaml-0.1.5-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for rustyyaml-0.1.5-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b3c765d863e181666d60cdcaa2e38152438518dfc3a877f7468d45267fa6de91
MD5 51349b9bdd179c738de3c1fe937d9847
BLAKE2b-256 f292302b6c08c89d3b3d4d280f357a4ae2cbccb97a4ae07a3da6bc2d3f66b440

See more details on using hashes here.

Provenance

The following attestation bundles were made for rustyyaml-0.1.5-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on beelal-k/rustyyaml

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

File details

Details for the file rustyyaml-0.1.5-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for rustyyaml-0.1.5-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 405497203ed4452547482cb428ba74afcf254a292c73b82311c60cafec6c11cc
MD5 2970044a02ef050f48aa9a91f0491155
BLAKE2b-256 232c47e16b41eaf932b256236a7050501e88241d5e7cac7e12b253e1fda86488

See more details on using hashes here.

Provenance

The following attestation bundles were made for rustyyaml-0.1.5-cp39-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl:

Publisher: release.yml on beelal-k/rustyyaml

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

File details

Details for the file rustyyaml-0.1.5-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for rustyyaml-0.1.5-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 9bea7c3a03ed24a257a9aad18229cbf3ff40c937697a3a9b1c91a846621ef43b
MD5 073fc3bb41a66b24c735a6ff152d8fa6
BLAKE2b-256 064096904b6ec9bfe201a652483ce2ab23eb10a3e540f360f3fb4f4bb72530fc

See more details on using hashes here.

Provenance

The following attestation bundles were made for rustyyaml-0.1.5-cp39-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on beelal-k/rustyyaml

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

File details

Details for the file rustyyaml-0.1.5-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for rustyyaml-0.1.5-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 cdf0db602e61a0a283468231c62ffc57ef40aa730fe6dbbe61514a38dd67fb95
MD5 26c029adb2c34ce4850099cc23fb8a56
BLAKE2b-256 db4d2070ca5712a51a5abe90d7c186e8efea5e3e77a918e3efcba26f4aca0e25

See more details on using hashes here.

Provenance

The following attestation bundles were made for rustyyaml-0.1.5-cp39-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on beelal-k/rustyyaml

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

File details

Details for the file rustyyaml-0.1.5-cp39-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl.

File metadata

File hashes

Hashes for rustyyaml-0.1.5-cp39-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 1defe00cb3c96af6fda7b9df241200d1e787722f38ed098a9a2cc3c7fb3e3e77
MD5 2be52ab2335d686fd5f16dedd508bfc7
BLAKE2b-256 72463bfda8aa026110614c9fddb65b46c4023edaef1e098c9147111f60bdd47f

See more details on using hashes here.

Provenance

The following attestation bundles were made for rustyyaml-0.1.5-cp39-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl:

Publisher: release.yml on beelal-k/rustyyaml

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 Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page