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
@@ -13,31 +13,21 @@
13
13
14
14
---
15
15
16
-
**cattrs** is an open source Python library for structuring and unstructuring
17
-
data. _cattrs_ works best with _attrs_ classes, dataclasses and the usual
18
-
Python collections, but other kinds of classes are supported by manually
19
-
registering converters.
20
-
21
-
Python has a rich set of powerful, easy to use, built-in data types like
22
-
dictionaries, lists and tuples. These data types are also the lingua franca
23
-
of most data serialization libraries, for formats like json, msgpack, cbor,
24
-
yaml or toml.
25
-
26
-
Data types like this, and mappings like `dict` s in particular, represent
27
-
unstructured data. Your data is, in all likelihood, structured: not all
28
-
combinations of field names or values are valid inputs to your programs. In
29
-
Python, structured data is better represented with classes and enumerations.
30
-
_attrs_ is an excellent library for declaratively describing the structure of
31
-
your data, and validating it.
32
-
33
-
When you're handed unstructured data (by your network, file system, database...),
34
-
_cattrs_ helps to convert this data into structured data. When you have to
35
-
convert your structured data into data types other libraries can handle,
36
-
_cattrs_ turns your classes and enumerations into dictionaries, integers and
37
-
strings.
38
-
39
-
Here's a simple taste. The list containing a float, an int and a string
40
-
gets converted into a tuple of three ints.
16
+
**cattrs** is an open source Python library for structuring and unstructuring data.
17
+
_cattrs_ works best with _attrs_ classes, dataclasses and the usual Python collections, but other kinds of classes are supported by manually registering converters.
18
+
19
+
Python has a rich set of powerful, easy to use, built-in data types like dictionaries, lists and tuples.
20
+
These data types are also the lingua franca of most data serialization libraries, for formats like json, msgpack, cbor, yaml or toml.
21
+
22
+
Data types like this, and mappings like `dict` s in particular, represent unstructured data.
23
+
Your data is, in all likelihood, structured: not all combinations of field names or values are valid inputs to your programs.
24
+
In Python, structured data is better represented with classes and enumerations.
25
+
_attrs_ is an excellent library for declaratively describing the structure of your data and validating it.
26
+
27
+
When you're handed unstructured data (by your network, file system, database...), _cattrs_ helps to convert this data into structured data.
28
+
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.
29
+
30
+
Here's a simple taste. The list containing a float, an int and a string gets converted into a tuple of three ints.
41
31
42
32
```python
43
33
>>>import cattrs
@@ -68,7 +58,7 @@ Here's a much more complex example, involving _attrs_ classes with type metadata
68
58
69
59
```python
70
60
>>>from enum import unique, Enum
71
-
>>>from typing importOptional, Sequence, Union
61
+
>>>from typing import Sequence
72
62
>>>from cattrs import structure, unstructure
73
63
>>>from attrs import define, field
74
64
@@ -91,14 +81,18 @@ Here's a much more complex example, involving _attrs_ classes with type metadata
91
81
>>>@define
92
82
...classDog:
93
83
... cuteness: int
94
-
... chip: Optional[DogMicrochip]=None
84
+
... chip: DogMicrochip|None=None
95
85
96
-
>>> p = unstructure([Dog(cuteness=1, chip=DogMicrochip(chip_id=1, time_chipped=10.0)),
@@ -151,6 +145,9 @@ _cattrs_ is based on a few fundamental design decisions.
151
145
- Un/structuring rules are separate from the models.
152
146
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.
153
147
(_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).)
148
+
- Strongly lean on function composition.
149
+
Almost all problems in _cattrs_ can be solved by writing and composing functions (called _hooks_), instead of writing classes and subclassing.
150
+
This makes _cattrs_ code elegant, concise, powerful and amenable to all the rich Python ways of working with functions.
154
151
- Invent as little as possible; reuse existing ordinary Python instead.
155
152
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).
156
153
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