Skip to main content

Fast, modern yaml parser and manipulator

Project description

Yamlium

A high-performance, dependency-free YAML parser for Python that preserves all YAML features including comments, anchors, and formatting.

📦 Features

  • 🎯 First-Class YAML Features: Preserves all YAML elements including comments, newlines, anchor names, and formatting
  • High Performance: 3x faster than PyYAML
  • 🧹 Zero Dependencies: Pure Python implementation with no external dependencies
  • 🛡️ Type Safety: Full type hints support
  • 🛠️ Rich API: Intuitive interface for manipulating YAML structures

🛠️ Installation

pip install yamlium

🚀 Quick Start

Basic Parsing

from yamlium import parse

# Parse a YAML string
yaml_str = """
name: John Doe
age: 30
address:
  street: 123 Main St
  city: Boston
"""
data = parse(yaml_str)

# Access values
print(data["name"])  # John Doe
print(data["address"]["city"])  # Boston

Preserving YAML Features

from yamlium import parse

yaml_str = """
# User configuration
user: &user_ref # Anchor definition
  name: Alice
  role: admin

# Reference to user
admin: *user_ref # Alias reference

""".lstrip()
yml = parse(yaml_str)

# The YAML structure is preserved when converting back including:
# - Anchor names
# - Comments
# - Newlines between objects
print(yml.to_yaml() == yaml_str)

Manipulating YAML

from yamlium import parse

yaml_str = """
users: # List of users
  - name: alice
    age: 25
  - name: Bob
    age: 30
  - name: charlie
"""
yml = parse(yaml_str)

# Modify values while preserving structure
for key, value, obj in yml.walk_keys():
    if key == "age":
        value += 1
    elif key == "name":
        # Using the string manipulation interface `.str`
        obj[key] = value.str.capitalize()

print(yml.to_yaml())

Working with Comments

Yamlium provides structured access to YAML comments via the comments attribute:

from yamlium import parse

yaml_str = """
app:
  # Database settings
  db_host: localhost # primary host
  # TODO: increase timeout

  # Cache config
  cache_ttl: 3600
"""
yml = parse(yaml_str)

# Access comments on a key
db_key = list(yml["app"].keys())[0]
print(db_key.comments.head)  # ['# Database settings']

# Access inline and foot comments on a value
db_value = yml["app"]["db_host"]
print(db_value.comments.line)  # '# primary host'
print(db_value.comments.foot)  # ['# TODO: increase timeout']

# Modify comments
db_value.comments.line = "# updated comment"

Comment types:

  • comments.head - comments directly above a node
  • comments.line - inline comment on the same line
  • comments.foot - comments below a node (before a blank line)

Reading Markdown Frontmatter

from yamlium import read_markdown

# Parse a markdown file with YAML frontmatter
frontmatter, content = read_markdown("post.md")

if frontmatter:
    print(frontmatter["title"])  # Access YAML fields
    print(content)               # Raw markdown after the frontmatter

# Also works with raw strings
text = """---
title: My Post
tags: [python, yaml]
---
# Hello World

This is the body.
"""
frontmatter, content = read_markdown(text)

Supports both standard (----delimited) and open (bare YAML followed by ---) frontmatter formats. If no frontmatter is detected, returns (None, full_text).

JSON Conversion

from yamlium import from_json, from_dict

# Convert from JSON string
json_str = '{"name": "test", "values": [1, 2, 3]}'
yaml_data = from_json(json_str)

# Convert from Python dict
python_dict = {"name": "test", "values": [1, 2, 3]}
yaml_data = from_dict(python_dict)

📚 API Reference

Parsing Functions

  • parse(input: str | Path) -> Mapping Parse a single YAML document
  • parse_full(input: str | Path) -> Document Parse multiple YAML documents
  • read_markdown(input: str | Path) -> tuple[Mapping | None, str] Extract YAML frontmatter and content from markdown
  • from_json(input: str | Path) -> Mapping | Sequence Convert JSON to YAML structure
  • from_dict(input: dict | list) -> Mapping | Sequence Convert Python dict/list to YAML structure

Yaml object functions

Given:

from yamlium import parse
yml = parse("my_yaml.yml")
  • yml.to_yaml() Convert to yaml string
  • yml.to_dict() Convert to python dictionary
  • yml.yaml_dump(destination="my_yaml.yml") Write directly to yaml file
  • yml.pprint() Pretty print the dictionary
  • yml.walk() Iterate through all yaml objects
  • yml.walk_keys() Iterate through all yaml keys

🔄 Comparison to PyYaml

While PyYaml solves the purpose of converting to dictionary perfectly fine, it completely ignores anything non-dictionary-conversion related in the yaml file.

Input yaml

# Anchor definition
dev: &default_config
  schedule: false
  my_config: [1, 2, 3]

staging:
  # Alias reference
  <<: *default_config
  schedule: true

Output

yamlium PyYaml
✅ Retaining structure❌ Changing structure
# Anchor definition
dev: &default_config
  schedule: false
  my_config: [1, 2, 3]

staging:
  # Alias reference
  <<: *default_config
  schedule: true
dev:
  my_config: &id001
  - 1
  - 2
  - 3
  schedule: false
staging:
  my_config: *id001
  schedule: true

🤝 Contributing

Contributions are welcome! Please feel free to submit Issues, Feature requests or Pull requests!

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

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

yamlium-0.3.0.tar.gz (100.6 kB view details)

Uploaded Source

Built Distribution

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

yamlium-0.3.0-py3-none-any.whl (22.6 kB view details)

Uploaded Python 3

File details

Details for the file yamlium-0.3.0.tar.gz.

File metadata

  • Download URL: yamlium-0.3.0.tar.gz
  • Upload date:
  • Size: 100.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for yamlium-0.3.0.tar.gz
Algorithm Hash digest
SHA256 8874e04d9b2273e200c7170dcab6e4a958dcfd6549f5a86259c8ef32788403cf
MD5 539378aa61b9946b0676b9fd1dbd2008
BLAKE2b-256 b4f2f2a19eba02f055302f7c5bd13c9665d03820508557ee3257546c56326f62

See more details on using hashes here.

Provenance

The following attestation bundles were made for yamlium-0.3.0.tar.gz:

Publisher: build_release.yml on erikmunkby/yamlium

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

File details

Details for the file yamlium-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: yamlium-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 22.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for yamlium-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bdf968a08fb88ab22d787afa0200c989953eec6d7c8bc218df61b618a62f74d4
MD5 8fe7788154238ec776a203d8895e5f76
BLAKE2b-256 756e6e657e4b61d15aaeca83dc7dcc11edf5cbea7b849130ef84573d5b179d7c

See more details on using hashes here.

Provenance

The following attestation bundles were made for yamlium-0.3.0-py3-none-any.whl:

Publisher: build_release.yml on erikmunkby/yamlium

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