| 1 |
YAML Import/Export |
| 2 |
================== |
| 3 |
|
| 4 |
YAML_ (YAML Ain't Markup Language) is a human-readable data serialization language. |
| 5 |
|
| 6 |
PYYAML_ implements importer and exporter in python. *Please install it, before continuing* |
| 7 |
|
| 8 |
.. note:: |
| 9 |
|
| 10 |
anytree package does not depend on any external packages. |
| 11 |
It does **NOT** include PYYAML_. |
| 12 |
|
| 13 |
.. Warning:: |
| 14 |
|
| 15 |
It is not safe to call yaml.load with any data received from an untrusted source! yaml.load is as powerful as pickle.load and so may call any Python function. |
| 16 |
The yaml.safe_load function limits the load functionality to built-in types. |
| 17 |
|
| 18 |
Export |
| 19 |
------ |
| 20 |
|
| 21 |
The :any:`DictExporter` converts any tree to a dictionary, which can be handled |
| 22 |
by `yaml.dump`. |
| 23 |
|
| 24 |
>>> import yaml |
| 25 |
>>> from anytree import AnyNode |
| 26 |
>>> from anytree.exporter import DictExporter |
| 27 |
|
| 28 |
Example tree: |
| 29 |
|
| 30 |
>>> root = AnyNode(a="root") |
| 31 |
>>> s0 = AnyNode(a="sub0", parent=root) |
| 32 |
>>> s0a = AnyNode(a="sub0A", b="foo", parent=s0) |
| 33 |
>>> s0b = AnyNode(a="sub0B", parent=s0) |
| 34 |
>>> s1 = AnyNode(a="sub1", parent=root) |
| 35 |
|
| 36 |
Export to dictionary and convert to YAML: |
| 37 |
|
| 38 |
>>> dct = DictExporter().export(root) |
| 39 |
>>> print(yaml.dump(dct, default_flow_style=False)) |
| 40 |
a: root |
| 41 |
children: |
| 42 |
- a: sub0 |
| 43 |
children: |
| 44 |
- a: sub0A |
| 45 |
b: foo |
| 46 |
- a: sub0B |
| 47 |
- a: sub1 |
| 48 |
<BLANKLINE> |
| 49 |
|
| 50 |
:any:`DictExporter` controls the content. |
| 51 |
`yaml.dump` controls the YAML related stuff. |
| 52 |
|
| 53 |
To dump to a file, use an file object as second argument: |
| 54 |
|
| 55 |
>>> with open("/path/to/file", "w") as file: # doctest: +SKIP |
| 56 |
... yaml.dump(data, file) |
| 57 |
|
| 58 |
Import |
| 59 |
------ |
| 60 |
|
| 61 |
The `yaml.load` function reads YAML data --- a dictionary, which |
| 62 |
:any:`DictImporter` converts to a tree. |
| 63 |
|
| 64 |
>>> import yaml |
| 65 |
>>> from anytree.importer import DictImporter |
| 66 |
>>> from pprint import pprint # just for nice printing |
| 67 |
>>> from anytree import RenderTree # just for nice printing |
| 68 |
|
| 69 |
Example data: |
| 70 |
|
| 71 |
>>> data = """ |
| 72 |
... a: root |
| 73 |
... children: |
| 74 |
... - a: sub0 |
| 75 |
... children: |
| 76 |
... - a: sub0A |
| 77 |
... b: foo |
| 78 |
... - a: sub0B |
| 79 |
... - a: sub1 |
| 80 |
... """ |
| 81 |
|
| 82 |
Import to dictionary and convert to tree: |
| 83 |
|
| 84 |
>>> dct = yaml.load(data, Loader=yaml.Loader) |
| 85 |
>>> pprint(dct) |
| 86 |
{'a': 'root', |
| 87 |
'children': [{'a': 'sub0', |
| 88 |
'children': [{'a': 'sub0A', 'b': 'foo'}, {'a': 'sub0B'}]}, |
| 89 |
{'a': 'sub1'}]} |
| 90 |
>>> root = DictImporter().import_(dct) |
| 91 |
>>> print(RenderTree(root)) |
| 92 |
AnyNode(a='root') |
| 93 |
├── AnyNode(a='sub0') |
| 94 |
│ ├── AnyNode(a='sub0A', b='foo') |
| 95 |
│ └── AnyNode(a='sub0B') |
| 96 |
└── AnyNode(a='sub1') |
| 97 |
|
| 98 |
.. _YAML: https://en.wikipedia.org/wiki/YAML |
| 99 |
|
| 100 |
.. _PYYAML: http://pyyaml.org/wiki/PyYAMLDocumentation |