Skip to content

Commit cf08405

Browse files
committed
docs: streamline Python docs with unified guides and clearer examples
- Refresh overview with the core “fast, correct, safe, simple” example plus clearer file/tag/roundtrip sections. - Replace Usage with a “YAML in 2 Minutes” guide covering a full first example, collections, scalars, and an end-to-end sample. - Expand Tags into an advanced guide on handlers, mapping keys, streams, anchors, tag directives, and core tags, using concise assertions and named args. - Tidy API examples to assert full round-trips. - Update MkDocs nav to group the two guides under Guides.
1 parent f344697 commit cf08405

File tree

5 files changed

+622
-84
lines changed

5 files changed

+622
-84
lines changed

docs/api.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,18 @@ from yaml12 import Yaml, format_yaml, parse_yaml
4646

4747
# Tagged scalar
4848
text = format_yaml({"env": Yaml("prod", "!env")})
49-
assert "!env" in text
50-
assert isinstance(parse_yaml(text)["env"], Yaml)
49+
env = parse_yaml(text)["env"]
50+
assert isinstance(env, Yaml) and env.tag == "!env" and env.value == "prod"
5151

5252
# Collection key
5353
data = {Yaml(["a", "b"]): "val"}
5454
out = format_yaml(data)
55-
assert parse_yaml(out)[Yaml(["a", "b"])] == "val"
55+
assert parse_yaml(out) == data
5656

5757
# Tagged collection key
5858
data = {Yaml(["a", "b"], "!pair"): "val"}
5959
out = format_yaml(data)
60-
key = next(iter(parse_yaml(out)))
61-
assert isinstance(key, Yaml) and key.tag == "!pair" and key.value == ["a", "b"]
60+
assert parse_yaml(out) == data
6261
```
6362

6463
## _dbg_yaml(text)

docs/index.md

Lines changed: 93 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# yaml12
22

3-
`yaml12` exposes the Rust-based `saphyr` YAML 1.2 parser and emitter to Python through a small, function-first API. The bindings keep conversions lean, support multi-document streams, and materialize tagged nodes or unhashable mapping keys as a single lightweight `Yaml` dataclass (also exported as `Tagged` and `MappingKey`); otherwise you just use plain Python types.
3+
`yaml12` exposes the Rust-based `saphyr` YAML 1.2 parser and emitter to Python through a small, function-first API. The bindings keep conversions lean, support multi-document streams, and materialize tagged nodes or unhashable mapping keys as a single lightweight `Yaml` dataclass; otherwise you just use plain Python types.
44

55
- Parse YAML text or files into familiar Python types with `parse_yaml` and `read_yaml`; handlers apply to both values and keys.
66
- Serialize Python values back to YAML with `format_yaml` or write directly to disk/stdout with `write_yaml`, including non-core tags.
@@ -22,27 +22,109 @@ pip install -e . --no-build-isolation
2222
```python
2323
from yaml12 import parse_yaml, format_yaml
2424

25-
doc = parse_yaml("server:\\n port: 8000\\n debug: true")
26-
assert doc == {"server": {"port": 8000, "debug": True}}
25+
yaml_text = """
26+
title: A modern YAML parser and emitter written in Rust
27+
properties: [fast, correct, safe, simple]
28+
features:
29+
tags: preserve
30+
streams: multi
31+
"""
32+
33+
doc = parse_yaml(yaml_text)
34+
35+
assert doc == {
36+
"title": "A modern YAML parser and emitter written in Rust",
37+
"properties": ["fast", "correct", "safe", "simple"],
38+
"features": {"tags": "preserve", "streams": "multi"},
39+
}
2740

2841
text = format_yaml(doc)
2942
print(text)
30-
# server:
31-
# port: 8000
32-
# debug: true
43+
# title: A modern YAML parser and emitter written in Rust
44+
# properties:
45+
# - fast
46+
# - correct
47+
# - safe
48+
# - simple
49+
# features:
50+
# tags: preserve
51+
# streams: multi
52+
```
53+
54+
### Reading and writing files
55+
56+
```python
57+
from yaml12 import read_yaml, write_yaml
58+
59+
value_out = {"alpha": 1, "nested": [True, None]}
60+
61+
write_yaml(value_out, "my.yaml")
62+
value_in = read_yaml("my.yaml")
63+
64+
assert value_in == value_out
65+
66+
# Multi-document streams
67+
docs_out = [{"foo": 1}, {"bar": [2, None]}]
68+
69+
write_yaml(docs_out, "my-multi.yaml", multi=True)
70+
docs_in = read_yaml("my-multi.yaml", multi=True)
71+
72+
assert docs_in == docs_out
73+
```
74+
75+
### Tag handlers
76+
77+
Handlers let you opt into custom behaviour for tagged nodes while keeping the default parser strict and safe.
78+
79+
```python
80+
from yaml12 import parse_yaml
81+
82+
yaml_text = """
83+
- !upper [rust, python]
84+
- !expr 6 * 7
85+
"""
86+
87+
handlers = {
88+
"!expr": lambda value: eval(value),
89+
"!upper": str.upper,
90+
}
91+
92+
doc = parse_yaml(yaml_text, handlers=handlers)
93+
assert doc == [["RUST", "PYTHON"], 42]
3394
```
3495

35-
Multi-document streams are just as simple:
96+
### Formatting and round-tripping
3697

3798
```python
38-
docs = parse_yaml(["first: 1", "second: 2"], multi=True)
39-
assert docs == [{"first": 1}, {"second": 2}]
99+
from yaml12 import Yaml, format_yaml, parse_yaml
100+
101+
obj = {
102+
"seq": [1, 2],
103+
"map": {"key": "value"},
104+
"tagged": Yaml("1 + 1", "!expr"),
105+
}
106+
107+
yaml_text = format_yaml(obj)
108+
print(yaml_text)
109+
# seq:
110+
# - 1
111+
# - 2
112+
# map:
113+
# key: value
114+
# tagged: !expr 1 + 1
115+
116+
parsed = parse_yaml(yaml_text)
117+
assert parsed == {
118+
"seq": [1, 2],
119+
"map": {"key": "value"},
120+
"tagged": Yaml("1 + 1", "!expr"),
121+
}
40122
```
41123

42124
## Where to go next
43125

44-
- Learn how to parse and emit YAML in more detail in [Usage](usage.md).
45-
- See how to work with custom tags and handlers in [Custom Tags](tags.md).
126+
- Learn YAML essentials in [YAML in 2 Minutes](usage.md).
127+
- Explore tags, anchors, and advanced features in [Tags, Anchors, and Advanced YAML](tags.md).
46128
- Scan the callable surface in [API Reference](api.md).
47129

48130
## Build or serve the docs locally

0 commit comments

Comments
 (0)