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
@@ -103,7 +107,7 @@ When you're done, `unstructure` the data to its unstructured form and pass it al
103
107
Use [attrs type metadata](http://attrs.readthedocs.io/en/stable/examples.html#types) to add type metadata to attributes, so _cattrs_ will know how to structure and destructure them.
Copy file name to clipboardExpand all lines: docs/basics.md
+34-33Lines changed: 34 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,16 +3,16 @@
3
3
```
4
4
5
5
All _cattrs_ functionality is exposed through a {class}`cattrs.Converter` object.
6
-
A global converter is provided for convenience as {data}`cattrs.global_converter` but more complex customizations should be performed on private instances.
6
+
A global converter is provided for convenience as {data}`cattrs.global_converter` but more complex customizations should be performed on private instances, any number of which can be made.
7
7
8
8
9
-
## Converters
9
+
## Converters and Hooks
10
10
11
-
The core functionality of a converter is [structuring](structuring.md) and [unstructuring](unstructuring.md) data by composing provided and [custom handling functions](customizing.md), called _hooks_.
11
+
The core functionality of a converter is structuring and unstructuring data by composing [provided](defaulthooks.md) and [custom handling functions](customizing.md), called _hooks_.
12
12
13
13
To create a private converter, instantiate a {class}`cattrs.Converter`. Converters are relatively cheap; users are encouraged to have as many as they need.
14
14
15
-
The two main methods are {meth}`structure <cattrs.BaseConverter.structure>` and {meth}`unstructure <cattrs.BaseConverter.unstructure>`, these are used to convert between _structured_ and _unstructured_ data.
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
17
```python
18
18
>>>from cattrs import structure, unstructure
@@ -28,53 +28,54 @@ The two main methods are {meth}`structure <cattrs.BaseConverter.structure>` and
28
28
Model(a=1)
29
29
```
30
30
31
-
_cattrs_ comes with a rich library of un/structuring rules by default, but it excels at composing custom rules with built-in ones.
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
-
The simplest approach to customization is wrapping an existing hook with your own function.
34
-
A base hook can be obtained from a converter and be subjected to the very rich machinery of function composition in Python.
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.
35
35
36
36
```python
37
-
>>>from cattrs import get_structure_hook
37
+
>>>defint_hook(value, type):
38
+
...ifnotisinstance(value, int):
39
+
...raiseValueError('not an int!')
40
+
...return value
41
+
```
38
42
39
-
>>> base_hook = get_structure_hook(Model)
43
+
We can then register this hook to a converter and any other hook converting an `int` will use it.
0 commit comments