Skip to content

Commit 661e4f1

Browse files
committed
0.2.0 - Updated documentation and changelog
1 parent e3dfd09 commit 661e4f1

3 files changed

Lines changed: 33 additions & 18 deletions

File tree

docs/changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
### 0.2.0 - Added default implementation
4+
5+
* By default `to_yaml_dict` returns `vars(self)` and `from_yaml_dict` returns `cls(**dct)`. Fixes [#1](https://github.com/smarie/python-yamlable/issues/1)
6+
37
### 0.1.0 - First public version
48

59
* Initial fork from private repository

docs/index.md

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44

55
[![Build Status](https://travis-ci.org/smarie/python-yamlable.svg?branch=master)](https://travis-ci.org/smarie/python-yamlable) [![Tests Status](https://smarie.github.io/python-yamlable/junit/junit-badge.svg?dummy=8484744)](https://smarie.github.io/python-yamlable/junit/report.html) [![codecov](https://codecov.io/gh/smarie/python-yamlable/branch/master/graph/badge.svg)](https://codecov.io/gh/smarie/python-yamlable) [![Documentation](https://img.shields.io/badge/docs-latest-blue.svg)](https://smarie.github.io/python-yamlable/) [![PyPI](https://img.shields.io/badge/PyPI-yamlable-blue.svg)](https://pypi.python.org/pypi/yamlable/)
66

7-
PyYaml is a great library. However it is a bit hard for anyone to add the yaml capability to their classes. Its `YamlObject` helper class is a first step but it has two drawbacks:
7+
PyYaml is a great library. However it is a bit hard for anyone to add the yaml capability to their classes while keeping control on what's happening. Its `YamlObject` helper class is a first step but it has two drawbacks:
88

99
* one has to master PyYaml Loader/Dumper internal features to understand what they are doing
1010
* there is a mandatory metaclass, which can prevent wide adoption (multiple inheritance with metaclasses...)
1111

12-
`yamlable` provides a very easy way for you to leverage PyYaml without seeing the complexity: simply inherit from `YamlAble`, decorate with `@yaml_info`, implement the abstract methods to write to / load from a dictionary, and you're set!
12+
`yamlable` provides a very easy way for you to leverage PyYaml without seeing the complexity: simply inherit from `YamlAble`, decorate with `@yaml_info`, and you're set!
1313

14-
In addition `yamlable` provides a way to creade Yaml codecs for several object types at the same time (`YamlCodec`) (see Usage page)
14+
You can then optionally override the methods `to_yaml_dict` and `from_yaml_dict` to write to / load from a dictionary, so as to adapt the yaml-ability to your class needs.
15+
16+
In addition `yamlable` provides a way to create Yaml codecs for several object types at the same time (`YamlCodec`) (see [Usage](./usage) page)
1517

1618

1719
## Installing
@@ -26,8 +28,8 @@ Let's make a class yaml-able: we have to
2628

2729
- inherit from `YamlAble`
2830
- decorate it with the `@yaml_info` annotation to declare the associated yaml tag
29-
- implement `from_yaml_dict` (class method called during decoding) and `to_yaml_dict` (instance method called during encoding)
30-
31+
- *optionally* implement `from_yaml_dict` (class method called during decoding) and/or `to_yaml_dict` (instance method called during encoding) if we wish to have control on the process, for example to only dump part of the attributes or perform some custom instance creation. Note that default implementation relies on `vars(self)` for dumping and on `cls(**dct)` for loading.
32+
3133
```python
3234
from yamlable import yaml_info, YamlAble
3335

@@ -43,14 +45,14 @@ class Foo(YamlAble):
4345
def __str__(self):
4446
""" String representation for prints """
4547
return "Foo - " + str(dict(a=self.a, b=self.b))
46-
48+
4749
def to_yaml_dict(self):
48-
""" This method is called when you call yaml.dump()"""
50+
""" This optional method is called when you call yaml.dump()"""
4951
return {'a': self.a, 'b': self.b}
5052

5153
@classmethod
5254
def from_yaml_dict(cls, dct, yaml_tag):
53-
""" This method is called when you call yaml.load()"""
55+
""" This optional method is called when you call yaml.load()"""
5456
return Foo(dct['a'], dct['b'])
5557
```
5658

@@ -63,23 +65,35 @@ That's it! Let's check that our class is correct and allows us to create instanc
6365
Foo - {'a': 1, 'b': 'hello'}
6466
```
6567

66-
The object directly has the `dump_yaml` (dumping to file) / `dumps_yaml` (dumping to string) methods:
68+
Now let's dump and load it using `pyyaml`:
6769

6870
```python
69-
>>> print(f.dumps_yaml())
71+
>>> import yaml
72+
>>> print(yaml.dump(f))
7073

7174
!yamlable/com.yamlable.example.Foo {a: 1, b: hello}
7275
```
7376

74-
The class directly has the `load_yaml` (load from file) / `loads_yaml` (load from string) methods
75-
7677
```python
77-
>>> print(Foo.loads_yaml("!yamlable/com.yamlable.example.Foo {a: 0, b: hey}"))
78+
>>> print(yaml.load("!yamlable/com.yamlable.example.Foo {a: 0, b: hey}"))
7879

7980
Foo - {'a': 0, 'b': 'hey'}
8081
```
8182

82-
See pyyaml help page for the various formatting arguments that you can use..
83+
For more general cases where your object is embedded in a more complex structure for example, it will work as expected:
84+
85+
```python
86+
>>> d = {'foo': f, 'foo2': 12}
87+
>>> print(yaml.dump(d))
88+
89+
foo: !yamlable/com.yamlable.example.Foo {a: 1, b: hello}
90+
foo2: 12
91+
```
92+
93+
94+
In addition, the object directly offers the `dump_yaml` (dumping to file) / `dumps_yaml` (dumping to string) convenience methods, and the class directly offers the `load_yaml` (load from file) / `loads_yaml` (load from string) convenience methods.
95+
96+
See [PyYaml documentation](http://pyyaml.org/wiki/PyYAMLDocumentation) for the various formatting arguments that you can use, they are the same than in the `yaml.dump` method. For example:
8397

8498
```python
8599
>>> print(f.dumps_yaml(default_flow_style=False))
@@ -89,9 +103,6 @@ a: 1
89103
b: hello
90104
```
91105

92-
For more general cases where your object is embedded in a more complex structure for example, you can of course call pyyaml `dump`/`load` functions, it will work as expected.
93-
94-
95106
See [Usage](./usage) for other possibilities of `yamlable`.
96107

97108

docs/usage.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
You have seen in the [main page](./index) a small example to understand the concepts. Below we present other advanced usages.
44

5-
TODO
5+
**TODO + explain YamlCodec**

0 commit comments

Comments
 (0)