-
Notifications
You must be signed in to change notification settings - Fork 658
Expand file tree
/
Copy pathmrpt_containers_example.py
More file actions
49 lines (42 loc) · 1.46 KB
/
mrpt_containers_example.py
File metadata and controls
49 lines (42 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python3
"""
mrpt_containers_example.py — YAML config containers with mrpt.containers.
Demonstrates:
- mrpt.containers.YAML: parse, query, modify, serialise YAML documents.
Note: for full INI-style config files (with sections/keys and default values)
see mrpt.config (CConfigFileMemory / CConfigFile).
"""
from mrpt.containers import YAML
# ---------------------------------------------------------------------------
# Parse YAML from a string
# ---------------------------------------------------------------------------
src = """
robot:
name: R2D2
wheels: 2
speed_limit: 1.5
sensors:
- lidar
- camera
"""
doc = YAML.from_string(src)
print("Parsed YAML:")
print(doc)
# Basic API
print(f"\nhas('robot') = {doc.has('robot')}")
print(f"has('foobar') = {doc.has('foobar')}")
print(f"size() = {doc.size()}")
print(f"empty() = {doc.empty()}")
# ---------------------------------------------------------------------------
# Round-trip: to_string → re-parse
# ---------------------------------------------------------------------------
txt = doc.to_string()
doc2 = YAML.from_string(txt)
print(f"\nRound-trip OK: size={doc2.size()} ✓")
# ---------------------------------------------------------------------------
# Empty document
# ---------------------------------------------------------------------------
empty = YAML()
print(f"\nEmpty YAML: empty()={empty.empty()}, size={empty.size()}")
empty.clear()
print(" clear() on empty — no error ✓")