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
Copy file name to clipboardExpand all lines: README.md
+27-30Lines changed: 27 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,31 +9,21 @@
9
9
10
10
---
11
11
12
-
**cattrs** is an open source Python library for structuring and unstructuring
13
-
data. _cattrs_ works best with _attrs_ classes, dataclasses and the usual
14
-
Python collections, but other kinds of classes are supported by manually
15
-
registering converters.
16
-
17
-
Python has a rich set of powerful, easy to use, built-in data types like
18
-
dictionaries, lists and tuples. These data types are also the lingua franca
19
-
of most data serialization libraries, for formats like json, msgpack, cbor,
20
-
yaml or toml.
21
-
22
-
Data types like this, and mappings like `dict` s in particular, represent
23
-
unstructured data. Your data is, in all likelihood, structured: not all
24
-
combinations of field names or values are valid inputs to your programs. In
25
-
Python, structured data is better represented with classes and enumerations.
26
-
_attrs_ is an excellent library for declaratively describing the structure of
27
-
your data, and validating it.
28
-
29
-
When you're handed unstructured data (by your network, file system, database...),
30
-
_cattrs_ helps to convert this data into structured data. When you have to
31
-
convert your structured data into data types other libraries can handle,
32
-
_cattrs_ turns your classes and enumerations into dictionaries, integers and
33
-
strings.
34
-
35
-
Here's a simple taste. The list containing a float, an int and a string
36
-
gets converted into a tuple of three ints.
12
+
**cattrs** is an open source Python library for structuring and unstructuring data.
13
+
_cattrs_ works best with _attrs_ classes, dataclasses and the usual Python collections, but other kinds of classes are supported by manually registering converters.
14
+
15
+
Python has a rich set of powerful, easy to use, built-in data types like dictionaries, lists and tuples.
16
+
These data types are also the lingua franca of most data serialization libraries, for formats like json, msgpack, cbor, yaml or toml.
17
+
18
+
Data types like this, and mappings like `dict` s in particular, represent unstructured data.
19
+
Your data is, in all likelihood, structured: not all combinations of field names or values are valid inputs to your programs.
20
+
In Python, structured data is better represented with classes and enumerations.
21
+
_attrs_ is an excellent library for declaratively describing the structure of your data and validating it.
22
+
23
+
When you're handed unstructured data (by your network, file system, database...), _cattrs_ helps to convert this data into structured data.
24
+
When you have to convert your structured data into data types other libraries can handle, _cattrs_ turns your classes and enumerations into dictionaries, integers and strings.
25
+
26
+
Here's a simple taste. The list containing a float, an int and a string gets converted into a tuple of three ints.
37
27
38
28
```python
39
29
>>>import cattrs
@@ -64,7 +54,7 @@ Here's a much more complex example, involving _attrs_ classes with type metadata
64
54
65
55
```python
66
56
>>>from enum import unique, Enum
67
-
>>>from typing importOptional, Sequence, Union
57
+
>>>from typing import Sequence
68
58
>>>from cattrs import structure, unstructure
69
59
>>>from attrs import define, field
70
60
@@ -87,14 +77,18 @@ Here's a much more complex example, involving _attrs_ classes with type metadata
87
77
>>>@define
88
78
...classDog:
89
79
... cuteness: int
90
-
... chip: Optional[DogMicrochip]=None
80
+
... chip: DogMicrochip|None=None
91
81
92
-
>>> p = unstructure([Dog(cuteness=1, chip=DogMicrochip(chip_id=1, time_chipped=10.0)),
@@ -147,6 +141,9 @@ _cattrs_ is based on a few fundamental design decisions.
147
141
- Un/structuring rules are separate from the models.
148
142
This allows models to have a one-to-many relationship with un/structuring rules, and to create un/structuring rules for models which you do not own and you cannot change.
149
143
(_cattrs_ can be configured to use un/structuring rules from models using the [`use_class_methods` strategy](https://catt.rs/en/latest/strategies.html#using-class-specific-structure-and-unstructure-methods).)
144
+
- Strongly lean on function composition.
145
+
Almost all problems in _cattrs_ can be solved by writing and composing functions (called _hooks_), instead of writing classes and subclassing.
146
+
This makes _cattrs_ code elegant, concise, powerful and amenable to all the rich Python ways of working with functions.
150
147
- Invent as little as possible; reuse existing ordinary Python instead.
151
148
For example, _cattrs_ did not have a custom exception type to group exceptions until the sanctioned Python [`exceptiongroups`](https://docs.python.org/3/library/exceptions.html#ExceptionGroup).
152
149
A side-effect of this design decision is that, in a lot of cases, when you're solving _cattrs_ problems you're actually learning Python instead of learning _cattrs_.
0 commit comments