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
yaml_str = """
name: John Doe
age: 30
address:
street: 123 Main St
city: Boston
"""
data = parse(yaml_str)
print(data["name"])
print(data["address"]["city"])
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)
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)
for key, value, obj in yml.walk_keys():
if key == "age":
value += 1
elif key == "name":
obj[key] = value.str.capitalize()
print(yml.to_yaml())
JSON Conversion
from yamlium import from_json, from_dict
json_str = '{"name": "test", "values": [1, 2, 3]}'
yaml_data = from_json(json_str)
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
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
dev: &default_config
schedule: false
my_config: [1, 2, 3]
staging:
<<: *default_config
schedule: true
Output
yamlium | PyYaml |
---|
โ
Retaining structure | โ Changing structure |
dev: &default_config
schedule: false
my_config: [1, 2, 3]
staging:
<<: *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.