Skip to content

Commit 05e5109

Browse files
author
Sylvain MARIE
committed
Completed documentation
1 parent 3d5211a commit 05e5109

3 files changed

Lines changed: 62 additions & 4 deletions

File tree

docs/index.md

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ In addition `yamlable` provides a way to create Yaml codecs for several object t
2626

2727
## Usage
2828

29+
### 1. The (recommended) `YamlAble` way
30+
31+
#### a. Creating the class
32+
2933
Let's make a class yaml-able: we have to
3034

3135
- inherit from `YamlAble`
@@ -38,7 +42,7 @@ from yamlable import yaml_info, YamlAble
3842
@yaml_info(yaml_tag_ns='com.yamlable.example')
3943
class Foo(YamlAble):
4044

41-
def __init__(self, a, b):
45+
def __init__(self, a, b="hey"):
4246
""" Constructor """
4347
self.a = a
4448
self.b = b
@@ -67,6 +71,8 @@ That's it! Let's check that our class is correct and allows us to create instanc
6771
Foo - {'a': 1, 'b': 'hello'}
6872
```
6973

74+
#### b. Dumping and loading to/from YAML
75+
7076
Now let's dump and load it using `pyyaml`:
7177

7278
```python
@@ -105,7 +111,44 @@ a: 1
105111
b: hello
106112
```
107113

108-
See [Usage](./usage) for other possibilities of `yamlable`.
114+
#### c. Support for sequences and scalars
115+
116+
Objects can also be loaded from YAML sequences:
117+
118+
```python
119+
>>> print(yaml.safe_load("""
120+
!yamlable/com.yamlable.example.Foo
121+
- 0
122+
- hey
123+
"""))
124+
125+
Foo - {'a': 0, 'b': 'hey'}
126+
```
127+
128+
The default implementation of `__from_yaml_sequence__` (that you may wish to override in your subclass), is to call
129+
the constructor with the sequence contents as positional arguments.
130+
131+
The same also works for scalars:
132+
133+
```python
134+
>>> print(yaml.safe_load("""
135+
!yamlable/com.yamlable.example.Foo 0
136+
"""))
137+
138+
Foo - {'a': "0", 'b': 'hey'}
139+
```
140+
141+
The default implementation of `__from_yaml_scalar__` (that you may wish to override in your subclass), is to call
142+
the constructor with the scalar as first positional argument.
143+
144+
!!! warning "Scalars are not resolved"
145+
As can be seen in the above example, scalars are not auto-resolved when constructing an object from a scalar. So an
146+
integer `0` is actually received as a string `"0"` by `from_yaml_scalar`.
147+
148+
149+
#### d. What if you can not modify the class ?
150+
151+
See [Usage](./usage#yamlcodec) for another possibility offered by `yamlable`: creating a codec to handle YAML for several classes at once, typically classes that you cannot modify.
109152

110153

111154
## Main features / benefits

docs/usage.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ You have seen in the [main page](./index) a small example to understand the conc
44

55
## `YamlCodec`
66

7+
### 1. Writing a codec class
8+
79
Sometimes you do not have the possibility to change the classes of the objects that you wish to encode/decode. In this case the solution is to write an independent codec, inheriting from `YamlCodec`. Once again this feature leverages the `multi_constructor` and `multi_representer` concepts available in the `PyYaml` internals, but with `YamlCodec` it becomes a bit easier to do.
810

911
Let's assume that the following two classes are given and can not be modified:
@@ -84,13 +86,17 @@ class MyCodec(YamlCodec):
8486
return types_to_yaml_tags[type(obj)], vars(obj)
8587
```
8688

89+
### 2. Registering a codec
90+
8791
When you codec has been defined, it needs to be registerd before being usable. You can specify with which `PyYaml` Loaders/Dumpers it should be registered, or use the default (all):
8892

8993
```python
9094
# register the codec
9195
MyCodec.register_with_pyyaml()
9296
```
9397

98+
### 3. Using a codec
99+
94100
Finally let's test that the codec works:
95101

96102
```python
@@ -111,3 +117,10 @@ assert dump(b) == by
111117
assert f == load(fy)
112118
assert b == load(by)
113119
```
120+
121+
### 4. Sequences and scalars
122+
123+
Objects can be loaded from sequences and scalars, in addition to dictionaries. To support this possibility, you simply need to fill the class methods:
124+
125+
- `from_yaml_sequence` for sequences
126+
- `from_yaml_scalar` for scalars

docs/mkdocs.yml renamed to mkdocs.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
site_name: yamlable
22
# site_description: 'A short description of my project'
33
repo_url: https://github.com/smarie/python-yamlable
4-
docs_dir: .
5-
site_dir: ../site
4+
# docs_dir: .
5+
# site_dir: ../site
6+
# default branch is main instead of master now on github
7+
edit_uri : ./edit/main/docs
68
nav:
79
- Home: index.md
810
- Usage details: usage.md

0 commit comments

Comments
 (0)