You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
8
8
9
9
* one has to master PyYaml Loader/Dumper internal features to understand what they are doing
10
10
* there is a mandatory metaclass, which can prevent wide adoption (multiple inheritance with metaclasses...)
11
11
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!
13
13
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)
15
17
16
18
17
19
## Installing
@@ -26,8 +28,8 @@ Let's make a class yaml-able: we have to
26
28
27
29
- inherit from `YamlAble`
28
30
- 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
+
31
33
```python
32
34
from yamlable import yaml_info, YamlAble
33
35
@@ -43,14 +45,14 @@ class Foo(YamlAble):
43
45
def__str__(self):
44
46
""" String representation for prints """
45
47
return"Foo - "+str(dict(a=self.a, b=self.b))
46
-
48
+
47
49
defto_yaml_dict(self):
48
-
""" This method is called when you call yaml.dump()"""
50
+
""" This optional method is called when you call yaml.dump()"""
49
51
return {'a': self.a, 'b': self.b}
50
52
51
53
@classmethod
52
54
deffrom_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()"""
54
56
return Foo(dct['a'], dct['b'])
55
57
```
56
58
@@ -63,23 +65,35 @@ That's it! Let's check that our class is correct and allows us to create instanc
63
65
Foo - {'a': 1, 'b': 'hello'}
64
66
```
65
67
66
-
The object directly has the `dump_yaml` (dumping to file) / `dumps_yaml` (dumping to string) methods:
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:
83
97
84
98
```python
85
99
>>>print(f.dumps_yaml(default_flow_style=False))
@@ -89,9 +103,6 @@ a: 1
89
103
b: hello
90
104
```
91
105
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
-
95
106
See [Usage](./usage) for other possibilities of `yamlable`.
0 commit comments