@@ -40,7 +40,7 @@ to save an instance of ``Decimal`` with {+driver-short+} results in an
40
40
from decimal import Decimal
41
41
42
42
num = Decimal("45.321")
43
- db. coll.insert_one({"num": num})
43
+ db[" coll"] .insert_one({"num": num})
44
44
45
45
.. output::
46
46
:language: shell
@@ -56,8 +56,8 @@ Define a Type Codec Class
56
56
~~~~~~~~~~~~~~~~~~~~~~~~~
57
57
58
58
To encode a custom type, you must first define a **type codec.** A type codec
59
- describes how an instance of a custom type can be
60
- converted to and from one of the types the ``bson`` module can already encode.
59
+ describes how an instance of a custom type is
60
+ converted to and from a type that the ``bson`` module can already encode.
61
61
62
62
When you define a type codec, your class must inherit from one of the base classes in the
63
63
``codec_options`` module. The following table describes these base classes, and when
@@ -75,30 +75,30 @@ and how to implement them:
75
75
* - ``codec_options.TypeEncoder``
76
76
- Inherit from this class to define a codec that
77
77
encodes a custom Python type to a known BSON type.
78
- - - ``python_type`` attribute: The Python type acted on by this type codec
78
+ - - ``python_type`` attribute: The custom Python type that's encoded by this type codec
79
79
- ``transform_python()`` method: Function that transforms a custom type value into a type
80
80
that BSON can encode
81
81
82
82
* - ``codec_options.TypeDecoder``
83
83
- Inherit from this class to define a codec that
84
84
decodes a specified BSON type into a custom Python type.
85
- - - ``bson_type`` attribute: The BSON type acted on by this type codec
85
+ - - ``bson_type`` attribute: The BSON type that's decoded by this type codec
86
86
- ``transform_bson()`` method: Function that transforms a standard BSON type value
87
87
into the custom type
88
88
89
89
* - ``codec_options.TypeCodec``
90
90
- Inherit from this class to define a codec that
91
91
can both encode and decode a custom type.
92
- - - ``python_type`` attribute: The Python type acted on by this type codec
93
- - ``bson_type`` attribute: The BSON type acted on by this type codec
92
+ - - ``python_type`` attribute: The custom Python type that's encoded by this type codec
93
+ - ``bson_type`` attribute: The BSON type that's decoded by this type codec
94
94
- ``transform_bson()`` method: Function that transforms a standard BSON type value
95
95
into the custom type
96
96
- ``transform_python()`` method: Function that transforms a custom type value into a type
97
97
that BSON can encode
98
98
99
99
Because the example ``Decimal`` custom type can be converted to and from a
100
100
``Decimal128`` instance, you must define how to encode and decode this type.
101
- Therefore, the``Decimal`` type codec class must inherit from
101
+ Therefore, the ``Decimal`` type codec class must inherit from
102
102
the ``TypeCodec`` base class:
103
103
104
104
.. code-block:: python
@@ -125,6 +125,9 @@ To do so, create an instance of the ``TypeRegistry`` class, passing in an instan
125
125
of your type codec class inside a list. If you create multiple custom codecs, you can
126
126
pass them all to the ``TypeRegistry`` constructor.
127
127
128
+ The following code examples adds an instance of the ``DecimalCodec`` type codec to
129
+ the type registry:
130
+
128
131
.. code-block:: python
129
132
130
133
from bson.codec_options import TypeRegistry
@@ -196,8 +199,8 @@ Encode a Subtype
196
199
----------------
197
200
198
201
You might also need to encode one or more types that inherit from your custom type.
199
- Consider this subtype of the ``Decimal`` type , which contains a method to return its value
200
- as an integer:
202
+ Consider the following subtype of the ``Decimal`` class , which contains a method to
203
+ return its value as an integer:
201
204
202
205
.. code-block:: python
203
206
@@ -225,14 +228,15 @@ codec for it, {+driver-short+} raises an error:
225
228
of type: <class 'decimal.Decimal'>
226
229
227
230
To encode an instance of the ``DecimalInt`` class, you must define a type codec for
228
- the class. This type codec must inherit from the parent class's codec:
231
+ the class. This type codec must inherit from the parent class's codec, ``DecimalCodec``,
232
+ as shown in the following example:
229
233
230
234
.. code-block:: python
231
235
232
236
class DecimalIntCodec(DecimalCodec):
233
237
@property
234
238
def python_type(self):
235
- # The Python type acted upon by this type codec
239
+ # The Python type encoded by this type codec
236
240
return DecimalInt
237
241
238
242
You can then add the sublcass's type codec to the type registry and encode instances
@@ -252,7 +256,6 @@ of the custom type:
252
256
codec_options = CodecOptions(type_registry=type_registry)
253
257
254
258
collection = db.get_collection("test", codec_options=codec_options)
255
- collection.drop()
256
259
collection.insert_one({"num": DecimalInt("45.321")})
257
260
258
261
my_doc = collection.find_one()
@@ -273,7 +276,7 @@ Define a Fallback Encoder
273
276
274
277
You can also register a **fallback encoder**, a callable to encode types not recognized
275
278
by BSON and for which no type codec has been registered.
276
- Like the ``transform_python()`` method, the fallback encoder accepts an unencodable
279
+ The fallback encoder accepts an unencodable
277
280
value as a parameter and returns a BSON-encodable value.
278
281
279
282
The following fallback encoder encodes Python's ``Decimal`` type to a ``Decimal128``:
@@ -332,9 +335,10 @@ class:
332
335
333
336
Encode Unknown Types
334
337
~~~~~~~~~~~~~~~~~~~~
338
+ .. TODO: consider new example
335
339
336
340
Because fallback encoders don't need to declare the types that they encode
337
- beforehand, you can them in cases where a ``TypeEncoder`` doesn't work.
341
+ beforehand, you can use them in cases where a ``TypeEncoder`` doesn't work.
338
342
For example, you can use a fallback encoder to save arbitrary objects to MongoDB.
339
343
Consider the following arbitrary custom types:
340
344
0 commit comments