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
* Initial list strategy work
* Black reformat
* More docs
* Changelog
* More sets for cols
* More history
* Add test for better recursive structuring
* Improve set handling on 3.8
* Docs
* Docs
- Introduce and [document](https://catt.rs/en/latest/customizing.html#customizing-collections) the {mod}`cattrs.cols` module for better collection customizations.
- The {class}`orjson preconf converter <cattrs.preconf.orjson.OrjsonConverter>` now passes through dates and datetimes to orjson while unstructuring, greatly improving speed.
- The [tagged union strategy](https://catt.rs/en/stable/strategies.html#tagged-unions-strategy) now leaves the tags in the payload unless `forbid_extra_keys` is set.
Copy file name to clipboardExpand all lines: docs/basics.md
+15-23Lines changed: 15 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ To create a private converter, instantiate a {class}`cattrs.Converter`. Converte
14
14
15
15
The two main methods, {meth}`structure <cattrs.BaseConverter.structure>` and {meth}`unstructure <cattrs.BaseConverter.unstructure>`, are used to convert between _structured_ and _unstructured_ data.
16
16
17
-
```python
17
+
```{doctest} basics
18
18
>>> from cattrs import structure, unstructure
19
19
>>> from attrs import define
20
20
@@ -23,40 +23,39 @@ The two main methods, {meth}`structure <cattrs.BaseConverter.structure>` and {me
23
23
... a: int
24
24
25
25
>>> unstructure(Model(1))
26
-
{"a": 1}
26
+
{'a': 1}
27
27
>>> structure({"a": 1}, Model)
28
28
Model(a=1)
29
29
```
30
30
31
31
_cattrs_ comes with a rich library of un/structuring hooks by default but it excels at composing custom hooks with built-in ones.
32
32
33
33
The simplest approach to customization is writing a new hook from scratch.
34
-
For example, we can write our own hook for the `int` class.
34
+
For example, we can write our own hook for the `int` class and register it to a converter.
35
35
36
-
```python
37
-
>>>defint_hook(value, type):
36
+
```{doctest} basics
37
+
>>> from cattrs import Converter
38
+
39
+
>>> converter = Converter()
40
+
41
+
>>> @converter.register_structure_hook
42
+
... def int_hook(value, type) -> int:
38
43
... if not isinstance(value, int):
39
44
... raise ValueError('not an int!')
40
45
... return value
41
46
```
42
47
43
-
We can then register this hook to a converter and any other hook converting an `int` will use it.
0 commit comments